Spaces:
Sleeping
Sleeping
File size: 10,993 Bytes
fd07338 ec7b20a fd07338 ec7b20a fd07338 ec7b20a fd07338 ec7b20a fd07338 cb527e9 fd07338 cb527e9 fd07338 343821c fd07338 343821c fd07338 343821c fd07338 343821c fd07338 343821c fd07338 ec7b20a cb527e9 ec7b20a 0717182 9d22537 0717182 9d22537 0717182 9d22537 0717182 9d22537 0717182 9d22537 ec7b20a cb527e9 ec7b20a fd07338 | 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 | import {
Controller,
Post,
Get,
Delete,
Param,
Body,
Query,
UseGuards,
Request,
Patch,
UseInterceptors,
UploadedFile,
HttpCode,
HttpStatus,
} from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiResponse,
ApiBearerAuth,
ApiParam,
ApiBody,
ApiConsumes,
} from '@nestjs/swagger';
import { FileInterceptor } from '@nestjs/platform-express';
import { Roles } from '../../auth/roles.decorator';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { RoleName } from '../../auth/entities/role.entity';
import { AssignmentsService } from '../services';
import {
CreateAssignmentDto,
UpdateAssignmentDto,
SubmitAssignmentDto,
GradeSubmissionDto,
AssignmentQueryDto,
UploadAssignmentInstructionDto,
UploadAssignmentSubmissionDto,
} from '../dto';
import { AssignmentStatus } from '../enums';
type UploadedAssignmentFile = {
originalname: string;
mimetype: string;
buffer: Buffer;
};
@ApiTags('📝 Assignments')
@Controller('api/assignments')
@UseGuards(JwtAuthGuard, RolesGuard)
@ApiBearerAuth('JWT-auth')
export class AssignmentsController {
constructor(private readonly assignmentsService: AssignmentsService) {}
@Get()
@ApiOperation({
summary: 'List assignments',
description:
'Filter by course, status, due dates. Supports pagination and sorting.',
})
@ApiResponse({ status: 200, description: 'Paginated list of assignments' })
async findAll(@Query() query: AssignmentQueryDto, @Request() req) {
const userId = req.user.userId || req.user.id;
const roles = req.user.roles?.map((role: any) => role.roleName || role.name || role) || [];
return this.assignmentsService.findAll(query, userId, roles);
}
@Post()
@Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
@ApiOperation({
summary: 'Create assignment',
description:
'Create a new assignment for a course. Only instructors, TAs, and admins.',
})
@ApiResponse({ status: 201, description: 'Assignment created' })
async create(@Body() dto: CreateAssignmentDto, @Request() req) {
const userId = req.user.userId || req.user.id;
return this.assignmentsService.create(dto, userId);
}
@Get(':id')
@ApiOperation({ summary: 'Get assignment details', description: 'Returns assignment with course info and submissions.' })
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
@ApiResponse({
status: 200,
description: 'Assignment details with submissions',
})
async findOne(@Param('id') id: number) {
return this.assignmentsService.findOne(+id);
}
@Patch(':id')
@Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
@ApiOperation({ summary: 'Update assignment' })
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
@ApiResponse({ status: 200, description: 'Assignment updated' })
async update(
@Param('id') id: number,
@Body() dto: UpdateAssignmentDto,
@Request() req,
) {
const userId = req.user.userId || req.user.id;
return this.assignmentsService.update(+id, dto, userId);
}
@Delete(':id')
@Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
@ApiOperation({ summary: 'Delete assignment (soft delete)' })
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
@ApiResponse({ status: 200, description: 'Assignment deleted' })
async remove(@Param('id') id: number) {
return this.assignmentsService.remove(+id);
}
@Patch(':id/status')
@Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
@ApiOperation({
summary: 'Change assignment status (publish/close/archive)',
})
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
@ApiBody({
schema: {
properties: {
status: {
type: 'string',
enum: ['draft', 'published', 'closed', 'archived'],
example: 'published',
},
},
},
})
async changeStatus(
@Param('id') id: number,
@Body('status') status: AssignmentStatus,
@Request() req,
) {
const userId = req.user.userId || req.user.id;
return this.assignmentsService.changeStatus(+id, status, userId);
}
@Post(':id/submit')
@Roles(RoleName.STUDENT)
@ApiOperation({
summary: 'Submit assignment',
description: 'Student submits their work',
})
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
async submit(
@Param('id') id: number,
@Body() dto: SubmitAssignmentDto,
@Request() req,
) {
const userId = req.user.userId || req.user.id;
return this.assignmentsService.submit(+id, dto, userId);
}
// /submissions/my must be defined BEFORE /:subId to avoid route conflicts
@Get(':id/submissions/my')
@Roles(RoleName.STUDENT)
@ApiOperation({ summary: "Get student's own submission" })
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
async getMySubmission(@Param('id') id: number, @Request() req) {
const userId = req.user.userId || req.user.id;
return this.assignmentsService.getMySubmission(+id, userId);
}
@Get(':id/submissions')
@Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
@ApiOperation({ summary: 'List all submissions for an assignment' })
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
async getSubmissions(@Param('id') id: number) {
return this.assignmentsService.getSubmissions(+id);
}
@Patch(':id/submissions/:subId/grade')
@Roles(RoleName.INSTRUCTOR, RoleName.TA)
@ApiOperation({ summary: 'Grade a submission (creates grade record)' })
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
@ApiParam({ name: 'subId', type: Number, description: 'Submission ID', example: 1 })
async gradeSubmission(
@Param('id') id: number,
@Param('subId') subId: number,
@Body() dto: GradeSubmissionDto,
@Request() req,
) {
const userId = req.user.userId || req.user.id;
return this.assignmentsService.gradeSubmission(+id, +subId, dto, userId);
}
// ============ GOOGLE DRIVE UPLOADS ============
@Post(':id/instructions/upload')
@Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
@UseInterceptors(FileInterceptor('file'))
@HttpCode(HttpStatus.CREATED)
@ApiOperation({
summary: 'Upload assignment instruction to Google Drive',
description: 'Upload an assignment instruction file (PDF, DOCX, etc.) directly to Google Drive. Creates folder structure automatically.',
})
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
required: ['file'],
properties: {
file: {
type: 'string',
format: 'binary',
description: 'Instruction file (PDF, DOCX, etc.)',
},
title: {
type: 'string',
description: 'Instruction title',
example: 'Assignment 1 - Database Design Guide',
},
orderIndex: {
type: 'integer',
description: 'Order index for sorting',
example: 1,
},
},
},
})
@ApiResponse({ status: 201, description: 'Instruction uploaded successfully' })
@ApiResponse({ status: 400, description: 'No file provided' })
@ApiResponse({ status: 404, description: 'Assignment not found' })
async uploadInstruction(
@Param('id') id: number,
@UploadedFile() file: UploadedAssignmentFile,
@Body() dto: UploadAssignmentInstructionDto,
@Request() req,
) {
if (!file) {
throw new Error('No file provided');
}
const userId = req.user.userId || req.user.id;
return this.assignmentsService.uploadInstructionToDrive(
+id,
file,
dto.title,
dto.orderIndex || 0,
userId,
);
}
@Delete(':assignmentId/instructions/:driveId')
@Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN)
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary: 'Delete assignment instruction file',
description: 'Delete an uploaded instruction file from an assignment. Removes the file from Google Drive and the database record.',
})
@ApiParam({ name: 'assignmentId', type: Number, description: 'Assignment ID', example: 3 })
@ApiParam({ name: 'driveId', type: String, description: 'Google Drive file ID (driveId from instructionFiles array)', example: '1IOjnr6y7JRO7hcoMXkpoRIw8txm7WZlV' })
@ApiResponse({ status: 200, description: 'Instruction file deleted successfully' })
@ApiResponse({ status: 400, description: 'Instruction file not found or invalid driveId' })
@ApiResponse({ status: 403, description: 'Forbidden — user not authorized' })
@ApiResponse({ status: 404, description: 'Assignment not found' })
async deleteInstruction(
@Param('assignmentId') assignmentId: number,
@Param('driveId') driveId: string,
@Request() req,
) {
const userId = req.user.userId || req.user.id;
return this.assignmentsService.deleteInstructionFile(
+assignmentId,
driveId,
userId,
);
}
@Post(':id/submissions/upload')
@Roles(RoleName.STUDENT)
@UseInterceptors(FileInterceptor('file'))
@HttpCode(HttpStatus.CREATED)
@ApiOperation({
summary: 'Upload assignment submission to Google Drive',
description: 'Upload assignment submission file directly to Google Drive. Creates student folder automatically. Auto-detects late submissions.',
})
@ApiParam({ name: 'id', type: Number, description: 'Assignment ID', example: 3 })
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
required: ['file'],
properties: {
file: {
type: 'string',
format: 'binary',
description: 'Submission file',
},
submissionText: {
type: 'string',
description: 'Optional submission notes/comments',
example: 'Completed all tasks as instructed.',
},
submissionLink: {
type: 'string',
description: 'Optional link to external submission (e.g., GitHub)',
example: 'https://github.com/student/project',
},
},
},
})
@ApiResponse({ status: 201, description: 'Submission uploaded successfully' })
@ApiResponse({ status: 400, description: 'No file provided' })
@ApiResponse({ status: 404, description: 'Assignment not found' })
async uploadSubmission(
@Param('id') id: number,
@UploadedFile() file: UploadedAssignmentFile,
@Body() dto: UploadAssignmentSubmissionDto,
@Request() req,
) {
if (!file) {
throw new Error('No file provided');
}
const userId = req.user.userId || req.user.id;
return this.assignmentsService.uploadSubmissionToDrive(
+id,
file,
dto.submissionText,
dto.submissionLink,
userId,
);
}
}
|