From 87be252af8b8c345157b29c0d4dafaeb3ce179ad Mon Sep 17 00:00:00 2001 From: jae-2024 Date: Mon, 16 Mar 2026 11:18:36 +0900 Subject: [PATCH] =?UTF-8?q?[#71]=20Refactor:=20=EC=95=8C=EB=A6=BC=20?= =?UTF-8?q?=EC=9D=BD=EC=9D=8C=EC=B2=98=EB=A6=AC=20&=20=EA=B3=84=EC=B8=B5?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/NotificationController.java | 28 ++++++------ .../notification/entity/Notification.java | 4 ++ .../service/NotificationService.java | 43 ++++++++++++++++--- 3 files changed, 57 insertions(+), 18 deletions(-) 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); + } }