diff --git a/src/main/java/com/openisle/controller/UserController.java b/src/main/java/com/openisle/controller/UserController.java index f4137c12f..b6d1326c8 100644 --- a/src/main/java/com/openisle/controller/UserController.java +++ b/src/main/java/com/openisle/controller/UserController.java @@ -68,75 +68,81 @@ public class UserController { return ResponseEntity.ok(toDto(user)); } - @GetMapping("/{username}") - public ResponseEntity getUser(@PathVariable String username) { - User user = userService.findByUsername(username).orElseThrow(); + @GetMapping("/{identifier}") + public ResponseEntity getUser(@PathVariable("identifier") String identifier) { + User user = userService.findByIdentifier(identifier).orElseThrow(); return ResponseEntity.ok(toDto(user)); } - @GetMapping("/{username}/posts") - public java.util.List userPosts(@PathVariable String username, + @GetMapping("/{identifier}/posts") + public java.util.List userPosts(@PathVariable("identifier") String identifier, @RequestParam(value = "limit", required = false) Integer limit) { int l = limit != null ? limit : defaultPostsLimit; - return postService.getRecentPostsByUser(username, l).stream() + User user = userService.findByIdentifier(identifier).orElseThrow(); + return postService.getRecentPostsByUser(user.getUsername(), l).stream() .map(this::toMetaDto) .collect(java.util.stream.Collectors.toList()); } - @GetMapping("/{username}/replies") - public java.util.List userReplies(@PathVariable String username, + @GetMapping("/{identifier}/replies") + public java.util.List userReplies(@PathVariable("identifier") String identifier, @RequestParam(value = "limit", required = false) Integer limit) { int l = limit != null ? limit : defaultRepliesLimit; - return commentService.getRecentCommentsByUser(username, l).stream() + User user = userService.findByIdentifier(identifier).orElseThrow(); + return commentService.getRecentCommentsByUser(user.getUsername(), l).stream() .map(this::toCommentInfoDto) .collect(java.util.stream.Collectors.toList()); } - @GetMapping("/{username}/hot-posts") - public java.util.List hotPosts(@PathVariable String username, + @GetMapping("/{identifier}/hot-posts") + public java.util.List hotPosts(@PathVariable("identifier") String identifier, @RequestParam(value = "limit", required = false) Integer limit) { int l = limit != null ? limit : 10; - java.util.List ids = reactionService.topPostIds(username, l); + User user = userService.findByIdentifier(identifier).orElseThrow(); + java.util.List ids = reactionService.topPostIds(user.getUsername(), l); return postService.getPostsByIds(ids).stream() .map(this::toMetaDto) .collect(java.util.stream.Collectors.toList()); } - @GetMapping("/{username}/hot-replies") - public java.util.List hotReplies(@PathVariable String username, + @GetMapping("/{identifier}/hot-replies") + public java.util.List hotReplies(@PathVariable("identifier") String identifier, @RequestParam(value = "limit", required = false) Integer limit) { int l = limit != null ? limit : 10; - java.util.List ids = reactionService.topCommentIds(username, l); + User user = userService.findByIdentifier(identifier).orElseThrow(); + java.util.List ids = reactionService.topCommentIds(user.getUsername(), l); return commentService.getCommentsByIds(ids).stream() .map(this::toCommentInfoDto) .collect(java.util.stream.Collectors.toList()); } - @GetMapping("/{username}/following") - public java.util.List following(@PathVariable String username) { - return subscriptionService.getSubscribedUsers(username).stream() + @GetMapping("/{identifier}/following") + public java.util.List following(@PathVariable("identifier") String identifier) { + User user = userService.findByIdentifier(identifier).orElseThrow(); + return subscriptionService.getSubscribedUsers(user.getUsername()).stream() .map(this::toDto) .collect(java.util.stream.Collectors.toList()); } - @GetMapping("/{username}/followers") - public java.util.List followers(@PathVariable String username) { - return subscriptionService.getSubscribers(username).stream() + @GetMapping("/{identifier}/followers") + public java.util.List followers(@PathVariable("identifier") String identifier) { + User user = userService.findByIdentifier(identifier).orElseThrow(); + return subscriptionService.getSubscribers(user.getUsername()).stream() .map(this::toDto) .collect(java.util.stream.Collectors.toList()); } - @GetMapping("/{username}/all") - public ResponseEntity userAggregate(@PathVariable String username, + @GetMapping("/{identifier}/all") + public ResponseEntity userAggregate(@PathVariable("identifier") String identifier, @RequestParam(value = "postsLimit", required = false) Integer postsLimit, @RequestParam(value = "repliesLimit", required = false) Integer repliesLimit) { - User user = userService.findByUsername(username).orElseThrow(); + User user = userService.findByIdentifier(identifier).orElseThrow(); int pLimit = postsLimit != null ? postsLimit : defaultPostsLimit; int rLimit = repliesLimit != null ? repliesLimit : defaultRepliesLimit; - java.util.List posts = postService.getRecentPostsByUser(username, pLimit).stream() + java.util.List posts = postService.getRecentPostsByUser(user.getUsername(), pLimit).stream() .map(this::toMetaDto) .collect(java.util.stream.Collectors.toList()); - java.util.List replies = commentService.getRecentCommentsByUser(username, rLimit).stream() + java.util.List replies = commentService.getRecentCommentsByUser(user.getUsername(), rLimit).stream() .map(this::toCommentInfoDto) .collect(java.util.stream.Collectors.toList()); UserAggregateDto dto = new UserAggregateDto(); diff --git a/src/main/java/com/openisle/service/UserService.java b/src/main/java/com/openisle/service/UserService.java index e874ec647..1d97097ba 100644 --- a/src/main/java/com/openisle/service/UserService.java +++ b/src/main/java/com/openisle/service/UserService.java @@ -91,6 +91,17 @@ public class UserService { return userRepository.findByUsername(username); } + public Optional findById(Long id) { + return userRepository.findById(id); + } + + public Optional findByIdentifier(String identifier) { + if (identifier.matches("\\d+")) { + return userRepository.findById(Long.parseLong(identifier)); + } + return userRepository.findByUsername(identifier); + } + public User updateAvatar(String username, String avatarUrl) { User user = userRepository.findByUsername(username) .orElseThrow(() -> new IllegalArgumentException("User not found"));