From 838d48236397191abc758b1518ef2816737fa1c5 Mon Sep 17 00:00:00 2001 From: harshitc0ding <145226558+harshitc0ding@users.noreply.github.com> Date: Sat, 20 Jul 2024 17:12:09 +0530 Subject: [PATCH] did the assignment from vid1 --- index.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index e189e725..a230ae93 100644 --- a/index.js +++ b/index.js @@ -21,50 +21,95 @@ const SUBMISSION = [ app.post('/signup', function(req, res) { // Add logic to decode body // body should have email and password + const{email, password,admin} = req.body; + //the admin here is a bool value which can be used further whenever questions have to be created + //Store email and password (as it is for now) in the USERS array above (only if the user with the given email doesnt exist) + const userExists=USERS.some(user=>user,email===email); - //Store email and password (as is for now) in the USERS array above (only if the user with the given email doesnt exist) - + //if user exists then- + if(userExists){ + return res.status(400).send("user already exists"); + } + //if user does not exist then- + //storing the new user's email and password in the USERS array above + USERS.push({email,password,admin}); // return back 200 status code to the client - res.send('Hello World!') + res.status(200).send("signed up successfully") }) app.post('/login', function(req, res) { // Add logic to decode body // body should have email and password + const {email, password} = req.body; // Check if the user with the given email exists in the USERS array + const userExists = USERS.some(user=>user.email===email); + if(userExists){ + return res.status(200).send("user exists and can be logged in"); + } + // Also ensure that the password is the same + const isSamePassword =USERS.some(user=>user.password===password); // If the password is the same, return back 200 status code to the client - // Also send back a token (any random string will do for now) - // If the password is not the same, return back 401 status code to the client + // Also sent back a token - "abc" + if(isSamePassword){ + const token="abc"; + return res.status(200).send(token); + } - - res.send('Hello World from route 2!') + + // If the password is not the same, return back 401 status code to the client + if(!isSamePassword){ + return res.status(401).send("invalid password"); + } }) app.get('/questions', function(req, res) { //return the user all the questions in the QUESTIONS array - res.send("Hello World from route 3!") + res.status(200).json(QUESTIONS); + }) app.get("/submissions", function(req, res) { // return the users submissions for this problem - res.send("Hello World from route 4!") + }); +function getRandomAcceptance() { + return Math.random() >= 0.5; // 50% chance of acceptance +} app.post("/submissions", function(req, res) { // let the user submit a problem, randomly accept or reject the solution + const{problemId,user,code}=req.body; + // Store the submission in the SUBMISSION array above - res.send("Hello World from route 4!") + const newSubmission={ + id:SUBMISSIONS.length+1,//generated new id + problemId, + user, + code, + isAccepted:getRandomAcceptance() + + } }); -// leaving as hard todos + +app.post("/addProblem",function(req,res){ + const{admin}=req.body; + + if(!admin){ + return res.status(403).send("only admins can add problems"); + } + + res.status(200).send("you are an admin"); +}) + // Create a route that lets an admin add a new problem // ensure that only admins can do that.