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

Full Stack Assignment done from the video 1 of the full stack series #623

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
67 changes: 56 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down