diff --git a/backend/createtable.sql b/backend/createtable.sql
index 76ae4b3..626651c 100644
--- a/backend/createtable.sql
+++ b/backend/createtable.sql
@@ -16,8 +16,8 @@ CREATE TABLE Rooms (
CREATE TABLE TimeSlots (
Id INTEGER PRIMARY KEY ASC AUTOINCREMENT,
- TimeStart TEXT,
- TimeEnd TEXT,
+ Period INTEGER,
+ Day INTEGER,
Room INTEGER,
FOREIGN KEY(Room) REFERENCES Rooms(Id)
);
\ No newline at end of file
diff --git a/backend/database.db b/backend/database.db
index 2e622c3..6e46152 100644
Binary files a/backend/database.db and b/backend/database.db differ
diff --git a/backend/index.js b/backend/index.js
index 51db8b5..6da81b4 100644
--- a/backend/index.js
+++ b/backend/index.js
@@ -5,8 +5,38 @@ import bcrypt from "bcrypt";
import Database from "better-sqlite3";
Date.prototype.addHours= function(h){
-this.setTime(this.getTime() + (h*60*60*1000));
- return this;
+ 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();
@@ -21,24 +51,35 @@ app.get('/', (req, res) => {
res.send("Nothing Here");
});
-app.get('/currentRooms', (req, res) => {
- // req has no data
+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 today = new Date();
- let stmt = db.prepare(`SELECT * FROM TimeSlots INNER JOIN Rooms ON Rooms.Id=TimeSlots.Room WHERE TimeEnd BETWEEN '${today.toISOString()}' AND '${today.addHours(1).toISOString()}';`);
+ 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 starttime and end time
+ // 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 (TimeStart, TimeEnd, Room) VALUES ('${body.startTime}', '${body.endTime}', ${roomid});`);
+ 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();
@@ -54,7 +95,7 @@ app.post('/addRoom', (req, res) => {
let stmt = db.prepare(`SELECT * FROM Rooms WHERE RoomName='${name}'`);
let storedRecord = stmt.get();
if (storedRecord) {
- res.status(400).send("room already exists");
+ res.status(418).send("room already exists");
return;
}
stmt = db.prepare(`INSERT INTO Rooms (RoomName) VALUES (${name})`);
@@ -81,7 +122,7 @@ app.post('/createUser', async (req, res) => {
let stmt = db.prepare(`SELECT * FROM Users WHERE Email='${body.email}';`);
let storedRecord = stmt.get();
if (storedRecord) {
- res.status(400).send("account with that email already exists");
+ res.status(418).send("account with that email already exists");
return;
}
const generatedHash = await bcrypt.hash(body.pass, 10);
@@ -100,7 +141,7 @@ app.post('/login', async (req, res) => {
let stmt = db.prepare(`SELECT Pass FROM Users WHERE Email='${body.email}';`);
let storedHash = stmt.get();
if (!storedHash) {
- res.status(400).send("problem with email");
+ res.status(418).send("problem with email");
return;
}
if (await bcrypt.compare(body.pass, storedHash.Pass)){
@@ -108,7 +149,7 @@ app.post('/login', async (req, res) => {
const uid = stmt.get();
res.status(200).send({uid: uid});
} else {
- res.status(400).send("incorrect password");
+ res.status(418).send("incorrect password");
}
});
diff --git a/backend/setup.js b/backend/setup.js
index 402fac4..f62580f 100644
--- a/backend/setup.js
+++ b/backend/setup.js
@@ -19,8 +19,8 @@ CREATE TABLE Rooms (
CREATE TABLE TimeSlots (
Id INTEGER PRIMARY KEY ASC AUTOINCREMENT,
- TimeStart TEXT,
- TimeEnd TEXT,
+ Period INTEGER,
+ Day INTEGER,
Room INTEGER,
FOREIGN KEY(Room) REFERENCES Rooms(Id)
);`);
\ No newline at end of file
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 0b602f6..e4261db 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -17,7 +17,7 @@ onBeforeMount(async () => {
-
-
-
+
+
+
diff --git a/frontend/src/components/AddTimeSlot.vue b/frontend/src/components/AddTimeSlot.vue
index 125287c..c490591 100644
--- a/frontend/src/components/AddTimeSlot.vue
+++ b/frontend/src/components/AddTimeSlot.vue
@@ -5,8 +5,9 @@ const emit = defineEmits(['goHome', 'addRoom']);
const rooms = ref();
const room = ref();
-const startTime = ref();
-const endTime = ref();
+const weekNumber = ref(1);
+const day = ref(1);
+const period = ref(1);
onBeforeMount(async () => {
let res = await fetch("http://localhost:3000/getRooms", {
@@ -20,7 +21,7 @@ async function addTimeSlot(e: SubmitEvent) {
let uid = await cookieStore.get("userId");
await fetch("http://localhost:3000/addTimeSlot", {
method: "POST",
- body: JSON.stringify({ roomid: room.value.Id, userid: uid?.value, startTime: startTime.value, endTime: endTime.value }),
+ body: JSON.stringify({ roomid: room.value.Id, userid: uid?.value, day: (day.value * weekNumber.value), period: period.value }),
headers: new Headers({'content-type': 'application/json'})
});
emit('goHome');
@@ -36,10 +37,12 @@ async function addTimeSlot(e: SubmitEvent) {
-
-
-
-
+
+
+
+
+
+
\ No newline at end of file
diff --git a/frontend/src/components/HomePage.vue b/frontend/src/components/HomePage.vue
index 3d86eaf..3862795 100644
--- a/frontend/src/components/HomePage.vue
+++ b/frontend/src/components/HomePage.vue
@@ -2,25 +2,48 @@
import { onBeforeMount, ref } from 'vue';
const rooms = ref();
+const showRooms = ref(false);
+const weekNumber = ref(1);
+
+async function getData() {
+ showRooms.value = false;
+ const today = new Date();
+ if (today.getDay() == 0 || today.getDay() == 6) {
+ return;
+ }
+ const day = today.getDay() * weekNumber.value;
+ const res = await fetch("http://localhost:3000/currentRooms", {
+ method: "POST",
+ body: JSON.stringify({day: day}),
+ headers: new Headers({'content-type': 'application/json'})
+ });
+ if (res.ok) {
+ rooms.value = await res.json();
+ showRooms.value = true;
+ return;
+ }
+ console.log(await res.text());
+}
defineEmits(['addSlot']);
onBeforeMount(async () => {
- const res = await fetch("http://localhost:3000/currentRooms", {
- method: "GET"
- });
- rooms.value = await res.json();
+ await getData();
});
What's free in my free?
-
+
+
-
+
Free Rooms:
- No free rooms right now
-
- - Room: {{ slot.RoomName }}, from {{ slot.TimeStart.toString().split("T")[1] }} til {{ slot.TimeEnd.toString().split("T")[1] }}
+ No free rooms right now
+
+ - Room: {{ slot.RoomName }}, in {{ slot.Period }}
\ No newline at end of file