-
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.
Create an endpoint to add new schools
- Loading branch information
Showing
8 changed files
with
228 additions
and
36 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,33 @@ | ||
CREATE MIGRATION m14j4szqlr74t67qbfcb55ukpjenf6n7bsg444albnz6y3ca3m7nsq | ||
ONTO m12drjaftq6ko7ez5n75r6gmuid4nzrxufl7pnyv5cx52zaglhqt7q | ||
{ | ||
CREATE TYPE default::Class { | ||
CREATE MULTI LINK students: default::User { | ||
CREATE PROPERTY joinedAt: std::datetime { | ||
SET default := (std::datetime_current()); | ||
SET readonly := true; | ||
}; | ||
}; | ||
CREATE REQUIRED PROPERTY created: std::datetime { | ||
SET default := (std::datetime_current()); | ||
SET readonly := true; | ||
}; | ||
CREATE REQUIRED PROPERTY name: std::str; | ||
}; | ||
CREATE TYPE default::School { | ||
CREATE MULTI LINK classes: default::Class { | ||
CREATE CONSTRAINT std::exclusive; | ||
}; | ||
CREATE REQUIRED PROPERTY created: std::datetime { | ||
SET default := (std::datetime_current()); | ||
SET readonly := true; | ||
}; | ||
CREATE REQUIRED PROPERTY description: std::str; | ||
CREATE REQUIRED PROPERTY name: std::str { | ||
CREATE CONSTRAINT std::exclusive; | ||
}; | ||
}; | ||
ALTER TYPE default::Class { | ||
CREATE SINGLE LINK school := (.<classes[IS default::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,7 @@ | ||
CREATE MIGRATION m1psgxwipft63xhdvv24e4bboazf26v6doqseu3nyzyoa3oecmjp4q | ||
ONTO m14j4szqlr74t67qbfcb55ukpjenf6n7bsg444albnz6y3ca3m7nsq | ||
{ | ||
ALTER TYPE default::User { | ||
CREATE LINK classes := (.<students[IS default::Class]); | ||
}; | ||
}; |
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 @@ | ||
import { VERSION } from "./general"; | ||
|
||
/** | ||
* The options for the swagger plugin | ||
* It can't be a standalone module, but this is possible | ||
*/ | ||
export const DOCUMENTATION_OPTIONS = { | ||
documentation: { | ||
info: { | ||
title: "Dlool API", | ||
license: { | ||
name: "GPL-3.0", | ||
url: "https://www.gnu.org/licenses/gpl-3.0.html", | ||
}, | ||
version: VERSION, | ||
}, | ||
externalDocs: { | ||
description: | ||
"The Dlool documentation for general information and usage of the frontend.", | ||
url: "https://dlool.me/documentation", | ||
}, | ||
tags: [ | ||
{ name: "App", description: "General app information" }, | ||
{ name: "Auth", description: "Authentication endpoints" }, | ||
{ name: "User", description: "User information endpoints" }, | ||
], | ||
}, | ||
}; |
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,5 +1,25 @@ | ||
import { responseBuilder } from "utils/response"; | ||
|
||
/** | ||
* A response indicating that the user is not authorized to access the resource | ||
* Use with `401 Unauthorized` | ||
*/ | ||
export const UNAUTHORIZED = responseBuilder("error", { | ||
error: "Unauthorized", | ||
}); | ||
|
||
/** | ||
* A response indicating that an error occurred while writing to the database | ||
* Use with `500 Internal Server Error` | ||
*/ | ||
export const DATABASE_WRITE_FAILED = responseBuilder("error", { | ||
error: "An error occurred while writing to the database", | ||
}); | ||
|
||
/** | ||
* A response indicating that an error occurred while reading from the database | ||
* Use with `500 Internal Server Error` | ||
*/ | ||
export const DATABASE_READ_FAILED = responseBuilder("error", { | ||
error: "An error occurred while reading from the database", | ||
}); |
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,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"], | ||
}, | ||
}, | ||
); |
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