From 66bdccfeebd7da58ece95d5ef960041f71d14c30 Mon Sep 17 00:00:00 2001 From: Dlurak <84224239+Dlurak@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:16:02 +0200 Subject: [PATCH] Add an endpoint to delete tags --- dbschema/default.esdl | 4 +++- src/routes/tags/delete.ts | 43 +++++++++++++++++++++++++++++++++++++++ src/routes/tags/index.ts | 2 ++ src/routes/tags/list.ts | 1 + 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/routes/tags/delete.ts diff --git a/dbschema/default.esdl b/dbschema/default.esdl index f4663ff..e4b8802 100644 --- a/dbschema/default.esdl +++ b/dbschema/default.esdl @@ -135,7 +135,9 @@ module default { location: str; - multi tags: Tag; + multi tags: Tag { + on target delete allow; + }; priority: Priority; multi updates: Change { diff --git a/src/routes/tags/delete.ts b/src/routes/tags/delete.ts new file mode 100644 index 0000000..695356c --- /dev/null +++ b/src/routes/tags/delete.ts @@ -0,0 +1,43 @@ +import e from "@edgedb"; +import { DATABASE_DELETE_FAILED, UNAUTHORIZED } from "constants/responses"; +import Elysia, { t } 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 deleteTag = new Elysia() + .use(auth) + .use(HttpStatusCode()) + .delete("/:id", async ({ set, httpStatus, auth, params }) => { + if (!auth.isAuthorized) { + set.status = httpStatus.HTTP_401_UNAUTHORIZED; + return UNAUTHORIZED; + } + + const query = e.delete(e.Tag, (t) => ({ + filter_single: e.op( + e.op(t.id, "=", e.cast(e.uuid, params.id)), + "and", + e.op(auth.username, "in", t.class.students.username), + ), + })); + const result = await promiseResult(() => query.run(client)); + + if (result.isError) { + set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR; + return DATABASE_DELETE_FAILED; + } + if (!result.data) { + set.status = httpStatus.HTTP_404_NOT_FOUND; + return responseBuilder("error", { + error: "This tag doesn't exist or you don't have rights to delete it", + }); + } + + return responseBuilder("success", { + data: null, + message: "Successfully deleted tag", + }); + }); diff --git a/src/routes/tags/index.ts b/src/routes/tags/index.ts index cf7884a..47ecfff 100644 --- a/src/routes/tags/index.ts +++ b/src/routes/tags/index.ts @@ -1,7 +1,9 @@ import Elysia from "elysia"; import { createTag } from "./create"; +import { deleteTag } from "./delete"; import { listTags } from "./list"; export const tagRouter = new Elysia({ prefix: "/tags" }) .use(createTag) + .use(deleteTag) .use(listTags); diff --git a/src/routes/tags/list.ts b/src/routes/tags/list.ts index dc87f53..19aab08 100644 --- a/src/routes/tags/list.ts +++ b/src/routes/tags/list.ts @@ -28,6 +28,7 @@ export const listTags = new Elysia().use(HttpStatusCode()).get( tag: true, color: true, + id: true, }; });