forked from hkirat/full-stack-assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
173 lines (146 loc) · 4.02 KB
/
index.js
File metadata and controls
173 lines (146 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import express from "express"
import bodyParser from "body-parser";
import { fileURLToPath } from "url";
import { dirname } from "path";
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express()
const port = 3000;
const USERS = [
{name: "admin", password: "123"},
{name: "user", password: "123"}
];
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 = []
app.use(bodyParser.urlencoded({ extended: true }))
app.get('/', (req, res) => {
res.sendFile(__dirname + "/public/index.html")
})
app.post('/signup', function(req, res) {
let name = req.body.name;
USERS.forEach(element => {
if(element.name === name) {
res.send("This name already exists. Try a different name")
}
});
USERS.push(req.body)
res.send(`Welcome ${name} to the community!`)
console.log(USERS)
})
app.post('/login', function(req, res) {
let name = req.body.name;
let password = req.body.password;
const user = USERS.find((user) => user.name === name);
if (!user || user.password !== password) {
return res.status(401).send('Invalid credentials.');
}
const token = Math.random().toString(36).substring(7);
console.log('Access token:', token)
res.send(`<html>
<body>
<h1>Welcome to the community</h1>
<h2>Name: ${name}</h2>
<a href="/questions">Questions</a>
</body>
<div>
${name === 'admin' ?
`<form method='post' action='/questions'>
<h2>Add a new problem statement:</h2>
<div><input name='title' placeholder='Title of the question:'/></div>
<div><input name='description' placeholder='Describe the question:' /></div>
<div><input name='testcase' placeholder='Enter the testcases:'/></div>
<div><input name='output' placeholder='Enter the expected output:' /></div>
<h3>
<button type='submit'>Add this question to the list.</button>
</h3>
</form>`:'<div />'}
</div>
</html>`)
})
app.post('/questions', (req, res) => {
res.send(`
This question is posted. <a href="/questions">Checkout in the Problems list.</a>
`)
console.log(req.body.title)
QUESTIONS.push(
{
title: req.body.title,
description: req.body.description,
testCases: [{
input: req.body.testcase,
output: req.body.output
}]
}
)
})
app.get('/questions', function(req, res) {
const questionData = ` <html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Questions List</h1>
<ul>
${QUESTIONS.map((question) => `
<li>
<h2>${question.title}</h2>
<p>${question.description}</p>
<h3>Test Cases:</h3>
<ul>
${question.testCases.map((testCase) => `
<li>
<strong>Input:</strong> ${testCase.input}<br>
<strong>Output:</strong> ${testCase.output} <br>
<a href="/submissions">
<button>Submit your answer</button>
</a>
</li>
`)}
</ul>
</li>
`)}
</ul>
</body>
</html>`
res.send(questionData)
})
app.get("/submissions", function(req, res) {
res.send(`<html>
<form method="post" action="/submissions">
<textarea name="answer" id="answerid"
style="width: 100%;
height: 200px;
padding: 10px;
font-size: 16px;"
>type your code</textarea>
<button type="submit">Submit</button>
</form>
</html>`)
});
app.post("/submissions", function(req, res) {
const status = Math.random() < 0.5;
const acceptedStatement = status ? "Your answer is accepted" : "Wrong answer. Please try again."
SUBMISSION.push(req.body)
console.log(SUBMISSION)
res.send(acceptedStatement)
});
app.listen(port, function() {
console.log(`Example app listening on port ${port}`)
console.log(USERS)
})