File size: 7,429 Bytes
a12cb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8aa5b53
 
 
 
a12cb0a
 
 
 
 
 
 
 
 
 
 
 
8aa5b53
 
a12cb0a
 
 
 
 
 
 
 
 
 
 
8aa5b53
a12cb0a
 
 
8aa5b53
 
 
a12cb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8aa5b53
 
a12cb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8a7b69
 
 
a12cb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32b4d43
 
 
 
 
 
 
 
 
 
 
 
 
a12cb0a
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
import {
  Controller,
  Get,
  Post,
  Put,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  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 { DiscussionsService } from '../services/discussions.service';
import { CreateThreadDto, UpdateThreadDto, CreateReplyDto, ThreadQueryDto } from '../dto';

@ApiTags('💭 Discussions')
@ApiBearerAuth('JWT-auth')
@Controller('api/discussions')
@UseGuards(JwtAuthGuard, RolesGuard)
export class DiscussionsController {
  constructor(private readonly discussionsService: DiscussionsService) {}

  @Get()
  @ApiOperation({
    summary: 'List discussion threads',
    description: 'List discussion threads with optional course filter. Pinned threads appear first.',
  })
  @ApiResponse({ status: 200, description: 'Threads retrieved' })
  async findAll(@Query() query: ThreadQueryDto, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    const roles = req.user.roles?.map((r: any) => r.roleName || r.name) || [];
    return this.discussionsService.findAll(query, userId, roles);
  }

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({
    summary: 'Create discussion thread',
    description: 'Create a new discussion thread in a course.',
  })
  @ApiBody({ type: CreateThreadDto })
  @ApiResponse({ status: 201, description: 'Thread created' })
  async create(@Body() dto: CreateThreadDto, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    const roles = req.user.roles?.map((r: any) => r.roleName || r.name) || [];
    return this.discussionsService.create(dto, userId, roles);
  }

  @Get(':id')
  @ApiOperation({
    summary: 'Get thread with replies',
    description: 'Get a discussion thread with paginated replies. Answers appear first. Increments view count.',
  })
  @ApiParam({ name: 'id', description: 'Thread ID', example: 1 })
  @ApiResponse({ status: 200, description: 'Thread with replies' })
  async getThreadWithReplies(
    @Param('id', ParseIntPipe) id: number,
    @Req() req: any,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = req.user.roles?.map((r: any) => r.roleName || r.name) || [];
    return this.discussionsService.getThreadWithReplies(id, userId, roles, page || 1, limit || 50);
  }

  @Put(':id')
  @ApiOperation({
    summary: 'Update thread',
    description: 'Update thread title/description. Author, instructors, and admins can update.',
  })
  @ApiParam({ name: 'id', description: 'Thread ID', example: 1 })
  @ApiBody({ type: UpdateThreadDto })
  @ApiResponse({ status: 200, description: 'Thread updated' })
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: UpdateThreadDto,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = req.user.roles?.map((r: any) => r.roleName || r.name) || [];
    return this.discussionsService.update(id, dto, userId, roles);
  }

  @Delete(':id')
  @Roles(RoleName.INSTRUCTOR, RoleName.ADMIN, RoleName.IT_ADMIN)
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({
    summary: 'Delete thread',
    description: 'Delete a discussion thread and all its replies. Instructors and admins only.',
  })
  @ApiParam({ name: 'id', description: 'Thread ID', example: 1 })
  @ApiResponse({ status: 204, description: 'Thread deleted' })
  async remove(@Param('id', ParseIntPipe) id: number) {
    return this.discussionsService.remove(id);
  }

  @Post(':id/reply')
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({
    summary: 'Post reply',
    description: 'Post a reply to a discussion thread. Fails if thread is locked.',
  })
  @ApiParam({ name: 'id', description: 'Thread ID', example: 1 })
  @ApiBody({ type: CreateReplyDto })
  @ApiResponse({ status: 201, description: 'Reply posted' })
  @ApiResponse({ status: 400, description: 'Thread is locked' })
  async addReply(
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: CreateReplyDto,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    const roles = req.user.roles?.map((r: any) => r.roleName || r.name) || [];
    return this.discussionsService.addReply(id, userId, roles, dto);
  }

  @Patch(':id/pin')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Toggle pin thread',
    description: 'Pin or unpin a discussion thread. Pinned threads appear at the top of the list.',
  })
  @ApiParam({ name: 'id', description: 'Thread ID', example: 1 })
  @ApiResponse({ status: 200, description: 'Pin status toggled' })
  async togglePin(@Param('id', ParseIntPipe) id: number) {
    return this.discussionsService.togglePin(id);
  }

  @Patch(':id/lock')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Toggle lock thread',
    description: 'Lock or unlock a discussion thread. Locked threads cannot receive new replies.',
  })
  @ApiParam({ name: 'id', description: 'Thread ID', example: 1 })
  @ApiResponse({ status: 200, description: 'Lock status toggled' })
  async toggleLock(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    return this.discussionsService.toggleLock(id, userId);
  }

  @Patch('replies/:replyId/mark-answer')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Toggle mark as answer',
    description: 'Mark or unmark a reply as the accepted answer. Answers appear first in the reply list.',
  })
  @ApiParam({ name: 'replyId', description: 'Reply message ID', example: 1 })
  @ApiResponse({ status: 200, description: 'Answer status toggled' })
  async markAnswer(@Param('replyId', ParseIntPipe) replyId: number) {
    return this.discussionsService.markAnswer(replyId);
  }

  @Patch('replies/:replyId/endorse')
  @Roles(RoleName.INSTRUCTOR, RoleName.TA, RoleName.ADMIN, RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Toggle endorse reply',
    description: 'Endorse or un-endorse a reply. Shows instructor approval of the answer.',
  })
  @ApiParam({ name: 'replyId', description: 'Reply message ID', example: 1 })
  @ApiResponse({ status: 200, description: 'Endorsement toggled' })
  async endorseReply(@Param('replyId', ParseIntPipe) replyId: number, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    return this.discussionsService.endorseReply(replyId, userId);
  }

  @Post('replies/:replyId/upvote')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({
    summary: 'Toggle upvote on reply',
    description: 'Add or remove an upvote on a discussion reply.',
  })
  @ApiParam({ name: 'replyId', description: 'Reply message ID', example: 1 })
  @ApiResponse({ status: 200, description: 'Upvote toggled' })
  async toggleMessageUpvote(@Param('replyId', ParseIntPipe) replyId: number, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    return this.discussionsService.toggleMessageUpvote(replyId, userId);
  }
}