diff --git a/src/main/java/Mua/Mua_backend/domain/notification/controller/NotificationController.java b/src/main/java/Mua/Mua_backend/domain/notification/controller/NotificationController.java index c86b4ee..a6c3f42 100644 --- a/src/main/java/Mua/Mua_backend/domain/notification/controller/NotificationController.java +++ b/src/main/java/Mua/Mua_backend/domain/notification/controller/NotificationController.java @@ -2,14 +2,11 @@ import Mua.Mua_backend.domain.member.entity.Member; import Mua.Mua_backend.domain.notification.dto.response.NotificationResponse; -import Mua.Mua_backend.domain.notification.repository.NotificationRepository; +import Mua.Mua_backend.domain.notification.service.NotificationService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -18,17 +15,24 @@ @RequestMapping("/api/notifications") public class NotificationController { - private final NotificationRepository notificationRepository; + private final NotificationService notificationService; + // 알림 목록 조회 @GetMapping public List getMyNotifications( @AuthenticationPrincipal Member member ) { - return notificationRepository - .findByMemberOrderByCreatedAtDesc(member) - .stream() - .map(NotificationResponse::from) - .toList(); + return notificationService.getMyNotifications(member); + } + + // 알림 읽음 처리 + @PatchMapping("/{notificationId}/read") + public ResponseEntity readNotification( + @PathVariable Long notificationId, + @AuthenticationPrincipal Member member + ) { + notificationService.readNotification(notificationId, member); + return ResponseEntity.ok().build(); } // 알림 전체 삭제 @@ -36,7 +40,7 @@ public List getMyNotifications( public ResponseEntity deleteMyNotifications( @AuthenticationPrincipal Member member ) { - notificationRepository.deleteByMember(member); + notificationService.deleteMyNotifications(member); return ResponseEntity.noContent().build(); } } diff --git a/src/main/java/Mua/Mua_backend/domain/notification/entity/Notification.java b/src/main/java/Mua/Mua_backend/domain/notification/entity/Notification.java index e9d82e7..2d10a79 100644 --- a/src/main/java/Mua/Mua_backend/domain/notification/entity/Notification.java +++ b/src/main/java/Mua/Mua_backend/domain/notification/entity/Notification.java @@ -40,4 +40,8 @@ public Notification(Member member, String message, this.targetId = targetId; this.read = false; } + + public void markAsRead() { + this.read = true; + } } diff --git a/src/main/java/Mua/Mua_backend/domain/notification/service/NotificationService.java b/src/main/java/Mua/Mua_backend/domain/notification/service/NotificationService.java index 187a26e..f66138d 100644 --- a/src/main/java/Mua/Mua_backend/domain/notification/service/NotificationService.java +++ b/src/main/java/Mua/Mua_backend/domain/notification/service/NotificationService.java @@ -1,6 +1,7 @@ package Mua.Mua_backend.domain.notification.service; import Mua.Mua_backend.domain.member.entity.Member; +import Mua.Mua_backend.domain.notification.dto.response.NotificationResponse; import Mua.Mua_backend.domain.notification.entity.Notification; import Mua.Mua_backend.domain.notification.entity.NotificationType; import Mua.Mua_backend.domain.notification.repository.NotificationRepository; @@ -9,6 +10,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; + @Service @RequiredArgsConstructor @Transactional @@ -17,21 +20,49 @@ public class NotificationService { private final NotificationRepository notificationRepository; private final FcmService fcmService; + // 알림 생성 + FCM 전송 public void sendNotification( Member targetMember, NotificationType type, Long targetId, String message ) { - Notification notification = new Notification( - targetMember, - message, - type, - targetId - ); + Notification notification = Notification.builder() + .member(targetMember) + .message(message) + .targetType(type) + .targetId(targetId) + .build(); notificationRepository.save(notification); fcmService.send(targetMember.getFcmToken(), message); } + + // 알림 목록 조회 + public List getMyNotifications(Member member) { + return notificationRepository + .findByMemberOrderByCreatedAtDesc(member) + .stream() + .map(NotificationResponse::from) + .toList(); + } + + // 읽음 처리 + public void readNotification(Long notificationId, Member member) { + Notification notification = notificationRepository + .findById(notificationId) + .orElseThrow(); + + if (!notification.getMember().getId().equals(member.getId())) { + throw new RuntimeException("권한 없음"); + } + + notification.markAsRead(); + } + + // 알림 전체 삭제 + public void deleteMyNotifications(Member member) { + notificationRepository.deleteByMember(member); + } }