Skip to content

Commit

Permalink
Overall updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed May 27, 2024
1 parent 3641bac commit b35f1a6
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 124 deletions.
8 changes: 4 additions & 4 deletions src/routes/assignments/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export const listAssignments = new Elysia().use(HttpStatusCode()).get(
user: () => ({ username: true, displayname: true }),
time: true,
}),
class: () => ({
name: true,
}),
id: true,
};
});
Expand All @@ -149,7 +152,6 @@ export const listAssignments = new Elysia().use(HttpStatusCode()).get(
});

if (result.isError) {
console.log(result.error);
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR;
return DATABASE_READ_FAILED;
}
Expand All @@ -162,15 +164,13 @@ export const listAssignments = new Elysia().use(HttpStatusCode()).get(
}

const formatted = result.data.assignments.map((assignment) => ({
subject: assignment.subject,
description: assignment.description,
...assignment,
from: normalDateToCustom(assignment.fromDate),
due: normalDateToCustom(assignment.dueDate),
updates: assignment.updates.map((upd) => ({
user: upd.user,
time: upd.time.getTime(),
})),
id: assignment.id,
}));

return responseBuilder("success", {
Expand Down
59 changes: 59 additions & 0 deletions src/routes/calendar/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import e from "@edgedb";
import { DATABASE_READ_FAILED } from "constants/responses";
import Elysia from "elysia";
import { HttpStatusCode } from "elysia-http-status-code";
import { client } from "index";
import { normalDateToCustomDateTime } from "utils/dates/customAndNormal";
import { promiseResult } from "utils/errors";
import { replaceDateDeep, replaceDateWithTimestamp } from "utils/objects/transform";
import { responseBuilder } from "utils/response";

export const specificCalendar = new Elysia()
.use(HttpStatusCode())
.get("/:id", async ({ params, set, httpStatus }) => {
const query = e.select(e.Calendar, (c) => ({
filter_single: e.op(c.id, "=", e.uuid(params.id)),

title: true,
beginning: true,
ending: true,
location: true,
priority: true,
summary: true,
class: () => ({
name: true,
school: () => ({ name: true })
}),
updates: () => ({
user: () => ({ username: true, displayname: true }),
time: true,
}),
tags: () => ({
tag: true,
color: true,
}),
id: true,
}));

const result = await promiseResult(async () => query.run(client));

if (result.isError) {
set.status = httpStatus.HTTP_400_BAD_REQUEST;
return DATABASE_READ_FAILED;
}

if (!result.data) {
set.status = httpStatus.HTTP_404_NOT_FOUND;
return responseBuilder("error", {
error: "Calendar event not found",
});
}

return responseBuilder("success", {
message: "Successfully retrieved calendar event",
data: {
...replaceDateDeep(result.data, normalDateToCustomDateTime),
updates: result.data.updates.map(replaceDateWithTimestamp),
}
});
})
8 changes: 5 additions & 3 deletions src/routes/calendar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { createCalendar } from "./create";
import { deleteCalendar } from "./delete";
import { listCalendar } from "./list";
import { updateCalendar } from "./update";
import { specificCalendar } from "routes/calendar/id";

export const calendarRouter = new Elysia({ prefix: "/calendar" })
.use(createCalendar)
.use(listCalendar)
.use(deleteCalendar)
.use(updateCalendar);
.use(specificCalendar)
.use(createCalendar)
.use(updateCalendar)
.use(deleteCalendar);
9 changes: 7 additions & 2 deletions src/routes/calendar/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { stringArraySchema } from "schemas/stringArray";
import { removeDuplicates } from "utils/arrays/duplicates";
import { filterTruthy } from "utils/arrays/filter";
import {
normalDateToCustom,
normalDateToCustomDateTime,
stringToNormal,
} from "utils/dates/customAndNormal";
import { strToDir } from "utils/db/direction";
Expand Down Expand Up @@ -95,6 +95,11 @@ export const listCalendar = new Elysia().use(HttpStatusCode()).get(
ending: true,
location: true,
priority: true,
summary: true,
class: () => ({
name: true,
school: () => ({ name: true })
}),
updates: () => ({
user: () => ({ username: true, displayname: true }),
time: true,
Expand Down Expand Up @@ -124,7 +129,7 @@ export const listCalendar = new Elysia().use(HttpStatusCode()).get(
}

const formatted = result.data.calendar.map((c) => ({
...replaceDateDeep(c, normalDateToCustom),
...replaceDateDeep(c, normalDateToCustomDateTime),
updates: c.updates.map((u) => replaceDateDeep(u, (d) => d.getTime())),
}));

Expand Down
6 changes: 3 additions & 3 deletions src/routes/notes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const createNote = new Elysia()
username: auth.username,
}),

summary: body.summary,
summary: body.summary || null,
priority: body.priority,
tags: e.select(e.Tag, (t) => ({
filter: e.op(
Expand Down Expand Up @@ -57,15 +57,15 @@ export const createNote = new Elysia()
set.status = httpStatus.HTTP_201_CREATED;
return responseBuilder("success", {
message: "Successfully created note",
data: null,
data: result.data
});
},
{
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 })),
summary: t.Optional(t.String()),
tags: t.Optional(t.Array(t.String({ minLength: 1 }))),
priority: t.Optional(
t.Union([
Expand Down
50 changes: 50 additions & 0 deletions src/routes/notes/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import e from "@edgedb";
import { DATABASE_READ_FAILED } from "constants/responses";
import Elysia from "elysia";
import { HttpStatusCode } from "elysia-http-status-code";
import { client } from "index";
import { promiseResult } from "utils/errors";
import { replaceDateWithTimestamp } from "utils/objects/transform";
import { responseBuilder } from "utils/response";

export const specificNote = new Elysia()
.use(HttpStatusCode())
.get("/:id", async ({ params, set, httpStatus }) => {
const query = e.select(e.Note, (n) => ({
filter_single: e.op(n.id, "=", e.uuid(params.id)),

title: true,
summary: true,
tags: () => ({
tag: true,
color: true,
}),
class: () => ({ name: true, school: () => ({ name: true }) }),
creator: () => ({ username: true }),
editScope: true,
priority: true,
updates: () => ({
user: () => ({ username: true, displayname: true }),
time: true,
}),
}));

const result = await promiseResult(async () => query.run(client));

if (result.isError) {
set.status = httpStatus.HTTP_400_BAD_REQUEST;
return DATABASE_READ_FAILED;
}

if (!result.data) {
set.status = httpStatus.HTTP_404_NOT_FOUND;
return responseBuilder("error", {
error: "Note not found",
});
}

return responseBuilder("success", {
message: "Successfully retrieved note",
data: replaceDateWithTimestamp(result.data),
});
});
4 changes: 3 additions & 1 deletion src/routes/notes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { createNote } from "./create";
import { deleteNote } from "./delete";
import { listNotes } from "./list";
import { updateNote } from "./update";
import { specificNote } from "./id";

export const noteRouter = new Elysia({ prefix: "/notes" })
.use(createNote)
.use(deleteNote)
.use(updateNote)
.use(listNotes);
.use(listNotes)
.use(specificNote);
Loading

0 comments on commit b35f1a6

Please sign in to comment.