diff --git a/index.js b/index.js index e189e725..bd2209c9 100644 --- a/index.js +++ b/index.js @@ -1,73 +1,91 @@ -const express = require('express') -const app = express() -const port = 3001 +const express = require("express"); +const app = express(); +const port = 3001; const USERS = []; -const QUESTIONS = [{ +const QUESTIONS = [ + { title: "Two states", description: "Given an array , return the maximum of the array?", - testCases: [{ + testCases: [ + { input: "[1,2,3,4,5]", - output: "5" - }] -}]; - - -const SUBMISSION = [ - -] - -app.post('/signup', function(req, res) { - // Add logic to decode body - // body should have email and password - - - //Store email and password (as is for now) in the USERS array above (only if the user with the given email doesnt exist) - - - // return back 200 status code to the client - res.send('Hello World!') -}) - -app.post('/login', function(req, res) { - // Add logic to decode body - // body should have email and password - - // Check if the user with the given email exists in the USERS array - // Also ensure that the password is the same - - - // 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 - - - res.send('Hello World from route 2!') -}) + output: "5", + }, + ], + }, +]; + +const SUBMISSION = []; + +//Creating Signup +app.post("/signup", function (req, res) { + const { email, password } = req.body; + + // if (!USERS.email && !USERS.password) { + // res.send("Email and password is required"); + // } + + const userExist = USERS.some((user) => user.email == email); + + if (!userExist) { + USERS.push({ email, password }); + res.status(200).send("User created successfully"); + console.log("Signup Successfull"); + } else { + res.status(400).send("User with this email already exists"); + } +}); -app.get('/questions', function(req, res) { +//Creating Login +app.post("/login", function (req, res) { + const { email, password } = req.body; + + const user = USERS.some( + (user) => user.email == email && user.password == password + ); + + const token = Math.random().toString(36).substring(2); + if (user) { + res.status(200).send({ token }); + console.log("Login Successfull"); + } else { + res.status(400).send("Invalid email or password"); + } +}); +app.get("/questions", function (req, res) { //return the user all the questions in the QUESTIONS array - res.send("Hello World from route 3!") -}) - -app.get("/submissions", function(req, res) { - // return the users submissions for this problem - res.send("Hello World from route 4!") + res.status(200).send(QUESTIONS); + res.send("Hello from route3"); }); +app.get("/submissions", function (req, res) { + // return the users submissions for this problem + res.status(200).send(SUBMISSION); + res.send("Hello from route4"); +}); -app.post("/submissions", function(req, res) { - // let the user submit a problem, randomly accept or reject the solution - // Store the submission in the SUBMISSION array above - res.send("Hello World from route 4!") +app.post("/submissions", function (req, res) { + // let the user submit a problem, randomly accept or reject the solution + // Store the submission in the SUBMISSION array above + const { problem, solution } = req.body; + const submission = { problem, solution, accepted: Math.random() < 0.5 }; + SUBMISSION.push(submission); + + res.send( + `Submission received! Your solution was ${ + submission.accepted ? "accepted" : "rejected" + }` + ); + s; }); // leaving as hard todos // Create a route that lets an admin add a new problem // ensure that only admins can do that. -app.listen(port, function() { - console.log(`Example app listening on port ${port}`) -}) \ No newline at end of file +app.listen(port, function () { + console.log(`Example app listening on port ${port}`); +}); diff --git a/package.json b/package.json index a891f8f9..21e487c1 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { "name": "express-backend-app", "version": "1.0.0", - "description": "", "main": "index.js", "scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, - "author": "", + "author": "Pratik Yesane", "license": "ISC", "dependencies": { "express": "^4.18.2" - } + }, + "description": "" }