You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
3.4 KiB
72 lines
3.4 KiB
package com.mydreamjob.notification;
|
|
|
|
import com.mydreamjob.common.api.ApiResponse;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Tag(name = "通知", description = "站内通知查询、已读管理")
|
|
@RestController
|
|
@RequestMapping("/notifications")
|
|
@RequiredArgsConstructor
|
|
public class NotificationController {
|
|
|
|
/** R01-A3:身份只来自安全上下文,不接受请求参数传入的 userId。 */
|
|
private final com.mydreamjob.security.CurrentUserAccessor currentUserAccessor;
|
|
/** R01-A3:requestId 只来自过滤器规范化后的唯一产出点。 */
|
|
private final com.mydreamjob.common.logging.RequestIdAccessor requestIdAccessor;
|
|
|
|
private final NotificationService notificationService;
|
|
|
|
@Operation(summary = "通知列表", description = "当前用户的通知,分页,按时间倒序")
|
|
@GetMapping
|
|
public ResponseEntity<ApiResponse<Map<String, Object>>> list(
|
|
@RequestParam(defaultValue = "0") int page,
|
|
@RequestParam(defaultValue = "20") int size,
|
|
HttpServletRequest request) {
|
|
Long userId = currentUserAccessor.requireUserId();
|
|
Page<Notification> result = notificationService.list(userId, page, size);
|
|
Map<String, Object> data = new HashMap<>();
|
|
data.put("items", result.getContent().stream().map(notificationService::toView).collect(Collectors.toList()));
|
|
data.put("total", result.getTotalElements());
|
|
data.put("page", result.getNumber());
|
|
data.put("size", result.getSize());
|
|
data.put("totalPages", result.getTotalPages());
|
|
return ResponseEntity.ok(ApiResponse.ok(data, requestIdAccessor.get(request)));
|
|
}
|
|
|
|
@Operation(summary = "标记已读", description = "标记单条通知已读(幂等)")
|
|
@PostMapping("/{id}/read")
|
|
public ResponseEntity<ApiResponse<Map<String, Object>>> markRead(
|
|
@PathVariable Long id,
|
|
HttpServletRequest request) {
|
|
Long userId = currentUserAccessor.requireUserId();
|
|
notificationService.markRead(userId, id);
|
|
return ResponseEntity.ok(ApiResponse.ok(Map.of("message", "marked as read"), requestIdAccessor.get(request)));
|
|
}
|
|
|
|
@Operation(summary = "全部已读", description = "标记当前用户全部通知已读(幂等)")
|
|
@PostMapping("/read-all")
|
|
public ResponseEntity<ApiResponse<Map<String, Object>>> markAllRead(HttpServletRequest request) {
|
|
Long userId = currentUserAccessor.requireUserId();
|
|
int marked = notificationService.markAllRead(userId);
|
|
return ResponseEntity.ok(ApiResponse.ok(Map.of("marked", marked), requestIdAccessor.get(request)));
|
|
}
|
|
|
|
@Operation(summary = "未读数", description = "当前用户未读通知数")
|
|
@GetMapping("/unread-count")
|
|
public ResponseEntity<ApiResponse<Map<String, Object>>> unreadCount(HttpServletRequest request) {
|
|
Long userId = currentUserAccessor.requireUserId();
|
|
long count = notificationService.unreadCount(userId);
|
|
return ResponseEntity.ok(ApiResponse.ok(Map.of("unreadCount", count), requestIdAccessor.get(request)));
|
|
}
|
|
|
|
}
|
|
|