-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
327 lines (281 loc) · 10.4 KB
/
app.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
console.clear();
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const bodyParser = require('body-parser');
const { ObjectId } = mongoose.Types;
const Decimal = require('decimal.js');
// Middleware
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.urlencoded({ extended: false }));
// Set up EJS as the templating engine
app.set('view engine', 'ejs');
// MongoDB Connection
mongoose.connect('mongodb://localhost:27017/WebApp')
.then(() => {
console.log('MongoDB connected');
}).catch(err => {
console.error('MongoDB connection error:', err);
});
// Set up sessions
app.use(session({
secret: 'your_secret_key',
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: 'mongodb://localhost:27017/WebApp',
collectionName: 'sessions'
}),
cookie: { maxAge: 1000 * 60 * 60 * 24 } // 1 day
}));
const userSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
coins: { type: Number, required: true, default: 0.0}
});
const timerSchema = new mongoose.Schema({
programID: { type: mongoose.Schema.Types.ObjectId, ref: 'TimerProgram', required: true },
name: {
type: String,
required: true,
enum: ['study', 'break'] // Only allow 'study' or 'break'
},
duration: {
type: Number,
required: true
}
});
// Define the TimerProgram schema
const timerProgramSchema = new mongoose.Schema({
userID: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
name: {type: String, required: true}
});
const dueDateSchema = new mongoose.Schema({
userID: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
dateTime: { type: Date, required: true },
title: { type: String, required: true },
description: { type: String, required: true }
});
const TimerProgram = mongoose.model('TimerProgram', timerProgramSchema);
const Timer = mongoose.model('Timer', timerSchema);
const User = mongoose.model('Users', userSchema);
const DueDate = mongoose.model('DueDate', dueDateSchema);
app.get('/play-sound', (req, res) => {
try {
const soundFilePath = path.join(__dirname, 'public', 'samsung.mp3');
res.sendFile(soundFilePath);
} catch (error) {
console.error('Error playing sound:', error);
res.status(500).send('Error playing sound');
}
});
app.post('/create-due-date', async (req, res) => {
const { dueDate } = req.body;
const { dueTime } = req.body;
const { title } = req.body;
const { description } = req.body;
const dateTime = new Date(`${dueDate}T${dueTime}:00`);
console.log(description, title, dueTime, dueDate);
try {
const newProgram = await DueDate.create({
userID: req.session.user.id,
dateTime: dateTime,
title: title,
description: description
});
res.status(200).render('times', { username: req.session.user.username});
} catch (error) {
console.error('Error creating timers:', error);
res.status(500).json({ status: 'error', message: 'An error occurred while creating timers.' });
}
});
app.post('/delete-program/:parsedProgramID', async (req, res) => {
const { parsedProgramID } = req.params;
console.log(parsedProgramID);
try {
await Timer.deleteMany({ programID: parsedProgramID });
await TimerProgram.findOneAndDelete({ _id: parsedProgramID });
res.status(200).send('Program and associated timers deleted successfully');
} catch (error) {
console.error(error);
res.status(500).send('Failed to delete program and associated timers');
}
});
app.post('/create-timer-program', async (req, res) => {
const { timers } = req.body;
const { programName } = req.body;
try {
const newProgram = await TimerProgram.create({
userID: req.session.user.id,
name: programName
});
for (let i = 0; i < timers.length; i++) {
await Timer.create({
programID: newProgram.id,
name: timers[i].name,
duration: (Number(timers[i].hours) * 60 + Number(timers[i].minutes))
});
}
console.log(`${timers.length} timers created.`);
res.status(200).json({ status: 'complete' });
} catch (error) {
console.error('Error creating timers:', error);
res.status(500).json({ status: 'error', message: 'An error occurred while creating timers.' });
}
});
app.post('/register', async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const newUser = await User.create({
username: req.body.username,
password: hashedPassword
});
res.status(200).sendFile(path.join(__dirname, 'register_success.html'));
} catch (error) {
res.status(400).json({ message: error.message });
}
});
app.post('/delete-pomodoro', async (req, res) => {
const { timerName } = req.body;
try {
// Use Mongoose to find the pomodoro and delete it
const deletedPomodoro = await TimerProgram.findOneAndDelete({ timerName });
if (!deletedPomodoro) {
return res.status(404).json({ message: 'Pomodoro not found' });
}
res.status(200).json({ message: 'Pomodoro deleted successfully' });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'login.html'));
});
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'login.html'));
});
app.get('/times', (req,res)=>{
res.render('times', { username: req.session.user.username});
})
app.post('/login', async (req, res) => {
const { username, password } = req.body;
try {
const existingUser = await User.findOne({ username });
if (!existingUser) {
return res.status(404).json({ message: 'User not found' });
}
// Validate password
const isPasswordValid = await bcrypt.compare(password, existingUser.password);
if (!isPasswordValid) {
return res.status(401).json({ message: 'Invalid password' });
}
// Store user data in session
req.session.user = {
id: existingUser._id,
username: existingUser.username,
coins: existingUser.coins
};
res.status(200).redirect(`/dashboard`);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
app.get('/dashboard', async (req, res) => {
if (!req.session.user) {
return res.status(401).redirect('/login');
}
const dueDates = await DueDate.find({ userID: req.session.user.id });
const user = await User.findOne({ username: req.session.user.username });
const programs = await TimerProgram.find({ userID: req.session.user.id });
const coins = user ? user.coins : 0;
//console.log(programs);
res.render('dashboard', { username: req.session.user.username, coins: coins, programs: programs, dueDates: dueDates });
});
app.get('/timerdata/:programID', async (req, res) => {
const { programID } = req.params;
try {
const timers = await Timer.find({ programID: programID });
const dueDates = await DueDate.find({ userID: req.session.user.id });
if (!timers.length) {
return res.status(404).json({ message: "No timers found for the specified program." });
}
if (!req.session.user) {
return res.status(401).redirect('/login');
}
const user = await User.findOne({ username: req.session.user.username });
const programs = await TimerProgram.find({ userID: req.session.user.id });
const coins = user ? user.coins : 0;
res.render('dashboardActive', { username: req.session.user.username, coins: coins, programs: programs, timers: timers, dueDates: dueDates });
} catch (error) {
console.error("Error fetching timers:", error);
res.status(500).json({ error: "An error occurred while fetching the timers." });
}
});
app.post('/increment-coins', async (req, res) => {
if (!req.session.user) {
return res.status(401).json({ message: 'Unauthorized' });
}
const { coins } = req.body;
try {
await User.updateOne(
{ username: req.session.user.username },
{ $inc: { coins: Number(coins) } } // Convert coins to Number for incrementing
);
req.session.user.coins = req.session.user.coins + Number(coins); // Update the session data correctly
res.status(200).json({ message: 'Coins incremented successfully' });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.get('/profile', (req, res) => {
if (!req.session.user) {
return res.status(401).redirect('/login');
}
res.render('profile', { username: req.session.user.username });
});
app.get('/due-dates', (req, res) => {
if (!req.session.user) {
return res.status(401).redirect('/login');
}
res.render('due-dates', { username: req.session.user.username });
});
app.get('/feed', async (req, res) => {
if (!req.session.user) {
return res.status(401).redirect('/login');
}
try {
const users = await User.find({ });
res.render('feed', { username: req.session.user.username, users });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.get('/leaderboard', async (req, res) => {
try {
// Find all users and sort by coins in descending order
const users = await User.find({}).sort({ coins: -1 });
res.render('leaderboard', { username: req.session.user.username, users });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.get('/signout', (req, res) => {
req.session.destroy((err) => {
if (err) {
return res.status(500).json({ message: 'Failed to log out' });
}
res.redirect('/login');
});
});
// Start Server
const PORT = 5000;
app.listen(PORT,'0.0.0.0', () => {
console.log(`Server is running on http://localhost:${PORT}`);
});