-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an endpoint to delete assignments
- Loading branch information
Showing
5 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters