Spaces:
Sleeping
Sleeping
File size: 10,559 Bytes
3b492d9 e6b22aa 3b492d9 e6677f0 e6b22aa e6677f0 3b492d9 e6b22aa 3b492d9 e6677f0 3b492d9 e6677f0 aa3bec5 e6677f0 3b492d9 e6677f0 3b492d9 1e03ebd 3b492d9 e6677f0 3b492d9 e6b22aa e6677f0 3b492d9 e6b22aa e6677f0 3b492d9 e6b22aa e6677f0 3b492d9 e6677f0 3b492d9 e6b22aa e6677f0 3b492d9 e6b22aa e6677f0 3b492d9 | 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | import {
Controller,
Get,
Post,
Patch,
Delete,
Body,
Param,
Query,
UseGuards,
HttpStatus,
HttpCode,
ParseIntPipe,
} from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiResponse,
ApiBearerAuth,
ApiParam,
ApiQuery,
ApiBody,
} from '@nestjs/swagger';
import { CoursesService } from '../services/courses.service';
import { CreateCourseDto, UpdateCourseDto, CreatePrerequisiteDto } from '../dtos';
import { CourseStatus } from '../enums';
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';
@ApiTags('π Courses')
@Controller('api/courses')
export class CoursesController {
constructor(private readonly coursesService: CoursesService) {}
@Get()
@ApiOperation({
summary: 'List all courses',
description: `
## List All Courses
Retrieves a paginated list of courses with optional filters.
### Access Control
- **Authentication Required**: No (Public endpoint)
- **Roles Required**: None (accessible to all)
### Filtering Options
- \`departmentId\`: Filter by department
- \`level\`: Filter by course level (100, 200, 300, etc.)
- \`status\`: Filter by course status
- \`search\`: Search by course name or code
### Pagination
- \`page\`: Page number (default: 1)
- \`limit\`: Items per page (default: 20, max: 100)
`,
})
@ApiQuery({ name: 'departmentId', required: false, type: Number })
@ApiQuery({ name: 'level', required: false, type: String, example: '300' })
@ApiQuery({
name: 'status',
required: false,
type: String,
schema: { type: 'string', enum: Object.values(CourseStatus) },
})
@ApiQuery({ name: 'search', required: false, type: String })
@ApiQuery({ name: 'page', required: false, type: Number, example: 1 })
@ApiQuery({ name: 'limit', required: false, type: Number, example: 20 })
@ApiResponse({ status: 200, description: 'Paginated list of courses' })
async findAll(
@Query('departmentId', new ParseIntPipe({ optional: true }))
departmentId?: number,
@Query('level') level?: string,
@Query('status') status?: CourseStatus,
@Query('search') search?: string,
@Query('page', new ParseIntPipe({ optional: true })) page = 1,
@Query('limit', new ParseIntPipe({ optional: true })) limit = 20,
) {
return this.coursesService.findAll(
departmentId,
level,
status,
search,
page,
limit,
);
}
@Get('department/:deptId')
@ApiOperation({
summary: 'Get courses by department',
description: `
## Get Courses by Department
Retrieves all courses offered by a specific department.
### Access Control
- **Authentication Required**: No (Public endpoint)
- **Roles Required**: None
`,
})
@ApiParam({ name: 'deptId', description: 'Department ID', type: Number })
@ApiResponse({ status: 200, description: 'List of courses' })
@ApiResponse({ status: 404, description: 'Department not found' })
async findByDepartment(
@Param('deptId', ParseIntPipe) deptId: number,
) {
return this.coursesService.findByDepartment(deptId);
}
@Get(':id/recent-activity')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN)
@ApiBearerAuth('JWT-auth')
@ApiOperation({
summary: 'Get recent course activity',
description:
'Returns recent timeline items for a course (assignments, materials, submissions, grading).',
})
@ApiParam({ name: 'id', description: 'Course ID', type: Number })
@ApiQuery({ name: 'limit', required: false, type: Number, example: 8 })
@ApiResponse({ status: 200, description: 'Recent activity list' })
async getRecentActivity(
@Param('id', ParseIntPipe) id: number,
@Query('limit', new ParseIntPipe({ optional: true })) limit = 8,
) {
return this.coursesService.getRecentActivity(id, limit);
}
@Get(':id')
@ApiOperation({
summary: 'Get course by ID',
description: `
## Get Course Details
Retrieves detailed information about a specific course.
### Access Control
- **Authentication Required**: No (Public endpoint)
- **Roles Required**: None
### Response Includes
- Course information (code, name, credits)
- Prerequisites count
- Available sections count
`,
})
@ApiParam({ name: 'id', description: 'Course ID', type: Number })
@ApiResponse({ status: 200, description: 'Course details' })
@ApiResponse({ status: 404, description: 'Course not found' })
async findById(@Param('id', ParseIntPipe) id: number) {
const course = await this.coursesService.findById(id);
const prerequisites = await this.coursesService.getPrerequisites(id);
return {
...course,
prerequisitesCount: prerequisites.length,
sectionsCount: course.sections.length,
};
}
@Post()
@HttpCode(HttpStatus.CREATED)
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(RoleName.ADMIN, RoleName.IT_ADMIN)
@ApiBearerAuth('JWT-auth')
@ApiOperation({
summary: 'Create new course',
description: `
## Create New Course
Creates a new course in the catalog.
### Access Control
- **Authentication Required**: β
Yes (Bearer Token recommended)
- **Roles Required**: ADMIN, IT_ADMIN, INSTRUCTOR
### Required Fields
- Course code (unique)
- Course name
- Credit hours
- Department ID
### Notes
- Course codes must be unique
- New courses are active by default
`,
})
@ApiBody({ type: CreateCourseDto })
@ApiResponse({ status: 201, description: 'Course created successfully' })
@ApiResponse({ status: 400, description: 'Invalid input data' })
@ApiResponse({ status: 409, description: 'Course code already exists' })
async create(@Body() dto: CreateCourseDto) {
return this.coursesService.create(dto);
}
@Patch(':id')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(RoleName.ADMIN, RoleName.IT_ADMIN)
@ApiBearerAuth('JWT-auth')
@ApiOperation({
summary: 'Update course',
description: `
## Update Course
Updates an existing course's information.
### Access Control
- **Authentication Required**: β
Yes (Bearer Token recommended)
- **Roles Required**: ADMIN, IT_ADMIN, INSTRUCTOR
`,
})
@ApiParam({ name: 'id', description: 'Course ID', type: Number })
@ApiBody({ type: UpdateCourseDto })
@ApiResponse({ status: 200, description: 'Course updated successfully' })
@ApiResponse({ status: 400, description: 'Invalid input data' })
@ApiResponse({ status: 404, description: 'Course not found' })
async update(
@Param('id', ParseIntPipe) id: number,
@Body() dto: UpdateCourseDto,
) {
return this.coursesService.update(id, dto);
}
@Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT)
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(RoleName.ADMIN, RoleName.IT_ADMIN)
@ApiBearerAuth('JWT-auth')
@ApiOperation({
summary: 'Delete course',
description: `
## Delete Course (Soft Delete)
Soft deletes a course from the system.
### Access Control
- **Authentication Required**: β
Yes (Bearer Token recommended)
- **Roles Required**: ADMIN, IT_ADMIN
### Notes
- This performs a soft delete (course is marked as inactive)
- Courses with active enrollments cannot be deleted
`,
})
@ApiParam({ name: 'id', description: 'Course ID', type: Number })
@ApiResponse({ status: 204, description: 'Course deleted successfully' })
@ApiResponse({ status: 400, description: 'Cannot delete course with enrollments' })
@ApiResponse({ status: 404, description: 'Course not found' })
async delete(@Param('id', ParseIntPipe) id: number) {
await this.coursesService.softDelete(id);
}
@Get(':id/prerequisites')
@ApiOperation({
summary: 'Get course prerequisites',
description: `
## Get Course Prerequisites
Retrieves all prerequisite courses for a specific course.
### Access Control
- **Authentication Required**: No (Public endpoint)
- **Roles Required**: None
### Response
Returns list of courses that must be completed before enrolling.
`,
})
@ApiParam({ name: 'id', description: 'Course ID', type: Number })
@ApiResponse({ status: 200, description: 'List of prerequisite courses' })
@ApiResponse({ status: 404, description: 'Course not found' })
async getPrerequisites(@Param('id', ParseIntPipe) id: number) {
return this.coursesService.getPrerequisites(id);
}
@Post(':id/prerequisites')
@HttpCode(HttpStatus.CREATED)
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(RoleName.ADMIN, RoleName.IT_ADMIN)
@ApiBearerAuth('JWT-auth')
@ApiOperation({
summary: 'Add prerequisite to course',
description: `
## Add Course Prerequisite
Adds a prerequisite requirement to a course.
### Access Control
- **Authentication Required**: β
Yes (Bearer Token recommended)
- **Roles Required**: ADMIN, IT_ADMIN
### Notes
- Cannot add circular prerequisites
- A course cannot be its own prerequisite
`,
})
@ApiParam({ name: 'id', description: 'Course ID', type: Number })
@ApiBody({ type: CreatePrerequisiteDto })
@ApiResponse({ status: 201, description: 'Prerequisite added successfully' })
@ApiResponse({ status: 400, description: 'Invalid prerequisite or circular dependency' })
@ApiResponse({ status: 404, description: 'Course not found' })
async addPrerequisite(
@Param('id', ParseIntPipe) id: number,
@Body() dto: CreatePrerequisiteDto,
) {
return this.coursesService.addPrerequisite(
id,
dto.prerequisiteCourseId,
dto.isMandatory,
);
}
@Delete(':id/prerequisites/:prereqId')
@HttpCode(HttpStatus.NO_CONTENT)
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(RoleName.ADMIN, RoleName.IT_ADMIN)
@ApiBearerAuth('JWT-auth')
@ApiOperation({
summary: 'Remove prerequisite from course',
description: `
## Remove Course Prerequisite
Removes a prerequisite requirement from a course.
### Access Control
- **Authentication Required**: β
Yes (Bearer Token recommended)
- **Roles Required**: ADMIN, IT_ADMIN
`,
})
@ApiParam({ name: 'id', description: 'Course ID', type: Number })
@ApiParam({ name: 'prereqId', description: 'Prerequisite Course ID', type: Number })
@ApiResponse({ status: 204, description: 'Prerequisite removed successfully' })
@ApiResponse({ status: 404, description: 'Course or prerequisite not found' })
async removePrerequisite(
@Param('id', ParseIntPipe) id: number,
@Param('prereqId', ParseIntPipe) prereqId: number,
) {
await this.coursesService.removePrerequisite(id, prereqId);
}
}
|