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,19 +1,14 @@
package com.openisle.controller;
import com.openisle.dto.AuthorDto;
import com.openisle.dto.CommentDto;
import com.openisle.dto.NotificationDto;
import com.openisle.dto.NotificationMarkReadRequest;
import com.openisle.dto.NotificationUnreadCountDto;
import com.openisle.dto.PostSummaryDto;
import com.openisle.model.Comment;
import com.openisle.model.Notification;
import com.openisle.mapper.NotificationMapper;
import com.openisle.service.NotificationService;
import lombok.RequiredArgsConstructor;
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;
@@ -23,12 +18,13 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class NotificationController {
private final NotificationService notificationService;
private final NotificationMapper notificationMapper;
@GetMapping
public List<NotificationDto> list(@RequestParam(value = "read", required = false) Boolean read,
Authentication auth) {
return notificationService.listNotifications(auth.getName(), read).stream()
.map(this::toDto)
.map(notificationMapper::toDto)
.collect(Collectors.toList());
}
@@ -44,51 +40,4 @@ public class NotificationController {
public void markRead(@RequestBody NotificationMarkReadRequest req, Authentication auth) {
notificationService.markRead(auth.getName(), req.getIds());
}
private NotificationDto toDto(Notification n) {
NotificationDto dto = new NotificationDto();
dto.setId(n.getId());
dto.setType(n.getType());
if (n.getPost() != null) {
PostSummaryDto postDto = new PostSummaryDto();
postDto.setId(n.getPost().getId());
postDto.setTitle(n.getPost().getTitle());
dto.setPost(postDto);
}
if (n.getComment() != null) {
dto.setComment(toCommentDto(n.getComment()));
Comment parent = n.getComment().getParent();
if (parent != null) {
dto.setParentComment(toCommentDto(parent));
}
}
if (n.getFromUser() != null) {
AuthorDto author = new AuthorDto();
author.setId(n.getFromUser().getId());
author.setUsername(n.getFromUser().getUsername());
author.setAvatar(n.getFromUser().getAvatar());
dto.setFromUser(author);
}
if (n.getReactionType() != null) {
dto.setReactionType(n.getReactionType());
}
dto.setApproved(n.getApproved());
dto.setContent(n.getContent());
dto.setRead(n.isRead());
dto.setCreatedAt(n.getCreatedAt());
return dto;
}
private CommentDto toCommentDto(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);
return dto;
}
}