mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-23 06:30:48 +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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,14 @@ package com.openisle.repository;
|
||||
|
||||
import com.openisle.model.Comment;
|
||||
import com.openisle.model.Post;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.openisle.model.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CommentRepository extends JpaRepository<Comment, Long> {
|
||||
List<Comment> findByPostAndParentIsNullOrderByCreatedAtAsc(Post post);
|
||||
List<Comment> findByParentOrderByCreatedAtAsc(Comment parent);
|
||||
List<Comment> findByAuthorOrderByCreatedAtDesc(User author, Pageable pageable);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@ package com.openisle.repository;
|
||||
|
||||
import com.openisle.model.Post;
|
||||
import com.openisle.model.PostStatus;
|
||||
import com.openisle.model.User;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||
List<Post> findByStatus(PostStatus status);
|
||||
List<Post> findByAuthorAndStatusOrderByCreatedAtDesc(User author, PostStatus status, Pageable pageable);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -54,4 +56,11 @@ public class CommentService {
|
||||
.orElseThrow(() -> new IllegalArgumentException("Comment not found"));
|
||||
return commentRepository.findByParentOrderByCreatedAtAsc(parent);
|
||||
}
|
||||
|
||||
public List<Comment> getRecentCommentsByUser(String username, int limit) {
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
Pageable pageable = PageRequest.of(0, limit);
|
||||
return commentRepository.findByAuthorOrderByCreatedAtDesc(user, pageable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
@Service
|
||||
public class PostService {
|
||||
@@ -59,6 +61,13 @@ public class PostService {
|
||||
return postRepository.findByStatus(PostStatus.PUBLISHED);
|
||||
}
|
||||
|
||||
public List<Post> getRecentPostsByUser(String username, int limit) {
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
Pageable pageable = PageRequest.of(0, limit);
|
||||
return postRepository.findByAuthorAndStatusOrderByCreatedAtDesc(user, PostStatus.PUBLISHED, pageable);
|
||||
}
|
||||
|
||||
public List<Post> listPendingPosts() {
|
||||
return postRepository.findByStatus(PostStatus.PENDING);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user