From cd41e4bf2887628bcdfa848360f8d787ad1bccbe Mon Sep 17 00:00:00 2001 From: Dlurak <84224239+Dlurak@users.noreply.github.com> Date: Fri, 29 Mar 2024 21:07:47 +0100 Subject: [PATCH] Create an endpoint to add new schools --- dbschema/default.esdl | 35 +++++++++++++ dbschema/migrations/00006.edgeql | 33 +++++++++++++ dbschema/migrations/00007.edgeql | 7 +++ src/constants/documentation.ts | 28 +++++++++++ src/constants/responses.ts | 20 ++++++++ src/index.ts | 28 ++--------- src/routes/school/index.ts | 84 ++++++++++++++++++++++++++++++++ src/routes/user/me.ts | 29 ++++++----- 8 files changed, 228 insertions(+), 36 deletions(-) create mode 100644 dbschema/migrations/00006.edgeql create mode 100644 dbschema/migrations/00007.edgeql create mode 100644 src/constants/documentation.ts create mode 100644 src/routes/school/index.ts diff --git a/dbschema/default.esdl b/dbschema/default.esdl index 974c709..73e335e 100644 --- a/dbschema/default.esdl +++ b/dbschema/default.esdl @@ -20,6 +20,8 @@ module default { default := datetime_current(); readonly := true; }; + + classes := . ({ diff --git a/src/routes/school/index.ts b/src/routes/school/index.ts new file mode 100644 index 0000000..18fe5c4 --- /dev/null +++ b/src/routes/school/index.ts @@ -0,0 +1,84 @@ +import e from "@edgedb"; +import { + DATABASE_READ_FAILED, + 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 { promiseResult } from "utils/errors"; +import { responseBuilder } from "utils/response"; + +export const schoolRouter = new Elysia({ prefix: "/school" }) + .use(auth) + .use(HttpStatusCode()) + .post( + "/", + async ({ auth, body, set, httpStatus }) => { + if (!auth.isAuthorized) { + set.status = httpStatus.HTTP_401_UNAUTHORIZED; + return UNAUTHORIZED; + } + + const countQuery = e.count( + e.select(e.School, (s) => ({ + filter: e.op(s.name, "=", body.name), + })), + ); + const count = await promiseResult(() => countQuery.run(client)); + + if (count.status === "error") { + set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR; + return DATABASE_READ_FAILED; + } + if (count.data >= 1) { + set.status = httpStatus.HTTP_400_BAD_REQUEST; + return responseBuilder("error", { + error: `A school with the name ${body.name} already exists`, + }); + } + + const createSchoolQuery = e.insert(e.School, { + name: body.name, + description: body.description, + }); + const result = await promiseResult(() => createSchoolQuery.run(client)); + + if (result.status === "error") { + set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR; + return DATABASE_WRITE_FAILED; + } + + set.status = httpStatus.HTTP_201_CREATED; + return responseBuilder("success", { + message: `Successfully created school ${body.name}`, + data: null, + }); + }, + { + body: t.Object({ + name: t.String({ + minLength: 1, + description: "The name of the school", + examples: ["Dlool High School", "Humboldt Gymnasium", "Hogwarts"], + }), + description: t.String({ + minLength: 1, + description: + "A description of the school, this is only so other users can distinguish between schools with similar names", + examples: [ + "The Dlool High School in Chicago", + "Das Humboldt Gymnasium in Köln", + "Hogwarts straight out of the Harry Potter books", + ], + }), + }), + detail: { + description: + "Create a new school, this requires the user to be authenthicated", + tags: ["school"], + }, + }, + ); diff --git a/src/routes/user/me.ts b/src/routes/user/me.ts index bb33436..a110c3b 100644 --- a/src/routes/user/me.ts +++ b/src/routes/user/me.ts @@ -6,17 +6,22 @@ import { auth } from "plugins/auth"; export const userOwnInfoRouter = new Elysia({ prefix: "/me" }) .use(auth) .use(HttpStatusCode()) - .get("/", ({ auth, set, httpStatus }) => { - if (!auth.isAuthorized) { - set.status = httpStatus.HTTP_401_UNAUTHORIZED; - return UNAUTHORIZED; - } + .get( + "/", + ({ auth, set, httpStatus }) => { + if (!auth.isAuthorized) { + set.status = httpStatus.HTTP_401_UNAUTHORIZED; + return UNAUTHORIZED; + } - // An authorized request to that endpoint will give a lot of info - set.redirect = `/user/info?username=${auth.username}`; - }, { - detail: { - description: "Get information about yourself. For this the user needs to authenthicated", - tags: ["User"], + // An authorized request to that endpoint will give a lot of info + set.redirect = `/user/info?username=${auth.username}`; }, -}); + { + detail: { + description: + "Get information about yourself. For this the user needs to authenthicated", + tags: ["User"], + }, + }, + );