|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Delete, |
| 5 | + Get, |
| 6 | + Param, |
| 7 | + Patch, |
| 8 | + Post, |
| 9 | + Query, |
| 10 | + Req, |
| 11 | + UseGuards, |
| 12 | +} from '@nestjs/common'; |
| 13 | +import { TimetableService } from './timetable.service'; |
| 14 | +import { AuthenticatedGuard } from 'src/auth/authenticated.guard'; |
| 15 | +import { Request } from 'express'; |
| 16 | + |
| 17 | +interface AuthenticatedRequest extends Request { |
| 18 | + user: { |
| 19 | + id: string; |
| 20 | + oidcId?: string; |
| 21 | + isGuest: boolean; |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +@Controller('user/timetables') |
| 26 | +export class TimetableController { |
| 27 | + constructor(private timetableService: TimetableService) {} |
| 28 | + |
| 29 | + @Get(':id') |
| 30 | + @UseGuards(AuthenticatedGuard) |
| 31 | + async getTimetable( |
| 32 | + @Req() req: AuthenticatedRequest, |
| 33 | + @Param('id') timetableId: string, |
| 34 | + ) { |
| 35 | + const timetable = await this.timetableService.getTimetable( |
| 36 | + req.user.id, |
| 37 | + timetableId, |
| 38 | + ); |
| 39 | + return timetable; |
| 40 | + } |
| 41 | + |
| 42 | + @Get() |
| 43 | + @UseGuards(AuthenticatedGuard) |
| 44 | + async getUserTimetables( |
| 45 | + @Req() req: AuthenticatedRequest, |
| 46 | + @Query('year') year: string, |
| 47 | + @Query('term') term: string, |
| 48 | + ) { |
| 49 | + const timetables = await this.timetableService.getUserTimetables( |
| 50 | + req.user.id, |
| 51 | + Number(year), |
| 52 | + term, |
| 53 | + ); |
| 54 | + return timetables; |
| 55 | + } |
| 56 | + |
| 57 | + @Post() |
| 58 | + @UseGuards(AuthenticatedGuard) |
| 59 | + async createTimetable( |
| 60 | + @Req() req: AuthenticatedRequest, |
| 61 | + @Body() data: { name: string; year: number; term: string }, |
| 62 | + ) { |
| 63 | + const timetable = await this.timetableService.createTimetable( |
| 64 | + req.user.id, |
| 65 | + data, |
| 66 | + ); |
| 67 | + return timetable; |
| 68 | + } |
| 69 | + |
| 70 | + @Delete(':id') |
| 71 | + @UseGuards(AuthenticatedGuard) |
| 72 | + async deleteTimetable( |
| 73 | + @Req() req: AuthenticatedRequest, |
| 74 | + @Param('id') timetableId: string, |
| 75 | + @Query('year') year: string, |
| 76 | + @Query('term') term: string, |
| 77 | + ) { |
| 78 | + await this.timetableService.deleteTimetable( |
| 79 | + req.user.id, |
| 80 | + timetableId, |
| 81 | + Number(year), |
| 82 | + term, |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + @Patch(':id/rename') |
| 87 | + @UseGuards(AuthenticatedGuard) |
| 88 | + async renameTimetable( |
| 89 | + @Req() req: AuthenticatedRequest, |
| 90 | + @Param('id') timetableId: string, |
| 91 | + @Body('name') newName: string, |
| 92 | + ) { |
| 93 | + await this.timetableService.renameTimetable( |
| 94 | + req.user.id, |
| 95 | + timetableId, |
| 96 | + newName, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + @Patch(':id/change-primary') |
| 101 | + @UseGuards(AuthenticatedGuard) |
| 102 | + async makePrimary( |
| 103 | + @Req() req: AuthenticatedRequest, |
| 104 | + @Param('id') timetableId: string, |
| 105 | + @Body() data: { year: number; term: string }, |
| 106 | + ) { |
| 107 | + await this.timetableService.makePrimary(req.user.id, timetableId, data); |
| 108 | + } |
| 109 | +} |
0 commit comments