diff --git a/backend/index.js b/backend/index.js index e97ef0f..754b21e 100644 --- a/backend/index.js +++ b/backend/index.js @@ -4,12 +4,19 @@ import bodyParser from "body-parser"; import bcrypt from "bcrypt"; import Database from "better-sqlite3"; +const app = e(); +const port = 3000; +const db = new Database("./database.db"); + +app.use(cors()); +app.use(e.json()); +app.use(bodyParser.json()); + 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(); @@ -18,7 +25,7 @@ function getPeriod() { // P1 return 1; } - if ((hours == 9 && minutes >= 45) || (hours < 11 && minutes < 5)) { + if ((hours == 9 && minutes >= 45) || (hours == 11 && minutes < 5) || (hours == 10)) { // P2 / Break return 2; } @@ -30,7 +37,7 @@ function getPeriod() { // P4 return 4; } - if ((hours == 13 && minutes >= 5) || (hours == 15 && minutes < 15)) { + if ((hours == 13 && minutes >= 5) || (hours == 15 && minutes < 15) || (hours == 14)) { // Lunch to P5 return 5; } else { @@ -39,17 +46,10 @@ function getPeriod() { } } -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"); -// }); +function incrementUserSubmissions(userid) { + const stmt = db.prepare(`UPDATE Users SET Submissions = Submissions + 1 WHERE Id=${parseInt(userid)};`); + stmt.run(); +} app.use('/', e.static('../frontend/dist')); @@ -69,7 +69,7 @@ app.post('/currentRooms', (req, res) => { 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 stmt = db.prepare(`SELECT TimeSlots.Id, TimeSlots.Period, TimeSlots.Day, Timeslots.Room, Rooms.RoomName FROM TimeSlots INNER JOIN Rooms ON Rooms.Id=TimeSlots.Room WHERE TimeSlots.Period BETWEEN ${currentPeriod - 1} AND ${currentPeriod + 1} AND Timeslots.Day=${today} ORDER BY Rooms.RoomName ASC;`); let records = stmt.all(); res.status(200).json({records: records}); }); @@ -83,11 +83,23 @@ app.post('/addTimeSlot', (req, res) => { 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(); + incrementUserSubmissions(userid) res.status(200).send("added timeslot"); }); +app.post('/removeTimeSlot', (req, res) => { + // req has roomid and userid + // ress has success or faliure + console.log("removeTimeSlot"); + const body = req.body; + const slotid = parseInt(body.Id); + const userid = parseInt(body.userId); + let stmt = db.prepare(`DELETE FROM TimeSlots WHERE Id=${slotid};`); + stmt.run(); + incrementUserSubmissions(userid); + res.status(200).send("removed timeslot"); +}) + app.post('/addRoom', (req, res) => { // req has userid and roomname // res has success or faliure @@ -100,10 +112,9 @@ app.post('/addRoom', (req, res) => { 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 = db.prepare(`INSERT INTO Rooms (RoomName) VALUES ('${name}')`); stmt.run(); + incrementUserSubmissions(parseInt(body.userid)); res.status(200).send("added room"); }); @@ -111,7 +122,7 @@ 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 stmt = db.prepare(`SELECT * FROM Rooms ORDER BY RoomName ASC;`); let records = stmt.all(); res.status(200).send({records: records}); }); @@ -128,7 +139,7 @@ app.post('/createUser', async (req, res) => { 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 = 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(); diff --git a/frontend/src/components/AddRoom.vue b/frontend/src/components/AddRoom.vue index a5ed8ad..04119c8 100644 --- a/frontend/src/components/AddRoom.vue +++ b/frontend/src/components/AddRoom.vue @@ -11,9 +11,10 @@ const roomName = ref(); async function addRoom(e: SubmitEvent) { e.preventDefault(); let uid = await $cookies.get("userId"); + console.log(uid); const res = await fetch("/addRoom", { method: "POST", - body: JSON.stringify({ roomName: roomName.value, userid: uid?.value }), + body: JSON.stringify({ roomName: roomName.value, userid: uid }), headers: new Headers({'content-type': 'application/json'}) }); if (res.ok) { diff --git a/frontend/src/components/AddTimeSlot.vue b/frontend/src/components/AddTimeSlot.vue index 4a3494d..a70732f 100644 --- a/frontend/src/components/AddTimeSlot.vue +++ b/frontend/src/components/AddTimeSlot.vue @@ -16,6 +16,11 @@ onBeforeMount(async () => { method: "GET" }); rooms.value = await res.json(); + const date = new Date(); + day.value = date.getDay(); + res = await fetch("getPeriod", { method: "GET" }); + const json = await res.json(); + period.value = json.period; }); async function addTimeSlot(e: SubmitEvent) { @@ -23,7 +28,7 @@ async function addTimeSlot(e: SubmitEvent) { let uid = await $cookies.get("userId"); await fetch("/addTimeSlot", { method: "POST", - body: JSON.stringify({ roomid: room.value.Id, userid: uid?.value, day: (day.value * weekNumber.value), period: period.value }), + body: JSON.stringify({ roomid: room.value.Id, userid: uid, day: (day.value * weekNumber.value), period: period.value }), headers: new Headers({'content-type': 'application/json'}) }); emit('goHome'); diff --git a/frontend/src/components/HomePage.vue b/frontend/src/components/HomePage.vue index 12c5a37..d09724d 100644 --- a/frontend/src/components/HomePage.vue +++ b/frontend/src/components/HomePage.vue @@ -1,5 +1,10 @@