Spaces:
Sleeping
Sleeping
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Put, | |
| Delete, | |
| Param, | |
| Body, | |
| Query, | |
| UseGuards, | |
| ParseIntPipe, | |
| Request, | |
| } from '@nestjs/common'; | |
| import { | |
| ApiTags, | |
| ApiBearerAuth, | |
| ApiOperation, | |
| ApiResponse, | |
| ApiParam, | |
| ApiBody, | |
| ApiQuery, | |
| } from '@nestjs/swagger'; | |
| import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard'; | |
| import { RolesGuard } from '../../auth/guards/roles.guard'; | |
| import { Roles } from '../../../common/decorators/roles.decorator'; | |
| import { RoleName } from '../../auth/entities/role.entity'; | |
| import { StudyGroupsService } from '../services/study-groups.service'; | |
| import { CreateStudyGroupDto } from '../dto/create-study-group.dto'; | |
| import { UpdateStudyGroupDto } from '../dto/update-study-group.dto'; | |
| ('📚 Study Groups') | |
| ('JWT-auth') | |
| ('api/study-groups') | |
| (JwtAuthGuard, RolesGuard) | |
| export class StudyGroupsController { | |
| constructor(private readonly studyGroupsService: StudyGroupsService) {} | |
| () | |
| (RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'List all study groups', | |
| description: | |
| 'Retrieves all study groups. Optionally filter by courseId. ' + | |
| 'Access roles: ALL. Uses tables: `study_groups`.', | |
| }) | |
| ({ name: 'courseId', required: false, type: Number, description: 'Filter by course ID' }) | |
| ({ status: 200, description: 'Study groups returned' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| async findAll(('courseId') courseId?: number) { | |
| return this.studyGroupsService.findAll(courseId ? +courseId : undefined); | |
| } | |
| ('my') | |
| (RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Get my study groups', | |
| description: | |
| 'Retrieves all study groups the current user is a member of. ' + | |
| 'Access roles: ALL. Uses tables: `study_group_members`, `study_groups`.', | |
| }) | |
| ({ status: 200, description: 'User study groups returned' }) | |
| ({ status: 401, description: 'Unauthorized' }) | |
| async findMyGroups(() req) { | |
| return this.studyGroupsService.findMyGroups(req.user.userId); | |
| } | |
| (':id') | |
| (RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Get study group by ID', | |
| description: | |
| 'Retrieves details of a specific study group. ' + | |
| 'Access roles: ALL. Uses table: `study_groups`.', | |
| }) | |
| ({ name: 'id', type: Number, description: 'Study group ID' }) | |
| ({ status: 200, description: 'Study group details returned' }) | |
| ({ status: 404, description: 'Study group not found' }) | |
| async findById(('id', ParseIntPipe) id: number) { | |
| return this.studyGroupsService.findById(id); | |
| } | |
| () | |
| (RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Create a study group', | |
| description: | |
| 'Creates a new study group and automatically adds the creator as the first member with "creator" role. ' + | |
| 'Access roles: ALL. Uses tables: `study_groups`, `study_group_members`.', | |
| }) | |
| ({ type: CreateStudyGroupDto }) | |
| ({ status: 201, description: 'Study group created' }) | |
| ({ status: 400, description: 'Validation error' }) | |
| async create(() dto: CreateStudyGroupDto, () req) { | |
| return this.studyGroupsService.createGroup(dto, req.user.userId); | |
| } | |
| (':id') | |
| (RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Update a study group', | |
| description: | |
| 'Updates an existing study group. Only the group creator can update. ' + | |
| 'Access roles: OWNER. Uses table: `study_groups`.', | |
| }) | |
| ({ name: 'id', type: Number, description: 'Study group ID' }) | |
| ({ type: UpdateStudyGroupDto }) | |
| ({ status: 200, description: 'Study group updated' }) | |
| ({ status: 403, description: 'Forbidden – not the creator' }) | |
| ({ status: 404, description: 'Study group not found' }) | |
| async update( | |
| ('id', ParseIntPipe) id: number, | |
| () dto: UpdateStudyGroupDto, | |
| () req, | |
| ) { | |
| return this.studyGroupsService.updateGroup(id, dto, req.user.userId); | |
| } | |
| (':id') | |
| (RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Delete a study group', | |
| description: | |
| 'Deletes a study group and all its memberships. Only the group creator or admins can delete. ' + | |
| 'Access roles: OWNER, ADMIN. Uses tables: `study_groups`, `study_group_members`.', | |
| }) | |
| ({ name: 'id', type: Number, description: 'Study group ID' }) | |
| ({ status: 200, description: 'Study group deleted' }) | |
| ({ status: 403, description: 'Forbidden – not the creator or admin' }) | |
| ({ status: 404, description: 'Study group not found' }) | |
| async delete(('id', ParseIntPipe) id: number, () req) { | |
| const userRoles = req.user.roles || []; | |
| return this.studyGroupsService.deleteGroup(id, req.user.userId, userRoles); | |
| } | |
| (':id/join') | |
| (RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Join a study group', | |
| description: | |
| 'Joins the current user to a study group. Checks if: group is active, not full, user not already a member. ' + | |
| 'Access roles: ALL. Uses tables: `study_groups`, `study_group_members`.', | |
| }) | |
| ({ name: 'id', type: Number, description: 'Study group ID' }) | |
| ({ status: 201, description: 'Joined study group successfully' }) | |
| ({ status: 400, description: 'Already a member or group is full' }) | |
| ({ status: 404, description: 'Study group not found' }) | |
| async join(('id', ParseIntPipe) id: number, () req) { | |
| return this.studyGroupsService.joinGroup(id, req.user.userId); | |
| } | |
| (':id/leave') | |
| (RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Leave a study group', | |
| description: | |
| 'Removes the current user from a study group. The group creator cannot leave (must delete the group instead). ' + | |
| 'Access roles: ALL. Uses tables: `study_groups`, `study_group_members`.', | |
| }) | |
| ({ name: 'id', type: Number, description: 'Study group ID' }) | |
| ({ status: 200, description: 'Left study group successfully' }) | |
| ({ status: 400, description: 'Not a member or is creator' }) | |
| ({ status: 404, description: 'Study group not found' }) | |
| async leave(('id', ParseIntPipe) id: number, () req) { | |
| return this.studyGroupsService.leaveGroup(id, req.user.userId); | |
| } | |
| (':id/members') | |
| (RoleName.STUDENT, RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'List study group members', | |
| description: | |
| 'Retrieves all members of a specific study group. ' + | |
| 'Access roles: ALL. Uses tables: `study_group_members`.', | |
| }) | |
| ({ name: 'id', type: Number, description: 'Study group ID' }) | |
| ({ status: 200, description: 'Members returned' }) | |
| ({ status: 404, description: 'Study group not found' }) | |
| async getMembers(('id', ParseIntPipe) id: number) { | |
| return this.studyGroupsService.getMembers(id); | |
| } | |
| } | |