|
1 | 1 | package com.openisle.controller; |
2 | 2 |
|
| 3 | +import com.openisle.dto.CommentContextDto; |
3 | 4 | import com.openisle.dto.CommentDto; |
4 | 5 | import com.openisle.dto.CommentRequest; |
5 | 6 | import com.openisle.dto.PostChangeLogDto; |
6 | 7 | import com.openisle.dto.TimelineItemDto; |
7 | 8 | import com.openisle.mapper.CommentMapper; |
8 | 9 | import com.openisle.mapper.PostChangeLogMapper; |
| 10 | +import com.openisle.mapper.PostMapper; |
9 | 11 | import com.openisle.model.Comment; |
10 | 12 | import com.openisle.model.CommentSort; |
11 | 13 | import com.openisle.service.*; |
@@ -40,6 +42,7 @@ public class CommentController { |
40 | 42 | private final PointService pointService; |
41 | 43 | private final PostChangeLogService changeLogService; |
42 | 44 | private final PostChangeLogMapper postChangeLogMapper; |
| 45 | + private final PostMapper postMapper; |
43 | 46 |
|
44 | 47 | @Value("${app.captcha.enabled:false}") |
45 | 48 | private boolean captchaEnabled; |
@@ -184,6 +187,37 @@ public List<TimelineItemDto<?>> listComments( |
184 | 187 | return itemDtoList; |
185 | 188 | } |
186 | 189 |
|
| 190 | + @GetMapping("/comments/{commentId}/context") |
| 191 | + @Operation( |
| 192 | + summary = "Comment context", |
| 193 | + description = "Get a comment along with its previous comments and related post" |
| 194 | + ) |
| 195 | + @ApiResponse( |
| 196 | + responseCode = "200", |
| 197 | + description = "Comment context", |
| 198 | + content = @Content(schema = @Schema(implementation = CommentContextDto.class)) |
| 199 | + ) |
| 200 | + public ResponseEntity<CommentContextDto> getCommentContext(@PathVariable Long commentId) { |
| 201 | + log.debug("getCommentContext called for comment {}", commentId); |
| 202 | + Comment comment = commentService.getComment(commentId); |
| 203 | + CommentContextDto dto = new CommentContextDto(); |
| 204 | + dto.setPost(postMapper.toSummaryDto(comment.getPost())); |
| 205 | + dto.setTargetComment(commentMapper.toDtoWithReplies(comment)); |
| 206 | + dto.setPreviousComments( |
| 207 | + commentService |
| 208 | + .getCommentsBefore(comment) |
| 209 | + .stream() |
| 210 | + .map(commentMapper::toDtoWithReplies) |
| 211 | + .collect(Collectors.toList()) |
| 212 | + ); |
| 213 | + log.debug( |
| 214 | + "getCommentContext returning {} previous comments for comment {}", |
| 215 | + dto.getPreviousComments().size(), |
| 216 | + commentId |
| 217 | + ); |
| 218 | + return ResponseEntity.ok(dto); |
| 219 | + } |
| 220 | + |
187 | 221 | @DeleteMapping("/comments/{id}") |
188 | 222 | @Operation(summary = "Delete comment", description = "Delete a comment") |
189 | 223 | @ApiResponse(responseCode = "200", description = "Deleted") |
|
0 commit comments