-
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 create a new note
- Loading branch information
Showing
7 changed files
with
168 additions
and
0 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,28 @@ | ||
CREATE MIGRATION m1mzn3ehkgkjrnujdwfxjhe5optsk3cmyqkdagwev6vq2p5a4xxbsa | ||
ONTO m1xsbfzwxprtxttepfmhlwzsayxv3keyigzrfa2o2zk3g4zttajubq | ||
{ | ||
ALTER TYPE default::Calendar { | ||
ALTER LINK tags { | ||
ON TARGET DELETE ALLOW; | ||
}; | ||
}; | ||
CREATE SCALAR TYPE default::EditScope EXTENDING enum<Self, Class, School>; | ||
CREATE TYPE default::Note { | ||
CREATE REQUIRED LINK class: default::Class { | ||
SET readonly := true; | ||
}; | ||
CREATE MULTI LINK tags: default::Tag { | ||
ON TARGET DELETE ALLOW; | ||
}; | ||
CREATE MULTI LINK updates: default::Change { | ||
ON SOURCE DELETE DELETE TARGET; | ||
ON TARGET DELETE ALLOW; | ||
}; | ||
CREATE PROPERTY editScope: default::EditScope { | ||
SET default := (default::EditScope.Self); | ||
}; | ||
CREATE PROPERTY priority: default::Priority; | ||
CREATE PROPERTY summary: std::str; | ||
CREATE REQUIRED PROPERTY title: std::str; | ||
}; | ||
}; |
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,83 @@ | ||
import e from "@edgedb"; | ||
import { DATABASE_WRITE_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 { fallback } from "utils/arrays/fallback"; | ||
import { promiseResult } from "utils/errors"; | ||
import { usersClassBySchoolAndName } from "utils/queries/class"; | ||
import { userByUsername } from "utils/queries/user"; | ||
import { responseBuilder } from "utils/response"; | ||
|
||
export const createNote = new Elysia() | ||
.use(auth) | ||
.use(HttpStatusCode()) | ||
.post( | ||
"/", | ||
async ({ set, httpStatus, auth, body }) => { | ||
if (!auth.isAuthorized) { | ||
set.status = httpStatus.HTTP_401_UNAUTHORIZED; | ||
return UNAUTHORIZED; | ||
} | ||
|
||
const query = e.insert(e.Note, { | ||
title: body.title, | ||
class: usersClassBySchoolAndName({ | ||
schoolName: body.school, | ||
className: body.class, | ||
username: auth.username, | ||
}), | ||
|
||
summary: body.summary, | ||
priority: body.priority, | ||
tags: e.select(e.Tag, (t) => ({ | ||
filter: e.op( | ||
e.op( | ||
e.op(t.class.name, "=", body.class), | ||
"and", | ||
e.op(t.class.school.name, "=", body.school), | ||
), | ||
"and", | ||
e.op(t.tag, "in", e.set(...fallback(body.tags || [], [""]))), | ||
), | ||
})), | ||
editScope: body.editScope, | ||
|
||
updates: e.insert(e.Change, { user: userByUsername(auth.username) }), | ||
}); | ||
const result = await promiseResult(() => query.run(client)); | ||
|
||
if (result.isError) { | ||
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR; | ||
return DATABASE_WRITE_FAILED; | ||
} | ||
|
||
set.status = httpStatus.HTTP_201_CREATED; | ||
return responseBuilder("success", { | ||
message: "Successfully created note", | ||
data: null, | ||
}); | ||
}, | ||
{ | ||
body: t.Object({ | ||
class: t.String({ minLength: 1 }), | ||
school: t.String({ minLength: 1 }), | ||
title: t.String({ minLength: 1 }), | ||
summary: t.Optional(t.String({ minLength: 1 })), | ||
tags: t.Optional(t.Array(t.String({ minLength: 1 }))), | ||
priority: t.Optional( | ||
t.Union([ | ||
t.Literal("Critical"), | ||
t.Literal("High"), | ||
t.Literal("Medium"), | ||
t.Literal("Low"), | ||
t.Literal("Minimal"), | ||
]), | ||
), | ||
editScope: t.Optional( | ||
t.Union([t.Literal("Self"), t.Literal("Class"), t.Literal("School")]), | ||
), | ||
}), | ||
}, | ||
); |
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,4 @@ | ||
import Elysia from "elysia"; | ||
import { createNote } from "./create"; | ||
|
||
export const noteRouter = new Elysia({ prefix: "/notes" }).use(createNote); |
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,6 @@ | ||
import e from "@edgedb"; | ||
|
||
export const userByUsername = (username: string) => | ||
e.select(e.User, (u) => ({ | ||
filter_single: e.op(u.username, "=", username), | ||
})); |