✨ Add timetable, substitutions and history API
This commit is contained in:
129
server/api/index.js
Normal file
129
server/api/index.js
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
import Prisma from "@prisma/client";
|
||||||
|
const prisma = new Prisma.PrismaClient();
|
||||||
|
|
||||||
|
export async function getTimetable(req, res) {
|
||||||
|
if (!req.query.class) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: "missing_parameter",
|
||||||
|
message: "No class parameter provided",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const requestedClass = req.query.class.toLowerCase();
|
||||||
|
const timetable = await prisma.timetable.findFirst({
|
||||||
|
where: {
|
||||||
|
class: requestedClass,
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
updatedAt: "desc",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!timetable) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: "no_timetable",
|
||||||
|
message: "No timetable was found for this class",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.send(timetable.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getSubstitutions(req, res) {
|
||||||
|
const requestedClass = (req.query.class || "").toLowerCase();
|
||||||
|
var from, to, date;
|
||||||
|
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);
|
||||||
|
} else {
|
||||||
|
date = new Date(req.query.date || new Date()).setUTCHours(0, 0, 0, 0);
|
||||||
|
console.log(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
const prismaOptions = {
|
||||||
|
where: {
|
||||||
|
removed: false,
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
lesson: "asc",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (requestedClass) {
|
||||||
|
prismaOptions.where.class = { has: requestedClass };
|
||||||
|
}
|
||||||
|
if (from && to) {
|
||||||
|
prismaOptions.where.date = {
|
||||||
|
gte: new Date(from),
|
||||||
|
lte: new Date(to),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
prismaOptions.where.date = new Date(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
const rawSubstitutions = await prisma.substitution.findMany(prismaOptions);
|
||||||
|
const substitutions = rawSubstitutions.map((element) => {
|
||||||
|
const substitution = {
|
||||||
|
id: element.id,
|
||||||
|
class: element.class,
|
||||||
|
type: element.type,
|
||||||
|
lesson: element.lesson,
|
||||||
|
date: new Date(element.date).getTime(),
|
||||||
|
change: {},
|
||||||
|
};
|
||||||
|
if (element.changedRoom) substitution.change.room = element.changedRoom;
|
||||||
|
if (element.changedTeacher)
|
||||||
|
substitution.change.teacher = element.changedTeacher;
|
||||||
|
if (element.changedSubject)
|
||||||
|
substitution.change.subject = element.changedSubject;
|
||||||
|
return substitution;
|
||||||
|
});
|
||||||
|
res.send(substitutions);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getHistory(req, res) {
|
||||||
|
const requestedClass = (req.query.class || "").toLowerCase();
|
||||||
|
var from, to, date;
|
||||||
|
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);
|
||||||
|
} else {
|
||||||
|
date = new Date(req.query.date || new Date()).setUTCHours(0, 0, 0, 0);
|
||||||
|
console.log(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
const prismaOptions = {
|
||||||
|
where: {},
|
||||||
|
include: {
|
||||||
|
substitution: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (requestedClass) {
|
||||||
|
prismaOptions.where.class = { has: requestedClass };
|
||||||
|
}
|
||||||
|
if (from && to) {
|
||||||
|
prismaOptions.where.substitution = {
|
||||||
|
date: {
|
||||||
|
gte: new Date(from),
|
||||||
|
lte: new Date(to),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
prismaOptions.where.substitution = { date: new Date(date) };
|
||||||
|
}
|
||||||
|
|
||||||
|
const rawChanges = await prisma.substitutionChange.findMany(prismaOptions);
|
||||||
|
const changes = rawChanges.map((element) => {
|
||||||
|
return {
|
||||||
|
id: element.id,
|
||||||
|
type: element.type,
|
||||||
|
substitutionId: element.substitutionId,
|
||||||
|
lesson: element.substitution.lesson,
|
||||||
|
updatedAt: new Date(element.createdAt).getTime(),
|
||||||
|
date: new Date(element.substitution.date).getTime(),
|
||||||
|
change: element.changes,
|
||||||
|
parseEventId: element.parseEventId,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
res.send(changes);
|
||||||
|
}
|
@ -1,15 +1,19 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import Prisma from "@prisma/client";
|
import { getTimetable, getSubstitutions, getHistory } from "./api/index.js";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const prisma = new Prisma.PrismaClient();
|
|
||||||
const port = process.send.PORT || 3000;
|
const port = process.send.PORT || 3000;
|
||||||
|
|
||||||
app.get("/", async (_req, res) => {
|
app.get("/api/timetable", getTimetable);
|
||||||
const result = await prisma.substitution.findMany();
|
app.get("/api/substitutions", getSubstitutions);
|
||||||
res.send(result);
|
app.get("/api/history", getHistory);
|
||||||
|
app.get("/api/*", (req, res) => {
|
||||||
|
res.sendStatus(400);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.use("/", express.static("../dist"));
|
||||||
|
app.use("/*", express.static("../dist"));
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Server listening on http://localhost:${port}`);
|
console.log(`Server listening on http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user