File size: 7,805 Bytes
84818f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Patch,
  Body,
  Param,
  Req,
  UseGuards,
  ParseIntPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiParam,
  ApiBody,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { Roles } from '../../auth/roles.decorator';
import { RoleName } from '../../auth/entities/role.entity';
import { CourseStructureService } from '../services';
import {
  CreateStructureDto,
  UpdateStructureDto,
  ReorderStructureDto,
} from '../dto';

@ApiTags('πŸ—οΈ Course Structure')
@ApiBearerAuth('JWT-auth')
@Controller('api/courses/:courseId/structure')
@UseGuards(JwtAuthGuard, RolesGuard)
export class CourseStructureController {
  constructor(private readonly structureService: CourseStructureService) {}

  @Get()
  @ApiOperation({
    summary: 'Get course structure',
    description: `
## Get Course Content Structure

Returns the course's content organization (lectures, sections, labs) grouped by week.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles**: ALL

### Response Structure
- \`data\`: Flat list of all structure items
- \`byWeek\`: Items grouped by week number for easy display

### Use Cases
- Building course syllabus view
- Navigation sidebar
- Progress tracking
    `,
  })
  @ApiParam({ name: 'courseId', description: 'Course ID', type: Number, example: 1 })
  @ApiResponse({ status: 200, description: 'Course structure retrieved' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  async findAll(@Param('courseId', ParseIntPipe) courseId: number) {
    return this.structureService.findAll(courseId);
  }

  @Get(':id')
  @ApiOperation({
    summary: 'Get structure item by ID',
    description: `
## Get Structure Item Details

Returns detailed information about a specific structure item.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles**: ALL
    `,
  })
  @ApiParam({ name: 'courseId', description: 'Course ID', type: Number, example: 1 })
  @ApiParam({ name: 'id', description: 'Structure item ID', type: Number, example: 1 })
  @ApiResponse({ status: 200, description: 'Structure item details' })
  @ApiResponse({ status: 404, description: 'Structure item not found' })
  async findById(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.structureService.findById(id);
  }

  @Post()
  @Roles(RoleName.INSTRUCTOR, RoleName.ADMIN, RoleName.IT_ADMIN)
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({
    summary: 'Create structure item',
    description: `
## Create Course Structure Item

Adds a new content organization item (lecture, section, lab) to the course.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles**: INSTRUCTOR, ADMIN only (TAs cannot modify structure)

### Organization Types
- \`lecture\`: Main lecture content
- \`section\`: Discussion or tutorial section
- \`lab\`: Hands-on lab session
- \`tutorial\`: Tutorial session

### Ordering
- Items are automatically ordered within their week
- Use \`orderIndex\` to specify custom order
- Use PATCH /reorder to reorder multiple items
    `,
  })
  @ApiParam({ name: 'courseId', description: 'Course ID', type: Number, example: 1 })
  @ApiBody({ type: CreateStructureDto })
  @ApiResponse({ status: 201, description: 'Structure item created' })
  @ApiResponse({ status: 400, description: 'Invalid input data' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  async create(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Body() dto: CreateStructureDto,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    return this.structureService.create(courseId, dto, userId, roles);
  }

  @Put(':id')
  @Roles(RoleName.INSTRUCTOR, RoleName.ADMIN, RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Update structure item',
    description: `
## Update Structure Item

Updates an existing structure item.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles**: INSTRUCTOR, ADMIN only
    `,
  })
  @ApiParam({ name: 'courseId', description: 'Course ID', type: Number, example: 1 })
  @ApiParam({ name: 'id', description: 'Structure item ID', type: Number, example: 1 })
  @ApiBody({ type: UpdateStructureDto })
  @ApiResponse({ status: 200, description: 'Structure item updated' })
  @ApiResponse({ status: 404, description: 'Structure item not found' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  async update(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: UpdateStructureDto,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    return this.structureService.update(id, dto, userId, roles);
  }

  @Delete(':id')
  @Roles(RoleName.INSTRUCTOR, RoleName.ADMIN, RoleName.IT_ADMIN)
  @HttpCode(HttpStatus.OK)
  @ApiOperation({
    summary: 'Delete structure item',
    description: `
## Delete Structure Item

Removes a structure item from the course.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles**: INSTRUCTOR, ADMIN only

### Note
Deleting a structure item does NOT delete associated materials.
    `,
  })
  @ApiParam({ name: 'courseId', description: 'Course ID', type: Number, example: 1 })
  @ApiParam({ name: 'id', description: 'Structure item ID', type: Number, example: 1 })
  @ApiResponse({ status: 200, description: 'Structure item deleted' })
  @ApiResponse({ status: 404, description: 'Structure item not found' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  async delete(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Param('id', ParseIntPipe) id: number,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    return this.structureService.delete(id, userId, roles);
  }

  @Patch('reorder')
  @Roles(RoleName.INSTRUCTOR, RoleName.ADMIN, RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Reorder structure items',
    description: `
## Reorder Structure Items

Changes the order of structure items within a course.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles**: INSTRUCTOR, ADMIN only

### Request Body
Provide array of structure item IDs in the desired new order.

### Example
\`\`\`json
{
  "orderIds": [3, 1, 2, 4]
}
\`\`\`
This would reorder items so that:
- ID 3 is first (orderIndex: 0)
- ID 1 is second (orderIndex: 1)
- ID 2 is third (orderIndex: 2)
- ID 4 is fourth (orderIndex: 3)
    `,
  })
  @ApiParam({ name: 'courseId', description: 'Course ID', type: Number, example: 1 })
  @ApiBody({ type: ReorderStructureDto })
  @ApiResponse({ status: 200, description: 'Items reordered successfully' })
  @ApiResponse({ status: 400, description: 'Invalid input data' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  @ApiResponse({ status: 404, description: 'Some items not found' })
  async reorder(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Body() dto: ReorderStructureDto,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = this.extractRoles(req.user);
    return this.structureService.reorder(courseId, dto, userId, roles);
  }

  private extractRoles(user: any): string[] {
    if (Array.isArray(user.roles)) {
      return user.roles.map((r: any) => (typeof r === 'string' ? r : r.name || r.roleName));
    }
    return [];
  }
}