Skip to content

Commit

Permalink
Add an endpoint to delete tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Apr 10, 2024
1 parent 86e8f9d commit 66bdccf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dbschema/default.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ module default {
location: str;


multi tags: Tag;
multi tags: Tag {
on target delete allow;
};
priority: Priority;

multi updates: Change {
Expand Down
43 changes: 43 additions & 0 deletions src/routes/tags/delete.ts
Original file line number Diff line number Diff line change
@@ -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",
});
});
2 changes: 2 additions & 0 deletions src/routes/tags/index.ts
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions src/routes/tags/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const listTags = new Elysia().use(HttpStatusCode()).get(

tag: true,
color: true,
id: true,
};
});

Expand Down

0 comments on commit 66bdccf

Please sign in to comment.