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 1 - Completed / Full Stack Roadmap #598

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
102 changes: 36 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,43 @@
const express = require('express')
const app = express()
const port = 3001
const express = require('express');
const app = express();
const port = 3001;
// JSON body parsor
app.use(express.json());

const USERS = [];
// Dummy database
const USERS = [{}];

const QUESTIONS = [{
title: "Two states",
description: "Given an array , return the maximum of the array?",
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!')
})

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!")
app.get('/',(req,res)=>{
res.json({message:"This is home page"});
});


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('/signup', (req, res) => {
const { email, password } = req.body;
// Creating an existing user
const exisitingUser = USERS.find((user) => user.email === email);
if (exisitingUser) {
return res.status(401).json({ message: "User already exists" });
}
USERS.push({ email, password });
// Returning a status
// Console logging the user object
console.log(USERS);
res.status(200).json({ message: "User created 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('/login',(req,res)=>{
const {email,password} = req.body;
const user = USERS.find((user)=>{user.email===email});
if(!user || user.password !==password){
return (res.status(403).json({message:"Email not found! Please Sign in first"}));
}
// Creating a token
const token = Math.random().toString(36).substring(7);
res.sendStatus(200).json({message:"Login Successful",token});
console.log(user,token);
})


app.listen(port, function() {
console.log(`Example app listening on port ${port}`)
})
app.listen(port,()=>{
console.log(`The server is listening at http://localhost:${port}`);
});
Loading