NotificationController.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package space.anyi.serve.controller;
  2. import io.swagger.v3.oas.annotations.Operation;
  3. import io.swagger.v3.oas.annotations.tags.Tag;
  4. import org.springframework.security.access.prepost.PreAuthorize;
  5. import org.springframework.security.core.Authentication;
  6. import org.springframework.web.bind.annotation.*;
  7. import space.anyi.serve.entity.Response;
  8. import space.anyi.serve.entity.auth.JwtUserDetails;
  9. import space.anyi.serve.entity.notification.NotificationVo;
  10. import space.anyi.serve.entity.notification.UnreadCountVo;
  11. import space.anyi.serve.service.NotificationService;
  12. import java.util.List;
  13. @Tag(name = "NotificationController", description = "系统通知")
  14. @RestController
  15. @RequestMapping("notifications")
  16. public class NotificationController {
  17. private final NotificationService notificationService;
  18. public NotificationController(NotificationService notificationService) {
  19. this.notificationService = notificationService;
  20. }
  21. @Operation(summary = "获取通知列表")
  22. @PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')")
  23. @GetMapping
  24. public Response<List<NotificationVo>> listNotifications(Authentication authentication) {
  25. JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
  26. Long userId = details.getUser().getId();
  27. return Response.ok(notificationService.listNotifications(userId));
  28. }
  29. @Operation(summary = "获取未读数")
  30. @PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')")
  31. @GetMapping("unread-count")
  32. public Response<UnreadCountVo> getUnreadCount(Authentication authentication) {
  33. JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
  34. Long userId = details.getUser().getId();
  35. long count = notificationService.getUnreadCount(userId);
  36. return Response.ok(new UnreadCountVo(count));
  37. }
  38. @Operation(summary = "标记已读")
  39. @PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')")
  40. @PutMapping("{id}/read")
  41. public Response<Void> markRead(@PathVariable Long id, Authentication authentication) {
  42. JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
  43. Long userId = details.getUser().getId();
  44. notificationService.markRead(id, userId);
  45. return Response.ok();
  46. }
  47. }