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 #601

Open
wants to merge 2 commits 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
260 changes: 216 additions & 44 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,245 @@
const express = require('express')
const { use } = require('express/lib/application');
const app = express()
const port = 3001
const port = 3000

// Middleware to parse URL-encoded bodies
app.use(express.urlencoded({ extended: true }));

const USERS = [{ username: "abc", password: 1234 }];
const ADMIN = [{ username: "admin", password: 1234 }];

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"
}]
title: "Two states",
description: "Given an array , return the maximum of the array?",
testCases: [{
input: "[1,2,3,4,5]",
output: "5"
}]
}];


const SUBMISSION = [
const SUBMISSION = [{ username: "abc", submissions: [] }]

]

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

app.get('/', (req, res) => {
res.send(`
<h1>Welcome to codevilla<h1/>
<a href="/login">Login</a><br>
<a href="/signup">SignUp</a>
`);
})

//Store email and password (as is for now) in the USERS array above (only if the user with the given email doesnt exist)
//--------------------Signup--------------------------//
//logic for user registration request
app.get('/signup', (req, res) => {
res.send(`
<form action="/signup" method="post">
<label>User Name</label><br>
<input type="text" id="username" name="username"><br>
<label>New Password</label><br>
<input type="text" id="password" name="password"><br>
<button type="submit">Submit</button>
<form/>`);
})

app.post('/signup', (req, res) => {
let username = req.body.username;
let password = Number(req.body.password);
let check = USERS.find(({ username: user }) => { return user === username })
if (!check) {
USERS.push({ username: username, password: password });
res.redirect('/login')
console.log(user)
}
else {
// res.status(400).json({ error: "username already taken" })
res.send("Username taken")
// res.redirect('/signup')
}
})

// return back 200 status code to the client
res.send('Hello World!')
//---------------Authentication middleware----------//
// function Authenticate(req, res, next) {

// }


//--------------------Login--------------------------//
//logic for user login request
token = "nsknkn1231nk@"
app.get('/login', (req, res) => {
if (req.headers.cookie) {
res.redirect('/questions')
}
res.send(`
<form action="/login" method="post">
<label>Username</label><br>
<input type="text" id="username" name="username"><br>
<label>password</label><br>
<input type="text" id="password" name="password"><br>
<button type="submit">Submit</button>
</form>`)
})
app.post('/login', (req, res) => {
let username = req.body.username;
let password = Number(req.body.password);
console.log(typeof username);
console.log(typeof password);
let check = USERS.find(data => data.username === username);

if (check && check.password === password) {
res.cookie('token', token, { maxAge: 3600000, httpOnly: true });
res.redirect(`/questions?username=${username}`);
} else {
res.status(401).send("Not Authorized");
}
})

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
//--------------------Qusetions--------------------------//
//logic for giving the user random Q in available questions
app.get('/questions', (req, res) => {

// Check if the token is valid (e.g., compare it with a stored token)
if (req.headers.cookie) {
let question_num = Math.floor(Math.random() * QUESTIONS.length)
console.log(question_num)
if (QUESTIONS.length != 0) {
let username = req.query.username;
let title = QUESTIONS[question_num].title;
let description = QUESTIONS[question_num].description;
let testcases = QUESTIONS[question_num].testCases;

//to send json string
// res.write(questionsString);

//to send html
// res.write(htmlContent);

//to end response
// res.end();

res.status(200).send(`
<div>Username : ${username}</div>
<form action='/submissions?username=${username}' method="post">
<ul>
<li>${title}</li>
<li>${description}</li>
<li>input : ${testcases[0].input}</li>
<li>output : ${testcases[0].output}</li>
</ul>
<input id="code" name="code">
<button type="submit">submit</button>
</form>
`)
} else {
res.send("No questions available")
}
} else {
res.send('Not Authorized');
}
})


// 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
app.post('/questions', (req, res) => {
let title = req.body.title
let description = req.body.description
let input = req.body.input
let output = req.body.output

QUESTIONS.push({ title: title, description: description, testCases: [{ input: input, output: output }] })
res.redirect('/login')
})

//--------------------Submissions--------------------------//
//logic to let user make submissions
app.post('/submissions', (req, res) => {
let username = req.query.username;
console.log("username", username)
let code = req.body.code;
if (code === code.toLowerCase()) {
let user_index = SUBMISSION.findIndex(user => user.username === username);
SUBMISSION[user_index].submissions.push(code)
console.log(user_index)
console.log(SUBMISSION[user_index].submissions[0])
res.redirect(`/submissions?userindex=${user_index}`)
} else {
res.send(`
<div>not even close</div>
`)
}
})

res.send('Hello World from route 2!')
//logic to show the user submissions
app.get('/submissions', (req, res) => {
if (req.headers.cookie) {
user_index = req.query.userindex
res.send(`
<div>Your submissions are</div>
<div>${SUBMISSION[user_index].submissions}</div>
<a href='logout'>LOGOUT</a>
`)
}
else {
res.send("Not Authorized")
}
})

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

//return the user all the questions in the QUESTIONS array
res.send("Hello World from route 3!")
//------------------logout---------------------------//
//logi for logging out and clearing cookie
app.get('/logout', (req, res) => {
res.cookie('token', '', { expires: new Date(0), httpOnly: true });
res.redirect('/')
})

app.get("/submissions", function(req, res) {
// return the users submissions for this problem
res.send("Hello World from route 4!")
});

//--------------------admin--------------------------//
//login for admin page
app.get('/admin', (req, res) => {
res.send(`
<form action="/admin" method="post">
<label>admin Username</label><br>
<input type="text" name="username"><br>
<label>Password</label><br>
<input type="text" name="password"><br>
<button type="submit">Submit</button>
</form>
`)
})

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

// leaving as hard todos
// Create a route that lets an admin add a new problem
// ensure that only admins can do that.
//add question for admin
app.post('/admin', (req, res) => {

let adminUser = req.body.username;
let adminPassword = Number(req.body.password);
let check = ADMIN.find(({ username: user }) => { return user === adminUser })
if (check && check.password === adminPassword) {
res.send(`
<form action="/questions" method="post">
<label>Question</label><br>
<input type="text" name="title"><br>
<label>Description</label><br>
<input type="text" name="description"><br>
<label>Testcases:</label><br>
<label>Input</label><br>
<input type="text" name="input"></input><br>
<label>Output</label><br>
<input type="text" name="output"></input><br>
<button type="submit">Submit</button>
</form>
`)
} else {
res.status(401).send("Not Authorized")
}
})

// app.
//-----------------server confirmation------------------------//

app.listen(port, function() {
console.log(`Example app listening on port ${port}`)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
})