Add subscription feature

This commit is contained in:
Tim
2025-07-02 18:17:32 +08:00
parent 6e9cd7f450
commit 47fc78a603
12 changed files with 337 additions and 2 deletions

View File

@@ -0,0 +1,44 @@
package com.openisle.controller;
import com.openisle.service.SubscriptionService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
/** Endpoints for subscribing to posts, comments and users. */
@RestController
@RequestMapping("/api/subscriptions")
@RequiredArgsConstructor
public class SubscriptionController {
private final SubscriptionService subscriptionService;
@PostMapping("/posts/{postId}")
public void subscribePost(@PathVariable Long postId, Authentication auth) {
subscriptionService.subscribePost(auth.getName(), postId);
}
@DeleteMapping("/posts/{postId}")
public void unsubscribePost(@PathVariable Long postId, Authentication auth) {
subscriptionService.unsubscribePost(auth.getName(), postId);
}
@PostMapping("/comments/{commentId}")
public void subscribeComment(@PathVariable Long commentId, Authentication auth) {
subscriptionService.subscribeComment(auth.getName(), commentId);
}
@DeleteMapping("/comments/{commentId}")
public void unsubscribeComment(@PathVariable Long commentId, Authentication auth) {
subscriptionService.unsubscribeComment(auth.getName(), commentId);
}
@PostMapping("/users/{username}")
public void subscribeUser(@PathVariable String username, Authentication auth) {
subscriptionService.subscribeUser(auth.getName(), username);
}
@DeleteMapping("/users/{username}")
public void unsubscribeUser(@PathVariable String username, Authentication auth) {
subscriptionService.unsubscribeUser(auth.getName(), username);
}
}

View File

@@ -5,6 +5,7 @@ import com.openisle.service.ImageUploader;
import com.openisle.service.UserService;
import com.openisle.service.PostService;
import com.openisle.service.CommentService;
import com.openisle.service.SubscriptionService;
import org.springframework.beans.factory.annotation.Value;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@@ -24,6 +25,7 @@ public class UserController {
private final ImageUploader imageUploader;
private final PostService postService;
private final CommentService commentService;
private final SubscriptionService subscriptionService;
@Value("${app.upload.check-type:true}")
private boolean checkImageType;
@@ -86,6 +88,20 @@ public class UserController {
.collect(java.util.stream.Collectors.toList());
}
@GetMapping("/{username}/following")
public java.util.List<UserDto> following(@PathVariable String username) {
return subscriptionService.getSubscribedUsers(username).stream()
.map(this::toDto)
.collect(java.util.stream.Collectors.toList());
}
@GetMapping("/{username}/followers")
public java.util.List<UserDto> followers(@PathVariable String username) {
return subscriptionService.getSubscribers(username).stream()
.map(this::toDto)
.collect(java.util.stream.Collectors.toList());
}
@GetMapping("/{username}/all")
public ResponseEntity<UserAggregateDto> userAggregate(@PathVariable String username,
@RequestParam(value = "postsLimit", required = false) Integer postsLimit,
@@ -112,6 +128,8 @@ public class UserController {
dto.setUsername(user.getUsername());
dto.setEmail(user.getEmail());
dto.setAvatar(user.getAvatar());
dto.setFollowers(subscriptionService.countSubscribers(user.getUsername()));
dto.setFollowing(subscriptionService.countSubscribed(user.getUsername()));
return dto;
}
@@ -140,6 +158,8 @@ public class UserController {
private String username;
private String email;
private String avatar;
private long followers;
private long following;
}
@Data