mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 22:21:09 +08:00
Add user content endpoints
This commit is contained in:
@@ -3,6 +3,8 @@ package com.openisle.controller;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.service.ImageUploader;
|
||||
import com.openisle.service.UserService;
|
||||
import com.openisle.service.PostService;
|
||||
import com.openisle.service.CommentService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -20,6 +22,8 @@ import java.util.Map;
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
private final ImageUploader imageUploader;
|
||||
private final PostService postService;
|
||||
private final CommentService commentService;
|
||||
|
||||
@Value("${app.upload.check-type:true}")
|
||||
private boolean checkImageType;
|
||||
@@ -27,6 +31,12 @@ public class UserController {
|
||||
@Value("${app.upload.max-size:5242880}")
|
||||
private long maxUploadSize;
|
||||
|
||||
@Value("${app.user.posts-limit:10}")
|
||||
private int defaultPostsLimit;
|
||||
|
||||
@Value("${app.user.replies-limit:50}")
|
||||
private int defaultRepliesLimit;
|
||||
|
||||
@GetMapping("/me")
|
||||
public ResponseEntity<UserDto> me(Authentication auth) {
|
||||
User user = userService.findByUsername(auth.getName()).orElseThrow();
|
||||
@@ -52,6 +62,30 @@ public class UserController {
|
||||
return ResponseEntity.ok(Map.of("url", url));
|
||||
}
|
||||
|
||||
@GetMapping("/{username}")
|
||||
public ResponseEntity<UserDto> getUser(@PathVariable String username) {
|
||||
User user = userService.findByUsername(username).orElseThrow();
|
||||
return ResponseEntity.ok(toDto(user));
|
||||
}
|
||||
|
||||
@GetMapping("/{username}/posts")
|
||||
public java.util.List<PostMetaDto> userPosts(@PathVariable String username,
|
||||
@RequestParam(value = "limit", required = false) Integer limit) {
|
||||
int l = limit != null ? limit : defaultPostsLimit;
|
||||
return postService.getRecentPostsByUser(username, l).stream()
|
||||
.map(this::toMetaDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@GetMapping("/{username}/replies")
|
||||
public java.util.List<CommentInfoDto> userReplies(@PathVariable String username,
|
||||
@RequestParam(value = "limit", required = false) Integer limit) {
|
||||
int l = limit != null ? limit : defaultRepliesLimit;
|
||||
return commentService.getRecentCommentsByUser(username, l).stream()
|
||||
.map(this::toCommentInfoDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
private UserDto toDto(User user) {
|
||||
UserDto dto = new UserDto();
|
||||
dto.setId(user.getId());
|
||||
@@ -61,6 +95,25 @@ public class UserController {
|
||||
return dto;
|
||||
}
|
||||
|
||||
private PostMetaDto toMetaDto(com.openisle.model.Post post) {
|
||||
PostMetaDto dto = new PostMetaDto();
|
||||
dto.setId(post.getId());
|
||||
dto.setTitle(post.getTitle());
|
||||
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.setPostId(comment.getPost().getId());
|
||||
return dto;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class UserDto {
|
||||
private Long id;
|
||||
@@ -68,4 +121,21 @@ public class UserController {
|
||||
private String email;
|
||||
private String avatar;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class PostMetaDto {
|
||||
private Long id;
|
||||
private String title;
|
||||
private java.time.LocalDateTime createdAt;
|
||||
private String category;
|
||||
private long views;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class CommentInfoDto {
|
||||
private Long id;
|
||||
private String content;
|
||||
private java.time.LocalDateTime createdAt;
|
||||
private Long postId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user