Compare commits
2 Commits
b636009422
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
5dd8b228f4
|
|||
|
ec0da2ab2f
|
@@ -4,8 +4,14 @@ Record of what classrooms are free when I have a free. CS Practice
|
|||||||
|
|
||||||
## Use
|
## Use
|
||||||
|
|
||||||
To run this, do the folowing:
|
To run this, do the following:
|
||||||
- in `frontend` execute `npm run build`
|
- in `frontend` execute `npm run build`
|
||||||
|
- in `backend` execute `npm run setup`
|
||||||
- in `backend` execute `npm run dev`
|
- in `backend` execute `npm run dev`
|
||||||
|
|
||||||
|
If reseting, do the following:
|
||||||
|
- in `backend` execute `npm run clean`
|
||||||
|
- in `backend` execute `npm run setup`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,19 @@ import bodyParser from "body-parser";
|
|||||||
import bcrypt from "bcrypt";
|
import bcrypt from "bcrypt";
|
||||||
import Database from "better-sqlite3";
|
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){
|
Date.prototype.addHours= function(h){
|
||||||
this.setTime(this.getTime() + (h*60*60*1000));
|
this.setTime(this.getTime() + (h*60*60*1000));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function getPeriod() {
|
function getPeriod() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
let hours = now.getHours();
|
let hours = now.getHours();
|
||||||
@@ -18,7 +25,7 @@ function getPeriod() {
|
|||||||
// P1
|
// P1
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if ((hours == 9 && minutes >= 45) || (hours < 11 && minutes < 5)) {
|
if ((hours == 9 && minutes >= 45) || (hours == 11 && minutes < 5) || (hours == 10)) {
|
||||||
// P2 / Break
|
// P2 / Break
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
@@ -30,7 +37,7 @@ function getPeriod() {
|
|||||||
// P4
|
// P4
|
||||||
return 4;
|
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
|
// Lunch to P5
|
||||||
return 5;
|
return 5;
|
||||||
} else {
|
} else {
|
||||||
@@ -39,17 +46,10 @@ function getPeriod() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = e();
|
function incrementUserSubmissions(userid) {
|
||||||
const port = 3000;
|
const stmt = db.prepare(`UPDATE Users SET Submissions = Submissions + 1 WHERE Id=${parseInt(userid)};`);
|
||||||
const db = new Database("./database.db");
|
stmt.run();
|
||||||
|
}
|
||||||
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.use('/', e.static('../frontend/dist'));
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ app.post('/currentRooms', (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const today = req.body.day;
|
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();
|
let records = stmt.all();
|
||||||
res.status(200).json({records: records});
|
res.status(200).json({records: records});
|
||||||
});
|
});
|
||||||
@@ -83,11 +83,23 @@ app.post('/addTimeSlot', (req, res) => {
|
|||||||
let userid = parseInt(body.userid);
|
let userid = parseInt(body.userid);
|
||||||
let stmt = db.prepare(`INSERT INTO TimeSlots (Period, Day, Room) VALUES (${body.period}, ${body.day}, ${roomid});`);
|
let stmt = db.prepare(`INSERT INTO TimeSlots (Period, Day, Room) VALUES (${body.period}, ${body.day}, ${roomid});`);
|
||||||
stmt.run();
|
stmt.run();
|
||||||
stmt = db.prepare(`UPDATE Users SET Submissions = Submissions + 1 WHERE Id=${userid};`);
|
incrementUserSubmissions(userid)
|
||||||
stmt.run();
|
|
||||||
res.status(200).send("added timeslot");
|
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) => {
|
app.post('/addRoom', (req, res) => {
|
||||||
// req has userid and roomname
|
// req has userid and roomname
|
||||||
// res has success or faliure
|
// res has success or faliure
|
||||||
@@ -100,10 +112,9 @@ app.post('/addRoom', (req, res) => {
|
|||||||
res.status(418).send("room already exists");
|
res.status(418).send("room already exists");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
stmt = db.prepare(`INSERT INTO Rooms (RoomName) VALUES (${name})`);
|
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();
|
stmt.run();
|
||||||
|
incrementUserSubmissions(parseInt(body.userid));
|
||||||
res.status(200).send("added room");
|
res.status(200).send("added room");
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -111,7 +122,7 @@ app.get('/getRooms', (req, res) => {
|
|||||||
// req has no data
|
// req has no data
|
||||||
// res has success or faliure
|
// res has success or faliure
|
||||||
console.log("getRooms");
|
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();
|
let records = stmt.all();
|
||||||
res.status(200).send({records: records});
|
res.status(200).send({records: records});
|
||||||
});
|
});
|
||||||
@@ -128,7 +139,7 @@ app.post('/createUser', async (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const generatedHash = await bcrypt.hash(body.pass, 10);
|
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.run();
|
||||||
stmt = db.prepare(`SELECT Id FROM Users WHERE Email='${body.email}';`);
|
stmt = db.prepare(`SELECT Id FROM Users WHERE Email='${body.email}';`);
|
||||||
const uid = stmt.get();
|
const uid = stmt.get();
|
||||||
|
|||||||
@@ -11,9 +11,10 @@ const roomName = ref<string>();
|
|||||||
async function addRoom(e: SubmitEvent) {
|
async function addRoom(e: SubmitEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
let uid = await $cookies.get("userId");
|
let uid = await $cookies.get("userId");
|
||||||
|
console.log(uid);
|
||||||
const res = await fetch("/addRoom", {
|
const res = await fetch("/addRoom", {
|
||||||
method: "POST",
|
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'})
|
headers: new Headers({'content-type': 'application/json'})
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ onBeforeMount(async () => {
|
|||||||
method: "GET"
|
method: "GET"
|
||||||
});
|
});
|
||||||
rooms.value = await res.json();
|
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) {
|
async function addTimeSlot(e: SubmitEvent) {
|
||||||
@@ -23,7 +28,7 @@ async function addTimeSlot(e: SubmitEvent) {
|
|||||||
let uid = await $cookies.get("userId");
|
let uid = await $cookies.get("userId");
|
||||||
await fetch("/addTimeSlot", {
|
await fetch("/addTimeSlot", {
|
||||||
method: "POST",
|
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'})
|
headers: new Headers({'content-type': 'application/json'})
|
||||||
});
|
});
|
||||||
emit('goHome');
|
emit('goHome');
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onBeforeMount, ref } from 'vue';
|
import { inject, onBeforeMount, ref } from 'vue';
|
||||||
|
import type { VueCookies } from 'vue-cookies';
|
||||||
|
|
||||||
|
type timeSlot_t = {Id: number, Period: number, Day: number, Room: number, RoomName: string};
|
||||||
|
|
||||||
|
const $cookies = inject<VueCookies>("$cookies") as VueCookies;
|
||||||
|
|
||||||
const rooms = ref();
|
const rooms = ref();
|
||||||
const showRooms = ref<boolean>(false);
|
const showRooms = ref<boolean>(false);
|
||||||
@@ -8,9 +13,9 @@ const weekNumber = ref<number>(1);
|
|||||||
async function getData() {
|
async function getData() {
|
||||||
showRooms.value = false;
|
showRooms.value = false;
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
if (today.getDay() == 0 || today.getDay() == 6) {
|
// if (today.getDay() == 0 || today.getDay() == 6) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
const day = today.getDay() * weekNumber.value;
|
const day = today.getDay() * weekNumber.value;
|
||||||
const res = await fetch("/currentRooms", {
|
const res = await fetch("/currentRooms", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -18,11 +23,28 @@ async function getData() {
|
|||||||
headers: new Headers({'content-type': 'application/json'})
|
headers: new Headers({'content-type': 'application/json'})
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
rooms.value = await res.json();
|
let json = await res.json();
|
||||||
|
if (json.records.length == 0) {
|
||||||
|
showRooms.value = false;
|
||||||
|
} else {
|
||||||
|
rooms.value = json;
|
||||||
showRooms.value = true;
|
showRooms.value = true;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteTimeSlot(timeSlot: timeSlot_t) {
|
||||||
|
const uid = await $cookies.get("userId");
|
||||||
|
const res = await fetch("/removeTimeSlot", {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({ Id: timeSlot.Id, userId: uid }),
|
||||||
|
headers: new Headers({ "content-type": "application/json" })
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
await getData();
|
||||||
|
} else {
|
||||||
console.log(await res.text());
|
console.log(await res.text());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
defineEmits(['addSlot']);
|
defineEmits(['addSlot']);
|
||||||
@@ -34,7 +56,8 @@ onBeforeMount(async () => {
|
|||||||
<header>
|
<header>
|
||||||
<h1>What's free in my free?</h1>
|
<h1>What's free in my free?</h1>
|
||||||
<button @click="$emit('addSlot')">Add new slot</button> <br>
|
<button @click="$emit('addSlot')">Add new slot</button> <br>
|
||||||
<select v-model="weekNumber" @change="getData">
|
<label for="weekNumber">Week Number</label>
|
||||||
|
<select v-model="weekNumber" @change="getData" id="weekNumber">
|
||||||
<option :value="1">1</option>
|
<option :value="1">1</option>
|
||||||
<option :value="2">2</option>
|
<option :value="2">2</option>
|
||||||
</select>
|
</select>
|
||||||
@@ -43,7 +66,7 @@ onBeforeMount(async () => {
|
|||||||
<h2>Free Rooms:</h2>
|
<h2>Free Rooms:</h2>
|
||||||
<p v-if="!showRooms">No free rooms right now</p>
|
<p v-if="!showRooms">No free rooms right now</p>
|
||||||
<ul v-else v-for="slot in rooms.records">
|
<ul v-else v-for="slot in rooms.records">
|
||||||
<li>Room: {{ slot.RoomName }}, in {{ slot.Period }}</li>
|
<li>Room: {{ slot.RoomName }}, in Period {{ slot.Period }}, <button type="button" @click="deleteTimeSlot(slot)">X</button></li>
|
||||||
</ul>
|
</ul>
|
||||||
</article>
|
</article>
|
||||||
</template>
|
</template>
|
||||||
Reference in New Issue
Block a user