Skip to content

Commit

Permalink
Merge branch 'ci/prisma-generate' into feat/rates
Browse files Browse the repository at this point in the history
  • Loading branch information
ddungiii committed Nov 21, 2023
2 parents db90a60 + 29139b3 commit e5a03cf
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ jobs:
run: npm ci
- name: Copy env file
run: cp ./env/.env.example ./env/.env.local
- name: Generate PrismaClient
run: npm run prisma-generate
- name: Build
run: npm run build
6 changes: 6 additions & 0 deletions src/common/interfaces/dto/timetable/timetable.request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ export class AddLectureDto {
@Type(() => Number)
lecture!: number;
}

export class ReorderTimetableDto {
@IsNumber()
@Type(() => Number)
arrange_order!: number;
}
21 changes: 21 additions & 0 deletions src/modules/timetables/timetables.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { session_userprofile } from '@prisma/client';
import { GetUser } from '../../common/decorators/get-user.decorator';
import {
AddLectureDto,
ReorderTimetableDto,
TimetableCreateDto,
TimetableQueryDto,
} from '../../common/interfaces/dto/timetable/timetable.request.dto';
Expand Down Expand Up @@ -93,4 +94,24 @@ export class TimetablesController {
);
return toJsonTimetable(timeTable);
}

@Post('/:timetableId/reorder')
async reorderTimetable(
/**
* @todo use user by auth instead of userId by endpoint param
* userId should be removed from endpoint in the future
* since each user should only control their own timetable
*/
@Param('userId') userId: number,
@Param('timetableId') timetableId: number,
@Body() body: ReorderTimetableDto,
@GetUser() user: session_userprofile,
) {
const timeTable = await this.timetablesService.reorderTimetable(
user,
timetableId,
body,
);
return toJsonTimetable(timeTable);
}
}
86 changes: 78 additions & 8 deletions src/modules/timetables/timetables.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import {
AddLectureDto,
ReorderTimetableDto,
TimetableCreateDto,
TimetableQueryDto,
} from '../../common/interfaces/dto/timetable/timetable.request.dto';
Expand Down Expand Up @@ -154,9 +151,8 @@ export class TimetablesService {

async deleteTimetable(user: session_userprofile, timetableId: number) {
return await this.prismaService.$transaction(async (tx) => {
const { semester, year, arrange_order } = await this.getTimetable(
timetableId,
);
const { semester, year, arrange_order } =
await this.timetableRepository.getTimeTableById(timetableId);
await this.timetableRepository.deleteById(timetableId);
const relatedTimeTables = await this.timetableRepository.getTimetables(
user,
Expand All @@ -181,4 +177,78 @@ export class TimetablesService {
);
});
}

async reorderTimetable(
user: session_userprofile,
timetableId: number,
body: ReorderTimetableDto,
) {
return await this.prismaService.$transaction(async (tx) => {
const { arrange_order: targetArrangeOrder } = body;
const targetTimetable = await this.timetableRepository.getTimeTableById(
timetableId,
);
if (targetTimetable.user_id !== user.id) {
throw new BadRequestException('User is not owner of timetable');
}
if (targetArrangeOrder === targetTimetable.arrange_order) {
return targetTimetable;
}

const relatedTimeTables = await this.timetableRepository.getTimetables(
user,
targetTimetable.year,
targetTimetable.semester,
);
if (
targetArrangeOrder < 0 ||
targetArrangeOrder >= relatedTimeTables.length
) {
throw new BadRequestException('Wrong field arrange_order in request');
}

let timeTablesToBeUpdated: { id: number; arrange_order: number }[] = [];
if (targetArrangeOrder < targetTimetable.arrange_order) {
timeTablesToBeUpdated = relatedTimeTables
.filter(
(timeTable) =>
timeTable.arrange_order >= targetArrangeOrder &&
timeTable.arrange_order < targetTimetable.arrange_order,
)
.map((timeTable) => {
return {
id: timeTable.id,
arrange_order: timeTable.arrange_order + 1,
};
});
} else if (targetArrangeOrder > targetTimetable.arrange_order) {
timeTablesToBeUpdated = relatedTimeTables
.filter(
(timeTable) =>
timeTable.arrange_order <= targetArrangeOrder &&
timeTable.arrange_order > targetTimetable.arrange_order,
)
.map((timeTable) => {
return {
id: timeTable.id,
arrange_order: timeTable.arrange_order - 1,
};
});
}

await Promise.all(
timeTablesToBeUpdated.map(async (timetable) => {
return this.timetableRepository.updateOrder(
timetable.id,
timetable.arrange_order,
);
}),
);
const updatedTimeTable = await this.timetableRepository.updateOrder(
targetTimetable.id,
targetArrangeOrder,
);
return updatedTimeTable;
});
}
}

0 comments on commit e5a03cf

Please sign in to comment.