|
1 | | -import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common'; |
| 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'; |
2 | 13 | import { UserService } from './user.service'; |
3 | 14 | import { AuthenticatedGuard } from 'src/auth/authenticated.guard'; |
4 | 15 | import { Request } from 'express'; |
@@ -49,4 +60,84 @@ export class UserController { |
49 | 60 | await this.userService.setSettings(req.user.id, settings); |
50 | 61 | return; |
51 | 62 | } |
| 63 | + |
| 64 | + @Get('timetables/:id') |
| 65 | + @UseGuards(AuthenticatedGuard) |
| 66 | + async getTimetable( |
| 67 | + @Req() req: AuthenticatedRequest, |
| 68 | + @Param('id') timetableId: string, |
| 69 | + ) { |
| 70 | + const timetable = await this.userService.getTimetable( |
| 71 | + req.user.id, |
| 72 | + timetableId, |
| 73 | + ); |
| 74 | + return timetable; |
| 75 | + } |
| 76 | + |
| 77 | + @Get('timetables') |
| 78 | + @UseGuards(AuthenticatedGuard) |
| 79 | + async getUserTimetables( |
| 80 | + @Req() req: AuthenticatedRequest, |
| 81 | + @Query('year') year: string, |
| 82 | + @Query('term') term: string, |
| 83 | + ) { |
| 84 | + const timetables = await this.userService.getUserTimetables( |
| 85 | + req.user.id, |
| 86 | + Number(year), |
| 87 | + term, |
| 88 | + ); |
| 89 | + return timetables; |
| 90 | + } |
| 91 | + |
| 92 | + @Post('timetables') |
| 93 | + @UseGuards(AuthenticatedGuard) |
| 94 | + async createTimetable( |
| 95 | + @Req() req: AuthenticatedRequest, |
| 96 | + @Body() data: { name: string; year: number; term: string }, |
| 97 | + ) { |
| 98 | + const timetable = await this.userService.createTimetable( |
| 99 | + req.user!.id, |
| 100 | + data, |
| 101 | + ); |
| 102 | + return timetable; |
| 103 | + } |
| 104 | + |
| 105 | + @Delete('timetables/:id') |
| 106 | + @UseGuards(AuthenticatedGuard) |
| 107 | + async deleteTimetable( |
| 108 | + @Req() req: AuthenticatedRequest, |
| 109 | + @Param('id') timetableId: string, |
| 110 | + @Query('year') year: string, |
| 111 | + @Query('term') term: string, |
| 112 | + ) { |
| 113 | + await this.userService.deleteTimetable( |
| 114 | + req.user!.id, |
| 115 | + timetableId, |
| 116 | + Number(year), |
| 117 | + term, |
| 118 | + ); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + @Patch('timetables/:id/rename') |
| 123 | + @UseGuards(AuthenticatedGuard) |
| 124 | + async renameTimetable( |
| 125 | + @Req() req: AuthenticatedRequest, |
| 126 | + @Param('id') timetableId: string, |
| 127 | + @Body('name') newName: string, |
| 128 | + ) { |
| 129 | + await this.userService.renameTimetable(req.user!.id, timetableId, newName); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + @Patch('timetables/:id/change-primary') |
| 134 | + @UseGuards(AuthenticatedGuard) |
| 135 | + async makePrimary( |
| 136 | + @Req() req: AuthenticatedRequest, |
| 137 | + @Param('id') timetableId: string, |
| 138 | + @Body() data: { year: number; term: string }, |
| 139 | + ) { |
| 140 | + await this.userService.makePrimary(req.user!.id, timetableId, data); |
| 141 | + return; |
| 142 | + } |
52 | 143 | } |
0 commit comments