Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignment completed #600

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 80 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const express = require('express')
const app = express()
const bodyparser = require('body-parser')

app.use(bodyparser)
const port = 3001

const USERS = [];
Expand All @@ -21,13 +24,28 @@ const SUBMISSION = [
app.post('/signup', function(req, res) {
// Add logic to decode body
// body should have email and password
if(!req.body.email || !req.body.password ){
res.send('Email and Password are mandatory to signup')
}else{
//Store email and password (as is for now) in the USERS array above (only if the user with the given email doesnt exist)

const email = req.body.email
const foundUser= USERS.find((user)=>user.email == email)
if(foundUser){
res.send('Email already exists')
}else{
USERS.push(
{
email : req.body.email,
password : req.body.password ,
role : req.body.role
}
)
// return back 200 status code to the client
res.send('User added successfully').status(200)
}
}


//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) {
Expand All @@ -42,32 +60,83 @@ app.post('/login', function(req, res) {
// 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

if(!req.body.email || !req.body.password ){
res.send('Email and Password are mandatory for login')
}else{
const email = req.body.email
const foundUser= USERS.find((user)=>user.email == email)
if(!foundUser){
res.json({message : 'User does not exist'})
}else{
if(foundUser.password == req.body.password ) {
res.json({message : 'Password does not match' ,status : 401})

}else{
res.json({message : 'Login successful' })
}
}

}


res.send('Hello World from route 2!')
})

app.get('/questions', function(req, res) {

//return the user all the questions in the QUESTIONS array
res.send("Hello World from route 3!")
res.json({questions : QUESTIONS})

})

app.get("/submissions", function(req, res) {
app.get("/submissions/:questionId", function(req, res) {
// return the users submissions for this problem
res.send("Hello World from route 4!")
if(!req.params.questionId){
res.json({message: 'Question id is missing'})
}else{
const questionId = req.params.questionId
const submissionForQuestion = SUBMISSION.filter((sub)=>sub.questionId == questionId)

res.json({message : "Submissions ",submission : submissionForQuestion})
}


});


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!")

const submission = req.body;
submission.id = SUBMISSION.length + 1;
submission.isAccepted = Math.random() > 0.5;

SUBMISSION.push(submission);

res.status(200).send("Submission submitted successfully");
});

// leaving as hard todos
// Create a route that lets an admin add a new problem
// ensure that only admins can do that.

app.post('/questions',(req,res)=>{
const email = req.body.email
const foundUser = USERS.find((user)=> user.email == email)
if(!foundUser){
res.json({message : 'Invalid user'})
}else{
const role = foundUser.role;
if(role =='admin'){
QUESTIONS.push(req.body.question)
res.json({message :'Question added successfully'})

}else{
res.json({message :'only admin can add problem'})
}
}
})

app.listen(port, function() {
console.log(`Example app listening on port ${port}`)
})
102 changes: 86 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2"
}
}