Skip to content

Commit

Permalink
feat: WHO LET HIM COOK with the editUserTimetable, minor changes to s…
Browse files Browse the repository at this point in the history
…chema and DTOs
  • Loading branch information
M7s7 committed Nov 8, 2023
1 parent 3b936e0 commit 7fd262d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
4 changes: 2 additions & 2 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ model Event {
day String
start DateTime
end DateTime
timetable Timetable? @relation(fields: [timetableId], references: [id], onDelete: Cascade)
timetable Timetable? @relation(fields: [timetableId], references: [id], onDelete: Cascade, onUpdate: Cascade)
timetableId String?
}

model Class {
id String @unique
classType String // e.g. Lecture, Tutorial, Laboratory, etc. Not putting into enum because there are other whacky classTypes
courseName String?
Timetable Timetable? @relation(fields: [timetableId], references: [id], onDelete: Cascade)
Timetable Timetable? @relation(fields: [timetableId], references: [id], onDelete: Cascade, onUpdate: Cascade)
timetableId String?
}

Expand Down
4 changes: 3 additions & 1 deletion server/src/user/dto/timetable.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { Type } from 'class-transformer';
export class TimetableDto {
@IsString()
timetableId: string; // Randomly generated on the backend

@IsString()
name: string;
@IsArray()
@IsString({ each: true })
selectedCourses: string[];
selectedClasses: ClassDto[];
events: EventDto[];
}

Expand Down
12 changes: 6 additions & 6 deletions server/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class UserController {
constructor(private userService: UserService) {}

@Get('profile/:userId')
getUserInfo(@Param('userId') userId: string) {
async getUserInfo(@Param('userId') userId: string) {
try {
return this.userService.getUserInfo(userId);
} catch (e) {
Expand All @@ -16,25 +16,25 @@ export class UserController {
}

@Get('settings/:userId')
getUserSettings(@Param('userId') userId: string) {
async getUserSettings(@Param('userId') userId: string) {
return this.userService.getUserSettings(userId);
}

@Put('settings')
setUserSettings(
async setUserSettings(
@Body('userId') userId: string,
@Body('setting') setting: SettingsDto,
) {
this.userService.setUserSettings(userId, setting);
}

@Get('timetable/:userId')
getUserTimetables(@Param('userId') userId: string) {
async getUserTimetables(@Param('userId') userId: string) {
return null;
}

@Post('timetable')
createUserTimetable(
async createUserTimetable(
@Body('zid') zid: string,
@Body('timetableName') timetableName: string,
@Body('timetableId') timetableId: string,
Expand All @@ -46,7 +46,7 @@ export class UserController {
}

@Put('timetable')
editUserTimetable(
async editUserTimetable(
@Body('userId') userId: string,
@Body('timetable') timetable: TimetableDto,
) {
Expand Down
65 changes: 63 additions & 2 deletions server/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,72 @@ export class UserService {
}
}

// TBD: Need to think about how to do this one
// NEEDS CHECKING! THIS MIGHT BE A BAD WAY TO DO THIS
async editUserTimetable(
_zid: string,
_timetable: TimetableDto,
): Promise<void> {}
): Promise<void> {
try {
// First, update our timetable's metadata
const { timetableId, name, selectedCourses, selectedClasses, events } =
_timetable;

await prisma.timetable.update({
where: {
id: timetableId,
},
data: {
name: name,
selectedCourses: selectedCourses,
selectedClasses: {
deleteMany: {
id: { notIn: selectedClasses.map((c) => c.id) },
},
},
createdEvents: {
deleteMany: {
id: { notIn: events.map((e) => e.id) },
},
},
},
});

// Note: Update many will not work as we are update with different data
// Currently implementing as an upsert in a loop, unless there is a better way to do it
// The alternative would be dropping all related event/class records and then creating them again- would this be faster??
// https://github.com/prisma/prisma/discussions/12389
// https://stackoverflow.com/questions/71408235/how-is-upsertmany-implemented-in-prisma-orm
await prisma.$transaction(
selectedClasses.map((c) =>
prisma.class.upsert({
where: { id: c.id, timetableId: timetableId },
update: {
classType: c.classType,
courseName: c.courseName,
},
create: { ...c },
}),
),
);

await prisma.$transaction(
events.map((e) =>
prisma.event.upsert({
where: { id: e.id },
update: {
name: e.name,
location: e.location,
description: e.description,
colour: e.colour,
},
create: { ...e },
}),
),
);
} catch (e) {
throw new Error('');
}
}

async deleteUserTimetable(_timetableId: string): Promise<void> {
try {
Expand Down

0 comments on commit 7fd262d

Please sign in to comment.