Files
whatsfreeinmyfree/backend/index.js
2026-03-19 17:47:37 +00:00

161 lines
4.9 KiB
JavaScript

import e from "express";
import cors from "cors";
import bodyParser from "body-parser";
import bcrypt from "bcrypt";
import Database from "better-sqlite3";
Date.prototype.addHours= function(h){
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}
function getPeriod() {
const now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
if ((hours == 8) || (hours == 9 && minutes < 45)) {
// P1
return 1;
}
if ((hours == 9 && minutes >= 45) || (hours < 11 && minutes < 5)) {
// P2 / Break
return 2;
}
if ((hours == 11 && minutes >= 5) || (hours == 12 && minutes < 5)) {
// P3
return 3;
}
if ((hours == 12 && minutes >= 5) || (hours == 13 && minutes < 5)) {
// P4
return 4;
}
if ((hours == 13 && minutes >= 5) || (hours == 15 && minutes < 15)) {
// Lunch to P5
return 5;
} else {
// too early / late
return -1;
}
}
const app = e();
const port = 3000;
const db = new Database("./database.db");
app.use(cors());
app.use(e.json());
app.use(bodyParser.json());
// app.get('/', (req, res) => {
// res.send("Nothing Here");
// });
app.use('/', e.static('../frontend/dist'));
app.get('/getPeriod', (req, res) => {
// req has nothing
// res has current period
res.status(200).json({period: getPeriod()});
});
app.post('/currentRooms', (req, res) => {
// req has day
// res has all room at current time
console.log("currentRooms");
const currentPeriod = getPeriod();
if (currentPeriod == -1) {
res.status(418).send("Not in school");
return;
}
const today = req.body.day;
let stmt = db.prepare(`SELECT * FROM TimeSlots INNER JOIN Rooms ON Rooms.Id=TimeSlots.Room WHERE Period BETWEEN ${currentPeriod - 1} AND ${currentPeriod + 1} AND Day=${today};`);
let records = stmt.all();
res.status(200).json({records: records});
});
app.post('/addTimeSlot', (req, res) => {
// req has roomid userid period and day
// res has success or faliure
console.log("addTimeSlot");
const body = req.body;
let roomid = parseInt(body.roomid);
let userid = parseInt(body.userid);
let stmt = db.prepare(`INSERT INTO TimeSlots (Period, Day, Room) VALUES (${body.period}, ${body.day}, ${roomid});`);
stmt.run();
stmt = db.prepare(`UPDATE Users SET Submissions = Submissions + 1 WHERE Id=${userid};`);
stmt.run();
res.status(200).send("added timeslot");
});
app.post('/addRoom', (req, res) => {
// req has userid and roomname
// res has success or faliure
console.log("addRoom");
const body = req.body;
let name = req.body.roomName.toUpperCase();
let stmt = db.prepare(`SELECT * FROM Rooms WHERE RoomName='${name}'`);
let storedRecord = stmt.get();
if (storedRecord) {
res.status(418).send("room already exists");
return;
}
stmt = db.prepare(`INSERT INTO Rooms (RoomName) VALUES (${name})`);
stmt.run();
stmt = db.prepare(`UPDATE Users SET Submissions = Submissions + 1 WHERE Id=${parseInt(body.userid)};`);
stmt.run();
res.status(200).send("added room");
});
app.get('/getRooms', (req, res) => {
// req has no data
// res has success or faliure
console.log("getRooms");
let stmt = db.prepare(`SELECT * FROM Rooms`);
let records = stmt.all();
res.status(200).send({records: records});
});
app.post('/createUser', async (req, res) => {
// req has email pass(hashed) and name
// res has success or fail
console.log("sign up");
const body = req.body;
let stmt = db.prepare(`SELECT * FROM Users WHERE Email='${body.email}';`);
let storedRecord = stmt.get();
if (storedRecord) {
res.status(418).send("account with that email already exists");
return;
}
const generatedHash = await bcrypt.hash(body.pass, 10);
stmt = db.prepare(`INSERT INTO Users (Email, Pass, Username, Submissions) VALUES ('${body.email}', '${generatedHash}', '${body.name}', 0)`);
stmt.run();
stmt = db.prepare(`SELECT Id FROM Users WHERE Email='${body.email}';`);
const uid = stmt.get();
res.status(200).send({uid: uid});
});
app.post('/login', async (req, res) => {
// req has email and pass(hashed)
// res has success or fail
console.log("login");
const body = req.body;
let stmt = db.prepare(`SELECT Pass FROM Users WHERE Email='${body.email}';`);
let storedHash = stmt.get();
if (!storedHash) {
res.status(418).send("problem with email");
return;
}
if (await bcrypt.compare(body.pass, storedHash.Pass)){
stmt = db.prepare(`SELECT Id FROM Users WHERE Email='${body.email}';`);
const uid = stmt.get();
res.status(200).send({uid: uid});
} else {
res.status(418).send("incorrect password");
}
});
app.listen(port, () => {
console.log(`Listening on ${port}`);
});