mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-10 00:51:00 +08:00
feat: add dedicated mappers
This commit is contained in:
@@ -1,17 +1,13 @@
|
||||
package com.openisle.controller;
|
||||
|
||||
import com.openisle.dto.CommentInfoDto;
|
||||
import com.openisle.dto.ParentCommentDto;
|
||||
import com.openisle.dto.PostMetaDto;
|
||||
import com.openisle.dto.TagDto;
|
||||
import com.openisle.dto.UpdateProfileDto;
|
||||
import com.openisle.dto.UserAggregateDto;
|
||||
import com.openisle.dto.UserDto;
|
||||
import com.openisle.dto.*;
|
||||
import com.openisle.exception.NotFoundException;
|
||||
import com.openisle.mapper.TagMapper;
|
||||
import com.openisle.mapper.UserMapper;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.service.*;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -31,10 +27,10 @@ public class UserController {
|
||||
private final ReactionService reactionService;
|
||||
private final TagService tagService;
|
||||
private final SubscriptionService subscriptionService;
|
||||
private final PostReadService postReadService;
|
||||
private final UserVisitService userVisitService;
|
||||
private final LevelService levelService;
|
||||
private final JwtService jwtService;
|
||||
private final UserMapper userMapper;
|
||||
private final TagMapper tagMapper;
|
||||
|
||||
@Value("${app.upload.check-type:true}")
|
||||
private boolean checkImageType;
|
||||
@@ -51,13 +47,10 @@ public class UserController {
|
||||
@Value("${app.user.tags-limit:50}")
|
||||
private int defaultTagsLimit;
|
||||
|
||||
@Value("${app.snippet-length:50}")
|
||||
private int snippetLength;
|
||||
|
||||
@GetMapping("/me")
|
||||
public ResponseEntity<UserDto> me(Authentication auth) {
|
||||
User user = userService.findByUsername(auth.getName()).orElseThrow();
|
||||
return ResponseEntity.ok(toDto(user, auth));
|
||||
return ResponseEntity.ok(userMapper.toDto(user, auth));
|
||||
}
|
||||
|
||||
@PostMapping("/me/avatar")
|
||||
@@ -85,7 +78,7 @@ public class UserController {
|
||||
User user = userService.updateProfile(auth.getName(), dto.getUsername(), dto.getIntroduction());
|
||||
return ResponseEntity.ok(Map.of(
|
||||
"token", jwtService.generateToken(user.getUsername()),
|
||||
"user", toDto(user, auth)
|
||||
"user", userMapper.toDto(user, auth)
|
||||
));
|
||||
}
|
||||
|
||||
@@ -99,7 +92,7 @@ public class UserController {
|
||||
public ResponseEntity<UserDto> getUser(@PathVariable("identifier") String identifier,
|
||||
Authentication auth) {
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow(() -> new NotFoundException("User not found"));
|
||||
return ResponseEntity.ok(toDto(user, auth));
|
||||
return ResponseEntity.ok(userMapper.toDto(user, auth));
|
||||
}
|
||||
|
||||
@GetMapping("/{identifier}/posts")
|
||||
@@ -108,7 +101,7 @@ public class UserController {
|
||||
int l = limit != null ? limit : defaultPostsLimit;
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
return postService.getRecentPostsByUser(user.getUsername(), l).stream()
|
||||
.map(this::toMetaDto)
|
||||
.map(userMapper::toMetaDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -118,7 +111,7 @@ public class UserController {
|
||||
int l = limit != null ? limit : defaultRepliesLimit;
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
return commentService.getRecentCommentsByUser(user.getUsername(), l).stream()
|
||||
.map(this::toCommentInfoDto)
|
||||
.map(userMapper::toCommentInfoDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -129,7 +122,7 @@ public class UserController {
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
java.util.List<Long> ids = reactionService.topPostIds(user.getUsername(), l);
|
||||
return postService.getPostsByIds(ids).stream()
|
||||
.map(this::toMetaDto)
|
||||
.map(userMapper::toMetaDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -140,7 +133,7 @@ public class UserController {
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
java.util.List<Long> ids = reactionService.topCommentIds(user.getUsername(), l);
|
||||
return commentService.getCommentsByIds(ids).stream()
|
||||
.map(this::toCommentInfoDto)
|
||||
.map(userMapper::toCommentInfoDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -150,17 +143,7 @@ public class UserController {
|
||||
int l = limit != null ? limit : 10;
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
return tagService.getTagsByUser(user.getUsername()).stream()
|
||||
.map(t -> {
|
||||
TagDto dto = new TagDto();
|
||||
dto.setId(t.getId());
|
||||
dto.setName(t.getName());
|
||||
dto.setDescription(t.getDescription());
|
||||
dto.setIcon(t.getIcon());
|
||||
dto.setSmallIcon(t.getSmallIcon());
|
||||
dto.setCreatedAt(t.getCreatedAt());
|
||||
dto.setCount(postService.countPostsByTag(t.getId()));
|
||||
return dto;
|
||||
})
|
||||
.map(t -> tagMapper.toDto(t, postService.countPostsByTag(t.getId())))
|
||||
.sorted((a, b) -> Long.compare(b.getCount(), a.getCount()))
|
||||
.limit(l)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
@@ -172,17 +155,7 @@ public class UserController {
|
||||
int l = limit != null ? limit : defaultTagsLimit;
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
return tagService.getRecentTagsByUser(user.getUsername(), l).stream()
|
||||
.map(t -> {
|
||||
TagDto dto = new TagDto();
|
||||
dto.setId(t.getId());
|
||||
dto.setName(t.getName());
|
||||
dto.setDescription(t.getDescription());
|
||||
dto.setIcon(t.getIcon());
|
||||
dto.setSmallIcon(t.getSmallIcon());
|
||||
dto.setCreatedAt(t.getCreatedAt());
|
||||
dto.setCount(postService.countPostsByTag(t.getId()));
|
||||
return dto;
|
||||
})
|
||||
.map(t -> tagMapper.toDto(t, postService.countPostsByTag(t.getId())))
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -190,7 +163,7 @@ public class UserController {
|
||||
public java.util.List<UserDto> following(@PathVariable("identifier") String identifier) {
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
return subscriptionService.getSubscribedUsers(user.getUsername()).stream()
|
||||
.map(this::toDto)
|
||||
.map(userMapper::toDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -198,17 +171,14 @@ public class UserController {
|
||||
public java.util.List<UserDto> followers(@PathVariable("identifier") String identifier) {
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
return subscriptionService.getSubscribers(user.getUsername()).stream()
|
||||
.map(this::toDto)
|
||||
.map(userMapper::toDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* List all administrator users.
|
||||
*/
|
||||
@GetMapping("/admins")
|
||||
public java.util.List<UserDto> admins() {
|
||||
return userService.getAdmins().stream()
|
||||
.map(this::toDto)
|
||||
.map(userMapper::toDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -221,83 +191,15 @@ public class UserController {
|
||||
int pLimit = postsLimit != null ? postsLimit : defaultPostsLimit;
|
||||
int rLimit = repliesLimit != null ? repliesLimit : defaultRepliesLimit;
|
||||
java.util.List<PostMetaDto> posts = postService.getRecentPostsByUser(user.getUsername(), pLimit).stream()
|
||||
.map(this::toMetaDto)
|
||||
.map(userMapper::toMetaDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
java.util.List<CommentInfoDto> replies = commentService.getRecentCommentsByUser(user.getUsername(), rLimit).stream()
|
||||
.map(this::toCommentInfoDto)
|
||||
.map(userMapper::toCommentInfoDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
UserAggregateDto dto = new UserAggregateDto();
|
||||
dto.setUser(toDto(user, auth));
|
||||
dto.setUser(userMapper.toDto(user, auth));
|
||||
dto.setPosts(posts);
|
||||
dto.setReplies(replies);
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
private UserDto toDto(User user, Authentication viewer) {
|
||||
UserDto dto = new UserDto();
|
||||
dto.setId(user.getId());
|
||||
dto.setUsername(user.getUsername());
|
||||
dto.setEmail(user.getEmail());
|
||||
dto.setAvatar(user.getAvatar());
|
||||
dto.setRole(user.getRole().name());
|
||||
dto.setIntroduction(user.getIntroduction());
|
||||
dto.setFollowers(subscriptionService.countSubscribers(user.getUsername()));
|
||||
dto.setFollowing(subscriptionService.countSubscribed(user.getUsername()));
|
||||
dto.setCreatedAt(user.getCreatedAt());
|
||||
dto.setLastPostTime(postService.getLastPostTime(user.getUsername()));
|
||||
dto.setLastCommentTime(commentService.getLastCommentTimeOfUserByUserId(user.getId()));
|
||||
dto.setTotalViews(postService.getTotalViews(user.getUsername()));
|
||||
dto.setVisitedDays(userVisitService.countVisits(user.getUsername()));
|
||||
dto.setReadPosts(postReadService.countReads(user.getUsername()));
|
||||
dto.setLikesSent(reactionService.countLikesSent(user.getUsername()));
|
||||
dto.setLikesReceived(reactionService.countLikesReceived(user.getUsername()));
|
||||
dto.setExperience(user.getExperience());
|
||||
dto.setCurrentLevel(levelService.getLevel(user.getExperience()));
|
||||
dto.setNextLevelExp(levelService.nextLevelExp(user.getExperience()));
|
||||
if (viewer != null) {
|
||||
dto.setSubscribed(subscriptionService.isSubscribed(viewer.getName(), user.getUsername()));
|
||||
} else {
|
||||
dto.setSubscribed(false);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
private UserDto toDto(User user) {
|
||||
return toDto(user, null);
|
||||
}
|
||||
|
||||
private PostMetaDto toMetaDto(com.openisle.model.Post post) {
|
||||
PostMetaDto dto = new PostMetaDto();
|
||||
dto.setId(post.getId());
|
||||
dto.setTitle(post.getTitle());
|
||||
String content = post.getContent();
|
||||
if (content == null) {
|
||||
content = "";
|
||||
}
|
||||
if (snippetLength >= 0) {
|
||||
dto.setSnippet(content.length() > snippetLength ? content.substring(0, snippetLength) : content);
|
||||
} else {
|
||||
dto.setSnippet(content);
|
||||
}
|
||||
dto.setCreatedAt(post.getCreatedAt());
|
||||
dto.setCategory(post.getCategory().getName());
|
||||
dto.setViews(post.getViews());
|
||||
return dto;
|
||||
}
|
||||
|
||||
private CommentInfoDto toCommentInfoDto(com.openisle.model.Comment comment) {
|
||||
CommentInfoDto dto = new CommentInfoDto();
|
||||
dto.setId(comment.getId());
|
||||
dto.setContent(comment.getContent());
|
||||
dto.setCreatedAt(comment.getCreatedAt());
|
||||
dto.setPost(toMetaDto(comment.getPost()));
|
||||
if (comment.getParent() != null) {
|
||||
ParentCommentDto pc = new ParentCommentDto();
|
||||
pc.setId(comment.getParent().getId());
|
||||
pc.setAuthor(comment.getParent().getAuthor().getUsername());
|
||||
pc.setContent(comment.getParent().getContent());
|
||||
dto.setParentComment(pc);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user