diff --git a/src/main/java/kr/co/conceptbe/notification/app/NotificationService.java b/src/main/java/kr/co/conceptbe/notification/app/NotificationService.java index 801e82d..49e2212 100644 --- a/src/main/java/kr/co/conceptbe/notification/app/NotificationService.java +++ b/src/main/java/kr/co/conceptbe/notification/app/NotificationService.java @@ -1,5 +1,6 @@ package kr.co.conceptbe.notification.app; +import javax.management.Notification; import kr.co.conceptbe.auth.presentation.dto.AuthCredentials; import kr.co.conceptbe.idea.domain.Idea; import kr.co.conceptbe.idea.domain.event.CreatedIdeaEvent; @@ -47,8 +48,20 @@ public void createNotificationByCreateIdeaEvent(CreatedIdeaEvent createdIdeaEven Idea idea = createdIdeaEvent.idea(); List notificationSettings = notificationSettingRepository.findAll(); - List ideaNotifications = notificationTrigger.getNotificationByCreatedIdeaEvent(idea, notificationSettings); + List ideaNotifications = notificationTrigger.getNotificationByCreatedIdeaEvent(idea, + notificationSettings); notificationRepository.saveAll(ideaNotifications); } + @Transactional + public void readNotification(AuthCredentials auth, Long notificationId) { + Long memberId = auth.id(); + IdeaNotification notification = notificationRepository.findByMemberIdAndId(memberId, notificationId) + .orElseThrow( + () -> new IllegalArgumentException("Not Found Notification ID : " + notificationId) + ); + notification.read(memberId); + notificationRepository.save(notification); + } + } diff --git a/src/main/java/kr/co/conceptbe/notification/domain/IdeaNotification.java b/src/main/java/kr/co/conceptbe/notification/domain/IdeaNotification.java index 92e5500..c917cbe 100644 --- a/src/main/java/kr/co/conceptbe/notification/domain/IdeaNotification.java +++ b/src/main/java/kr/co/conceptbe/notification/domain/IdeaNotification.java @@ -44,6 +44,9 @@ public class IdeaNotification extends BaseTimeEntity { @Column(nullable = false) private String cooperationWay; + @Column(nullable = false) + private boolean isAlreadyRead; + public static IdeaNotification withIdea(Idea idea, IdeaNotificationSetting notificationSetting) { Long receiverId = notificationSetting.getMemberId(); String title = idea.getTitle(); @@ -63,7 +66,8 @@ public static IdeaNotification withIdea(Idea idea, IdeaNotificationSetting notif idea.getId(), joinBadges(branchBadges, Function.identity()), joinBadges(purposes, purpose -> purpose.getPurpose().getName()), - cooperationWay + cooperationWay, + false ); } @@ -100,4 +104,11 @@ private List splitBadges(String badges) { return new ArrayList<>(List.of(badgesEach)); } + public void read(Long memberId) { + if (!memberId.equals(this.memberId)) { + throw new IllegalArgumentException("다른 계정의 알림을 읽을 수 없습니다."); + } + isAlreadyRead = true; + } + } diff --git a/src/main/java/kr/co/conceptbe/notification/domain/repository/NotificationRepository.java b/src/main/java/kr/co/conceptbe/notification/domain/repository/NotificationRepository.java index ed295a7..3a3e5d1 100644 --- a/src/main/java/kr/co/conceptbe/notification/domain/repository/NotificationRepository.java +++ b/src/main/java/kr/co/conceptbe/notification/domain/repository/NotificationRepository.java @@ -1,5 +1,6 @@ package kr.co.conceptbe.notification.domain.repository; +import java.util.Optional; import kr.co.conceptbe.notification.domain.IdeaNotification; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -16,4 +17,6 @@ public interface NotificationRepository extends JpaRepository findAllNotifications(Long memberId, Long cursorId, Long limit); + Optional findByMemberIdAndId(Long memberId, Long notificationId); + } diff --git a/src/main/java/kr/co/conceptbe/notification/presentation/NotificationController.java b/src/main/java/kr/co/conceptbe/notification/presentation/NotificationController.java index 0c18cd3..ee44a62 100644 --- a/src/main/java/kr/co/conceptbe/notification/presentation/NotificationController.java +++ b/src/main/java/kr/co/conceptbe/notification/presentation/NotificationController.java @@ -9,6 +9,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -24,13 +26,24 @@ public class NotificationController implements NotificationSwagger { @GetMapping public ResponseEntity> getNotifications( - @Parameter(hidden = true) @Auth AuthCredentials auth, + @Auth AuthCredentials auth, @RequestParam(required = false, defaultValue = "" + Long.MAX_VALUE) Long cursorId ) { Long limit = 10L; - List responses = notificationService.findAllNotifications(auth, cursorId, limit); + List responses = notificationService.findAllNotifications(auth, cursorId, + limit); return ResponseEntity.ok(responses); } + @PutMapping("/{notificationId}/read") + public ResponseEntity readNotification( + @Auth AuthCredentials auth, + @PathVariable Long notificationId + ) { + notificationService.readNotification(auth, notificationId); + + return ResponseEntity.ok().build(); + } + } diff --git a/src/main/java/kr/co/conceptbe/notification/presentation/swagger/NotificationSwagger.java b/src/main/java/kr/co/conceptbe/notification/presentation/swagger/NotificationSwagger.java index bbe6734..bbfb829 100644 --- a/src/main/java/kr/co/conceptbe/notification/presentation/swagger/NotificationSwagger.java +++ b/src/main/java/kr/co/conceptbe/notification/presentation/swagger/NotificationSwagger.java @@ -17,4 +17,9 @@ ResponseEntity> getNotifications( Long cursorId ); + ResponseEntity readNotification( + @Parameter(hidden = true) AuthCredentials auth, + Long notificationId + ); + }