♻️ Refactor backend and add comments
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
import Prisma from "@prisma/client";
|
||||
const prisma = new Prisma.PrismaClient();
|
||||
|
||||
// Get timetable API endpoint (/api/timetable)
|
||||
// Returns timetable data for requested class if available
|
||||
export async function getTimetable(req, res) {
|
||||
if (!req.query.class) {
|
||||
res.status(400).send({
|
||||
@ -30,9 +32,13 @@ export async function getTimetable(req, res) {
|
||||
res.send(timetable.data);
|
||||
}
|
||||
|
||||
// Get substitutions API endpoint (/api/substitutions)
|
||||
// Returns all known substitutions for requested date / class
|
||||
// If no class is supplied, all substitutions are returned
|
||||
export async function getSubstitutions(req, res) {
|
||||
const requestedClass = (req.query.class || "").toLowerCase();
|
||||
var from, to, date;
|
||||
// Check if from or to date is set in request
|
||||
if (req.query.from && req.query.to) {
|
||||
from = new Date(req.query.from).setUTCHours(0, 0, 0, 0);
|
||||
to = new Date(req.query.to).setUTCHours(0, 0, 0, 0);
|
||||
@ -51,6 +57,7 @@ export async function getSubstitutions(req, res) {
|
||||
if (requestedClass) {
|
||||
prismaOptions.where.class = { has: requestedClass };
|
||||
}
|
||||
// Choose which date to use in database query
|
||||
if (from && to) {
|
||||
prismaOptions.where.date = {
|
||||
gte: new Date(from),
|
||||
@ -59,6 +66,7 @@ export async function getSubstitutions(req, res) {
|
||||
} else if (date) {
|
||||
prismaOptions.where.date = new Date(date);
|
||||
} else {
|
||||
// Default to all substitutions for today and in the future
|
||||
prismaOptions.where.date = {
|
||||
gte: new Date(new Date().setUTCHours(0, 0, 0, 0)),
|
||||
};
|
||||
@ -85,9 +93,13 @@ export async function getSubstitutions(req, res) {
|
||||
res.send(substitutions);
|
||||
}
|
||||
|
||||
// Get history API endpoint (/api/history)
|
||||
// Returns history of changes for all substituions in the date range
|
||||
// for the requested class if supplied
|
||||
export async function getHistory(req, res) {
|
||||
const requestedClass = (req.query.class || "").toLowerCase();
|
||||
var from, to, date;
|
||||
// Check if from or to date is set in request
|
||||
if (req.query.from && req.query.to) {
|
||||
from = new Date(req.query.from).setUTCHours(0, 0, 0, 0);
|
||||
to = new Date(req.query.to).setUTCHours(0, 0, 0, 0);
|
||||
@ -109,6 +121,7 @@ export async function getHistory(req, res) {
|
||||
if (requestedClass) {
|
||||
prismaOptions.where.substitution.class = { has: requestedClass };
|
||||
}
|
||||
// Choose which date to use in database query
|
||||
if (from && to) {
|
||||
prismaOptions.where.substitution.date = {
|
||||
gte: new Date(from),
|
||||
@ -117,6 +130,7 @@ export async function getHistory(req, res) {
|
||||
} else if (date) {
|
||||
prismaOptions.where.substitution.date = new Date(date);
|
||||
} else {
|
||||
// Default to history of all substitutions for today and in the future
|
||||
prismaOptions.where.substitution.date = {
|
||||
gte: new Date(new Date().setUTCHours(0, 0, 0, 0)),
|
||||
};
|
||||
@ -139,6 +153,9 @@ export async function getHistory(req, res) {
|
||||
res.send(changes);
|
||||
}
|
||||
|
||||
// Get classes API endpoints (/api/classes)
|
||||
// Get all available classes where timetable and
|
||||
// substitutions can be requested for
|
||||
export async function getClasses(req, res) {
|
||||
const classes = await prisma.class.findMany({
|
||||
select: {
|
||||
@ -149,6 +166,7 @@ export async function getClasses(req, res) {
|
||||
name: "asc",
|
||||
},
|
||||
});
|
||||
// Only return the name of the class
|
||||
const classList = classes.map((element) => element.name);
|
||||
res.send(classList);
|
||||
}
|
||||
|
Reference in New Issue
Block a user