-
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.
Implement an endpoint to delete notes
- Loading branch information
Showing
6 changed files
with
91 additions
and
4 deletions.
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,12 @@ | ||
CREATE MIGRATION m17fqphiq6jr5unbqqolhaopjqdcgklmjowirlpgkejf2qsqocypsq | ||
ONTO m1mzn3ehkgkjrnujdwfxjhe5optsk3cmyqkdagwev6vq2p5a4xxbsa | ||
{ | ||
ALTER TYPE default::Note { | ||
CREATE LINK creator: default::User { | ||
ON TARGET DELETE ALLOW; | ||
}; | ||
ALTER PROPERTY editScope { | ||
SET REQUIRED USING (default::EditScope.Self); | ||
}; | ||
}; | ||
}; |
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,64 @@ | ||
import e from "@edgedb"; | ||
import { DATABASE_DELETE_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 deleteNote = new Elysia() | ||
.use(auth) | ||
.use(HttpStatusCode()) | ||
.delete("/:id", async ({ auth, set, httpStatus, params }) => { | ||
if (!auth.isAuthorized) { | ||
set.status = httpStatus.HTTP_401_UNAUTHORIZED; | ||
return UNAUTHORIZED; | ||
} | ||
|
||
const deletionQuery = e.delete(e.Note, (n) => { | ||
const selfFilter = e.op( | ||
e.op(n.editScope, "=", e.cast(e.EditScope, "Self")), | ||
"and", | ||
e.op(n.creator.username, "=", auth.username), | ||
); | ||
const classFilter = e.op( | ||
e.op(n.editScope, "=", e.cast(e.EditScope, "Class")), | ||
"and", | ||
e.op(auth.username, "in", n.class.students.username), | ||
); | ||
const schoolFilter = e.op( | ||
e.op(n.editScope, "=", e.cast(e.EditScope, "School")), | ||
"and", | ||
e.op(auth.username, "in", n.class.school.classes.students.username), | ||
); | ||
|
||
const editScopeFilter = e.op( | ||
e.op(selfFilter, "or", classFilter), | ||
"or", | ||
schoolFilter, | ||
); | ||
|
||
const paramId = e.cast(e.uuid, params.id); | ||
const idFilter = e.op(n.id, "=", paramId); | ||
|
||
return { filter_single: e.op(editScopeFilter, "and", idFilter) }; | ||
}); | ||
const result = await promiseResult(() => deletionQuery.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 note doesn't exist or you don't have rights to delete it", | ||
}); | ||
} | ||
|
||
return responseBuilder("success", { | ||
message: "Successfully deleted note", | ||
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 { createNote } from "./create"; | ||
import { deleteNote } from "./delete"; | ||
import { listNotes } from "./list"; | ||
|
||
export const noteRouter = new Elysia({ prefix: "/notes" }) | ||
.use(createNote) | ||
.use(deleteNote) | ||
.use(listNotes); |
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