Skip to content

Commit

Permalink
Add an endpoint to update tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Apr 10, 2024
1 parent 66bdccf commit e68e602
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/constants/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ export const DATE = /^\d{4}(-\d{2}(-\d{2})?)?$/;
*/
export const PASSWORD =
/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$ %^&*-]).{8,}$/;

/**
* A regular expression to match a hex color
* - Only lowercase
* - 3 or 6 digits
* - Starting with a #
*/
export const COLOR = /^#[0-9a-f]{3}([0-9a-f]{3})?$/;
3 changes: 2 additions & 1 deletion src/routes/tags/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import e from "@edgedb";
import { COLOR } from "constants/regex";
import {
DATABASE_READ_FAILED,
DATABASE_WRITE_FAILED,
Expand Down Expand Up @@ -88,7 +89,7 @@ export const createTag = new Elysia()
body: t.Object({
tag: t.String({ minLength: 1 }),
class: t.String({ minLength: 1 }),
color: t.Optional(t.RegExp(/^#[0-9a-f]{3}([0-9a-f]{3})?$/)),
color: t.Optional(t.RegExp(COLOR)),
}),
},
);
6 changes: 4 additions & 2 deletions src/routes/tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import Elysia from "elysia";
import { createTag } from "./create";
import { deleteTag } from "./delete";
import { listTags } from "./list";
import { updateTag } from "./update";

export const tagRouter = new Elysia({ prefix: "/tags" })
.use(createTag)
.use(deleteTag)
.use(listTags);
.use(listTags)
.use(updateTag)
.use(deleteTag);
57 changes: 57 additions & 0 deletions src/routes/tags/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import e from "@edgedb";
import { COLOR } from "constants/regex";
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 { promiseResult } from "utils/errors";
import { responseBuilder } from "utils/response";

export const updateTag = new Elysia()
.use(auth)
.use(HttpStatusCode())
.patch(
"/:id",
async ({ set, httpStatus, auth, params, body }) => {
if (!auth.isAuthorized) {
set.status = httpStatus.HTTP_401_UNAUTHORIZED;
return UNAUTHORIZED;
}

const query = e.update(e.Tag, (t) => ({
filter_single: e.op(
e.op(t.id, "=", e.uuid(params.id)),
"and",
e.op(auth.username, "in", t.class.students.username),
),
set: {
tag: body.tag ?? t.tag,
color: body.color ?? t.color,
},
}));

const result = await promiseResult(() => query.run(client));
if (result.isError) {
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR;
return DATABASE_WRITE_FAILED;
}
if (!result.data) {
set.status = httpStatus.HTTP_404_NOT_FOUND;
return responseBuilder("error", {
error: "Tag doesn't exist or you don't have rights to edit it",
});
}

return responseBuilder("success", {
data: null,
message: "Successfully updated tag",
});
},
{
body: t.Object({
tag: t.Optional(t.String({ minLength: 1 })),
color: t.Optional(t.RegExp(COLOR)),
}),
},
);

0 comments on commit e68e602

Please sign in to comment.