Skip to content

Commit

Permalink
Add an endpoint to delete assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Apr 9, 2024
1 parent ef61e3f commit 5d8757d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions dbschema/default.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ module default {

multi updates: Change {
on target delete allow;
on source delete delete target;
};

multi completedBy: User {
Expand Down
9 changes: 9 additions & 0 deletions dbschema/migrations/00016.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE MIGRATION m1k5kk6wjhcyjkqblwsla7tml2hbmfohxsdafccqr45wapyvwfwhva
ONTO m1hmgl6ihppy3vtahtblnbmpvhbrn7vtjhloj5f7f2e6qgwsn7eqgq
{
ALTER TYPE default::Assignment {
ALTER LINK updates {
ON SOURCE DELETE DELETE TARGET;
};
};
};
44 changes: 44 additions & 0 deletions src/routes/assignments/delete.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});
4 changes: 3 additions & 1 deletion src/routes/assignments/index.ts
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions src/routes/assignments/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 5d8757d

Please sign in to comment.