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

Added required changes to index file and added readme file. #90

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
79 changes: 64 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const express = require('express')
const app = express()
const port = 3001

const USERS = [];
const USERS = [
{
email: "xyz@abc.com",
password: "2193#$%^" }
];

const QUESTIONS = [{
title: "Two states",
Expand All @@ -14,60 +18,105 @@ const QUESTIONS = [{
}];


const SUBMISSION = [

]
const SUBMISSION = []
app.use(express.json());

app.post('/signup', function(req, res) {
// Add logic to decode body
// body should have email and password

const { email,password } = req.body

//Store email and password (as is for now) in the USERS array above (only if the user with the given email doesnt exist)
const ExistingUser = USERS.find((user)=>user.email==email)


// If user already exit return
if(ExistingUser){
return res.status(409).send("This email address already exists");
}
USERS.push({ email, password });
// 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
const { email, password } = req.body;
// checking if the email and password were included in the request body
if (!email || !password) {
return res.status(400).send("Both Email and Password required");
}

// Check if the user with the given email exists in the USERS array
// Also ensure that the password is the same
const ExistingUser = USERS.find((user)=>user.email==email && user.password === password)


// If the password is not the same, return back 401 status code to the client
if(!ExistingUser){
return res.status(401).send("Invalid userID or 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


res.send('Hello World from route 2!')
const loginToken = Math.random().toString(11);
res.status(200).json({ loginToken });
})

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!")
res.status(200).json(SUBMISSION);
});


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 { problemId, userId, code } = req.body;

if (!problemId || !userId || !code) {
return res.status(400).send("Problem ID, user ID, and code are required");
}

const newSubmission = { id: SUBMISSIONS.length + 1, problemId, userId, code };
SUBMISSIONS.push(newSubmission);
res.status(201).json(newSubmission);
});

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

// Create an array of admin user IDs
const ADMIN_IDS = [1, 2, 3];

// POST request to add a new problem
app.post("/problems", function (req, res) {
const { title, description, testCases } = req.body;

// Check if the user making the request is an admin
const userId = req.headers["user-id"];
if (!ADMIN_IDS.includes(parseInt(userId))) {
return res.status(401).send("Only admins can add new problems");
}

// Check that all required fields are present in the request body
if (!title || !description || !testCases) {
return res
.status(400)
.send("Title, description, and test cases are required");
}

// Create a new problem object and add it to the PROBLEMS array
const newQuestion = { id: QUESTIONS.length + 1, title, description, testCases };
QUESTIONS.push(newQuestion);
res.status(201).json(newQuestion);
});


app.listen(port, function() {
console.log(`Example app listening on port ${port}`)
})
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Environment Setup

Before you begin with the assignment, you must first configure your development environment.

You will need to have the following software installed on your machine:
Node.js v14 or later
Git

To set up your development environment, please follow these steps:
Clone the repository using Git.
Open a terminal and navigate to the project directory.
Run npm install to install the necessary packages.
Running the server
To run the server, open a terminal in the project directory and execute node index.js. The server will begin running on localhost:3001.


# Assignment Details

The server is a rudimentary HTTP server skeleton with a variety of routes defined. Your task is to implement the missing functionalities for the routes described in the index.js file. Specifically, you must implement the following functionalities: