feat: add dedicated mappers

This commit is contained in:
Tim
2025-08-04 21:10:09 +08:00
parent 2db998a9d9
commit e2e5942941
17 changed files with 374 additions and 408 deletions

View File

@@ -1,15 +1,11 @@
package com.openisle.controller;
import com.openisle.dto.AuthorDto;
import com.openisle.dto.CommentDto;
import com.openisle.dto.CommentRequest;
import com.openisle.dto.ReactionDto;
import com.openisle.model.Comment;
import com.openisle.model.CommentSort;
import com.openisle.mapper.CommentMapper;
import com.openisle.service.CaptchaService;
import com.openisle.service.CommentService;
import com.openisle.service.LevelService;
import com.openisle.service.ReactionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
@@ -17,7 +13,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@@ -29,7 +24,7 @@ public class CommentController {
private final CommentService commentService;
private final LevelService levelService;
private final CaptchaService captchaService;
private final ReactionService reactionService;
private final CommentMapper commentMapper;
@Value("${app.captcha.enabled:false}")
private boolean captchaEnabled;
@@ -47,7 +42,7 @@ public class CommentController {
return ResponseEntity.badRequest().build();
}
Comment comment = commentService.addComment(auth.getName(), postId, req.getContent());
CommentDto dto = toDto(comment);
CommentDto dto = commentMapper.toDto(comment);
dto.setReward(levelService.awardForComment(auth.getName()));
log.debug("createComment succeeded for comment {}", comment.getId());
return ResponseEntity.ok(dto);
@@ -63,7 +58,7 @@ public class CommentController {
return ResponseEntity.badRequest().build();
}
Comment comment = commentService.addReply(auth.getName(), commentId, req.getContent());
CommentDto dto = toDto(comment);
CommentDto dto = commentMapper.toDto(comment);
dto.setReward(levelService.awardForComment(auth.getName()));
log.debug("replyComment succeeded for comment {}", comment.getId());
return ResponseEntity.ok(dto);
@@ -71,61 +66,19 @@ public class CommentController {
@GetMapping("/posts/{postId}/comments")
public List<CommentDto> listComments(@PathVariable Long postId,
@RequestParam(value = "sort", required = false, defaultValue = "OLDEST") CommentSort sort) {
@RequestParam(value = "sort", required = false, defaultValue = "OLDEST") com.openisle.model.CommentSort sort) {
log.debug("listComments called for post {} with sort {}", postId, sort);
List<CommentDto> list = commentService.getCommentsForPost(postId, sort).stream()
.map(this::toDtoWithReplies)
.map(commentMapper::toDtoWithReplies)
.collect(Collectors.toList());
log.debug("listComments returning {} comments", list.size());
return list;
}
private CommentDto toDtoWithReplies(Comment comment) {
CommentDto dto = toDto(comment);
List<CommentDto> replies = commentService.getReplies(comment.getId()).stream()
.map(this::toDtoWithReplies)
.collect(Collectors.toList());
dto.setReplies(replies);
List<ReactionDto> reactions = reactionService.getReactionsForComment(comment.getId()).stream()
.map(this::toReactionDto)
.collect(Collectors.toList());
dto.setReactions(reactions);
return dto;
}
@DeleteMapping("/comments/{id}")
public void deleteComment(@PathVariable Long id, Authentication auth) {
log.debug("deleteComment called by user {} for comment {}", auth.getName(), id);
commentService.deleteComment(auth.getName(), id);
log.debug("deleteComment completed for comment {}", id);
}
private CommentDto toDto(Comment comment) {
CommentDto dto = new CommentDto();
dto.setId(comment.getId());
dto.setContent(comment.getContent());
dto.setCreatedAt(comment.getCreatedAt());
AuthorDto author = new AuthorDto();
author.setId(comment.getAuthor().getId());
author.setUsername(comment.getAuthor().getUsername());
author.setAvatar(comment.getAuthor().getAvatar());
dto.setAuthor(author);
dto.setReward(0);
return dto;
}
private ReactionDto toReactionDto(com.openisle.model.Reaction reaction) {
ReactionDto dto = new ReactionDto();
dto.setId(reaction.getId());
dto.setType(reaction.getType());
dto.setUser(reaction.getUser().getUsername());
if (reaction.getPost() != null) {
dto.setPostId(reaction.getPost().getId());
}
if (reaction.getComment() != null) {
dto.setCommentId(reaction.getComment().getId());
}
dto.setReward(0);
return dto;
}
}