-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcodeController.js
More file actions
36 lines (34 loc) · 1.25 KB
/
codeController.js
File metadata and controls
36 lines (34 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const Code=require('../models/Code');
const getCode=async(req,res)=>{
try{
const {id,language}=req.query;
console.log(decodeURIComponent(language));
const code=await Code.findOne({room:id,language:language});
if(!code){
let codeValue='';
if(language==='javascript'){
codeValue="console.log('Hello Worlds')";
}
else if(language==='python'){
codeValue="print('Hello World')";
}
else if(language==='java'){
codeValue="public class HelloWorld{public static void main(String[] args){System.out.println('Hello World');}}";
}
else if(language==="C++(Clang 7.0.1)"){
codeValue="#include<iostream>using namespace std;int main(){cout<<'Hello World'<<endl;return 0;}";
}
const newCode=await Code.create({
room:id,
language:language,
code:codeValue
})
return res.status(201).json({code:newCode});
}
return res.status(200).json({code});
}catch(err){
console.log(err);
return res.status(400).json({msg:"Error in fetching the code"})
}
}
module.exports={getCode};