From 5d8757d486a053c7cfe426795db1c5e0d2eb8612 Mon Sep 17 00:00:00 2001 From: Dlurak <84224239+Dlurak@users.noreply.github.com> Date: Tue, 9 Apr 2024 11:35:39 +0200 Subject: [PATCH] Add an endpoint to delete assignments --- dbschema/default.esdl | 1 + dbschema/migrations/00016.edgeql | 9 +++++++ src/routes/assignments/delete.ts | 44 ++++++++++++++++++++++++++++++++ src/routes/assignments/index.ts | 4 ++- src/routes/assignments/list.ts | 1 + 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 dbschema/migrations/00016.edgeql create mode 100644 src/routes/assignments/delete.ts diff --git a/dbschema/default.esdl b/dbschema/default.esdl index e03c218..e7a0395 100644 --- a/dbschema/default.esdl +++ b/dbschema/default.esdl @@ -104,6 +104,7 @@ module default { multi updates: Change { on target delete allow; + on source delete delete target; }; multi completedBy: User { diff --git a/dbschema/migrations/00016.edgeql b/dbschema/migrations/00016.edgeql new file mode 100644 index 0000000..dc22d0b --- /dev/null +++ b/dbschema/migrations/00016.edgeql @@ -0,0 +1,9 @@ +CREATE MIGRATION m1k5kk6wjhcyjkqblwsla7tml2hbmfohxsdafccqr45wapyvwfwhva + ONTO m1hmgl6ihppy3vtahtblnbmpvhbrn7vtjhloj5f7f2e6qgwsn7eqgq +{ + ALTER TYPE default::Assignment { + ALTER LINK updates { + ON SOURCE DELETE DELETE TARGET; + }; + }; +}; diff --git a/src/routes/assignments/delete.ts b/src/routes/assignments/delete.ts new file mode 100644 index 0000000..e278708 --- /dev/null +++ b/src/routes/assignments/delete.ts @@ -0,0 +1,44 @@ +import e from "@edgedb"; +import { DATABASE_READ_FAILED, UNAUTHORIZED } from "constants/responses"; +import Elysia from "elysia"; +import { HttpStatusCode } from "elysia-http-status-code"; +import { client } from "index"; +import { auth } from "plugins/auth"; +import { promiseResult } from "utils/errors"; +import { responseBuilder } from "utils/response"; + +export const deleteAssignment = new Elysia() + .use(HttpStatusCode()) + .use(auth) + .delete("/:id", async ({ auth, set, httpStatus, params }) => { + if (!auth.isAuthorized) { + set.status = httpStatus.HTTP_401_UNAUTHORIZED; + return UNAUTHORIZED; + } + + const query = e.delete(e.Assignment, (a) => ({ + filter_single: e.op( + e.op(a.id, "=", e.cast(e.uuid, params.id)), + "and", + e.op(auth.username, "in", a.class.students.username), + ), + })); + const result = await promiseResult(() => query.run(client)); + + if (result.isError) { + set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR; + return DATABASE_READ_FAILED; + } + if (!result.data) { + // Either the user isn't in the class and/or the homework doesn't exist + set.status = httpStatus.HTTP_404_NOT_FOUND; + return responseBuilder("error", { + error: "Homework not found in any of your classes", + }); + } + + return responseBuilder("success", { + message: "Successfully deleted assignment", + data: null, + }); + }); diff --git a/src/routes/assignments/index.ts b/src/routes/assignments/index.ts index 33ae272..9d9aae3 100644 --- a/src/routes/assignments/index.ts +++ b/src/routes/assignments/index.ts @@ -1,7 +1,9 @@ import Elysia from "elysia"; import { createAssignment } from "./create"; +import { deleteAssignment } from "./delete"; import { listAssignments } from "./list"; export const assignmentsRouter = new Elysia({ prefix: "/assignments" }) .use(listAssignments) - .use(createAssignment); + .use(createAssignment) + .use(deleteAssignment); diff --git a/src/routes/assignments/list.ts b/src/routes/assignments/list.ts index e88729d..da8be01 100644 --- a/src/routes/assignments/list.ts +++ b/src/routes/assignments/list.ts @@ -169,6 +169,7 @@ export const listAssignments = new Elysia().use(HttpStatusCode()).get( user: upd.user, time: upd.time.getTime(), })), + id: assignment.id, })); return responseBuilder("success", {