-
Notifications
You must be signed in to change notification settings - Fork 6
Update backend routes for events #1018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
48fe5f9
e6df03b
b7225c3
65a52ec
f324cfa
2b6e47d
82fe5e5
3292f7b
fb050ae
8de03a0
cc6e318
cdc4cb1
aae45c3
cd41598
a8cec1f
928b874
1298e0d
02866a0
f353af6
803d808
7152091
16627da
0da2ef7
7cd505f
ad8df30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /* | ||
| Warnings: | ||
|
|
||
| - Added the required column `title` to the `event` table without a default value. This is not possible if the table is not empty. | ||
|
|
||
| */ | ||
| -- AlterTable | ||
| ALTER TABLE "event" ADD COLUMN "description" TEXT, | ||
| ADD COLUMN "location" TEXT, | ||
| ADD COLUMN "title" TEXT NOT NULL; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,11 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | |
| import { PrismaService } from 'src/prisma/prisma.service'; | ||
| import { GraphqlService } from 'src/graphql/graphql.service'; | ||
| import { validate } from 'src/utils/validate'; | ||
| import { CourseDetails, UserTimetable } from './types'; | ||
| import { CourseDetails, UserTimetable, EventParametersDto } from './types'; | ||
| import type { ClassDetails } from 'src/graphql/types'; | ||
| import { EventType } from '../generated/prisma/enums'; | ||
| import { EventMinAggregateOutputType } from '../generated/prisma/models/Event'; | ||
| import type { Event } from 'src/generated/prisma/client'; | ||
|
|
||
| @Injectable() | ||
| export class TimetableService { | ||
|
|
@@ -428,4 +431,138 @@ export class TimetableService { | |
| }), | ||
| ]); | ||
| } | ||
|
|
||
| async getEvent(userId: string, eventId: string): Promise<Event> { | ||
| try { | ||
| const event = await this.prisma.event.findUnique({ | ||
| select: { | ||
| id: true, | ||
| colour: true, | ||
| dayOfWeek: true, | ||
| start: true, | ||
| end: true, | ||
| type: true, | ||
| title: true, | ||
| description: true, | ||
| location: true, | ||
| timetable: { select: { userId: true, id: true } }, | ||
| }, | ||
| where: { id: eventId }, | ||
| }); | ||
|
|
||
| if (!event || event.timetable.userId !== userId) { | ||
| throw new HttpException('Event not found', HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| const eventData = { | ||
| id: event.id, | ||
| timetableId: event.timetable.id, | ||
| colour: event.colour, | ||
| dayOfWeek: event.dayOfWeek, | ||
| start: event.start, | ||
| end: event.end, | ||
| type: event.type, | ||
| title: event.title, | ||
| description: event.description ?? null, | ||
| location: event.location ?? null, | ||
| }; | ||
|
|
||
| return eventData; | ||
| } catch { | ||
| throw new HttpException('Event not in timetable', HttpStatus.NOT_FOUND); | ||
| } | ||
| } | ||
|
|
||
| async getAllEvent( | ||
| userId: string, | ||
| timetableId: string, | ||
| ): Promise<EventMinAggregateOutputType[]> { | ||
| const timetableExists = await this.isTimetableOwnedByUser( | ||
| userId, | ||
| timetableId, | ||
| ); | ||
| validate(timetableExists, 'Timetable does not exist', HttpStatus.NOT_FOUND); | ||
|
|
||
| const events = (await this.prisma.event.findMany({ | ||
| where: { timetableId }, | ||
| })) as EventMinAggregateOutputType[]; | ||
|
|
||
| return events; | ||
| } | ||
|
|
||
| async addEvent( | ||
| userId: string, | ||
| eventDetails: EventParametersDto, | ||
| timetableId: string, | ||
| ): Promise<void> { | ||
| const timetableExists = await this.isTimetableOwnedByUser( | ||
| userId, | ||
| timetableId, | ||
| ); | ||
| validate(timetableExists, 'Timetable does not exist', HttpStatus.NOT_FOUND); | ||
|
|
||
| const colourValid = this.isColourCodeValid(eventDetails.colour); | ||
| validate(colourValid, 'Colour code is not valid', HttpStatus.BAD_REQUEST); | ||
|
|
||
| if (!(eventDetails.type in EventType)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious if this is actually possible. Might be worth testing to see what happens when you pass an invalid type... Ask @jason4193 about using Postman later.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lhvy Tested on the Postman, only |
||
| throw new HttpException('Invalid event type', HttpStatus.BAD_REQUEST); | ||
| } | ||
|
|
||
| await this.prisma.event.create({ | ||
| data: { | ||
| title: eventDetails.title, | ||
| description: eventDetails.description ?? undefined, | ||
| location: eventDetails.location ?? undefined, | ||
| colour: eventDetails.colour, | ||
| dayOfWeek: eventDetails.dayOfWeek, | ||
| start: eventDetails.start, | ||
| end: eventDetails.end, | ||
| type: eventDetails.type, | ||
| timetable: { connect: { id: timetableId } }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| async removeEvent(userId: string, eventId: string): Promise<void> { | ||
| if (!(await this.isEventOwnedByUser(userId, eventId))) { | ||
| throw new HttpException('Event could not be found', HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| await this.prisma.event.delete({ | ||
| where: { | ||
| id: eventId, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| async updateEvent( | ||
| userId: string, | ||
| eventId: string, | ||
| eventDetails: EventParametersDto, | ||
| ): Promise<void> { | ||
| if (!(await this.isEventOwnedByUser(userId, eventId))) { | ||
| throw new HttpException('Event could not be found', HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| const dtoKeys = Object.keys(new EventParametersDto()); | ||
|
|
||
| const filteredData = Object.fromEntries( | ||
| Object.entries(eventDetails).filter( | ||
| ([key, value]) => value !== undefined && dtoKeys.includes(key), | ||
| ), | ||
| ); | ||
|
|
||
| await this.prisma.event.update({ | ||
fox-58 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| where: { id: eventId }, | ||
| data: filteredData, | ||
| }); | ||
| } | ||
|
|
||
| async isEventOwnedByUser(userId: string, eventId: string): Promise<boolean> { | ||
| const hit = await this.prisma.event.findFirst({ | ||
| where: { id: eventId, timetable: { userId } }, | ||
| select: { id: true }, | ||
| }); | ||
| return !!hit; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.