Skip to content

Commit 1d6a6ef

Browse files
sunnyyuqingchensunny chen
andauthored
Restructure backend routes add timetable routes (#1042)
* created routes for user timetables * fixed returns * separarted timetable and course routes into the new structure * removed redundant imports * fix redundant assert --------- Co-authored-by: sunny chen <[email protected]>
1 parent cb49829 commit 1d6a6ef

File tree

7 files changed

+578
-454
lines changed

7 files changed

+578
-454
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Module } from '@nestjs/common';
2+
import { TimetableController } from './timetable.controller';
3+
import { TimetableService } from './timetable.service';
4+
5+
@Module({
6+
controllers: [TimetableController],
7+
providers: [TimetableService],
8+
})
9+
export class TimetableModule {}

0 commit comments

Comments
 (0)