Skip to content

Commit

Permalink
Merge pull request #8 from Killerrekt/master
Browse files Browse the repository at this point in the history
Submission API PR
  • Loading branch information
Mr-Emerald-Wolf authored Aug 26, 2023
2 parents 645770c + b22a406 commit ff96411
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# Environment
.env

/node_modules
/node_modules
35 changes: 35 additions & 0 deletions api/controllers/submission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const submission_db = require("../models/submission.js");
const questiondb = require("../models/ques.js");
const testdb = require("../models/testCasesModel.js");

class submission{
async create(req,res){
const {user,language,code,question_id} = req.body;
await submission_db.create({user : user, language : language, code : code, question_id : question_id})
.then(() => console.log("Data has been entered into the DB")).catch(err => console.log(err));
res.sendStatus(201);
}

async getdata(req,res){
const {question_id,code,language_id} = req.body;
const testcase = await questiondb.findById(question_id,'testCases');
for(let i = 0;i<testcase.length;i++){
const current = await testdb.findById(testcase[i]);
const data_sent_to_judge0 = {
source_code : code,
language_id : language_id,
stdin : current.input,
expected_output : current.expectedOutput,
cpu_time_limit : current.time,
memory_limit : current.memory,
}
//now this data_sent_to_judge0 can be sent to the get submission route of judge0 API and that will return a token
//it can then use the given token to post the submission to see the results
console.log(data_sent_to_judge0);
}
res.sendStatus(200);//in near future need to add the score to a score schema so status will turn to 201 and hence request is a post
//need to test this function call
}
}

module.exports = submission;
14 changes: 14 additions & 0 deletions api/models/submission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const {Schema,model} = require("mongoose");

const submission_schema = new Schema({
user : {type:Number,required:true},
language : {type:String,required:true},
code : {type:String,required:true},
pass : {type:Boolean,required:true,default:false},
question_id : {type:Number}}, //this will act as an indicator to which question in the question DB shld be tested against the code
{timestamps : true}
);

const submission_db = model("Submissions",submission_schema);

module.exports = submission_db;
19 changes: 19 additions & 0 deletions api/routes/submission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require("express");
const router = express.Router();
const submission = require("../controllers/submission.js");

const submit = new submission;

router.use(express.json());
router.use(express.urlencoded())

router.post('/create', (req, res) => {
//console.log("The route worked");
submit.create(req,res);
})

router.post("/test",(req,res) =>{
submit.getdata(req,res);
})

module.exports = router
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const os = require("os");
const TestCaseRouter = require("./api/routes/testCaseRouter");
const authRoute = require("./api/routes/auth");
const quesRoute = require("./api/routes/questionsRouter");
const subroute = require("./api/routes/submission");
require("dotenv").config();

const app = express();
Expand Down Expand Up @@ -34,6 +35,7 @@ app.get("/ping", (_, res) => {
app.use("/api/testcases/", TestCaseRouter);
app.use("/auth/", authRoute);
app.use("/ques/", quesRoute);
app.use("/submit/",subroute);

// Starting Server
app.listen(8080, () => {
Expand Down
Loading

0 comments on commit ff96411

Please sign in to comment.