diff --git a/src/main/java/com/wooribound/api/individual/controller/WbUserNotificationController.java b/src/main/java/com/wooribound/api/individual/controller/WbUserNotificationController.java index 0826522..c4a92d7 100644 --- a/src/main/java/com/wooribound/api/individual/controller/WbUserNotificationController.java +++ b/src/main/java/com/wooribound/api/individual/controller/WbUserNotificationController.java @@ -2,11 +2,16 @@ import com.wooribound.domain.notification.dto.NotificationDTO; import com.wooribound.domain.notification.service.NotificationService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import java.util.List; +@Tag(name = "알림 메세지 API", description = "개인회원 서비스 중 알림 메세지 입니다.") @RestController @RequiredArgsConstructor @RequestMapping("/individual/notifications") @@ -14,13 +19,16 @@ public class WbUserNotificationController { private final NotificationService notificationService; + @Operation(summary = "알림 메세지 전체 목록 조회", description = "알림 메세지 나눔 전체 목록 조회") @GetMapping - public List getNotifications(@RequestParam String userId) { - return notificationService.getNotifications(userId); + public ResponseEntity getNotifications(Authentication authentication) { + return ResponseEntity.ok().body(notificationService.getNotifications(authentication)); } - @GetMapping("/detail") - public String getNotificationDetail(@RequestParam long notiId) { - return notificationService.getNotificationDetail(notiId); + @Operation(summary = "알림 메세지 읽음 표시 업데이트", description = "알림 메세지 읽음 표시 업데이트") + @PutMapping("/read") + public ResponseEntity readNotification(Authentication authentication, @RequestParam(value = "notiId") Long notiId) { + return ResponseEntity.ok().body(notificationService.readNotification(authentication, notiId)); } + } diff --git a/src/main/java/com/wooribound/domain/knowhow/service/WbUserKnowhowServiceImpl.java b/src/main/java/com/wooribound/domain/knowhow/service/WbUserKnowhowServiceImpl.java index 69949bb..470493e 100644 --- a/src/main/java/com/wooribound/domain/knowhow/service/WbUserKnowhowServiceImpl.java +++ b/src/main/java/com/wooribound/domain/knowhow/service/WbUserKnowhowServiceImpl.java @@ -65,7 +65,7 @@ public Long createShareKnowhow(String userId, WbUserKnowhowDTO wbUserKnowhowDTO) Long knowhowId = 1L; - Optional byUserId = wbUserRepository.findByUserId(userId); + Optional byUserId = wbUserRepository.findById(userId); Optional maxKnowhowId = knowhowRepository.getMaxKnowhowId(); if (maxKnowhowId.isPresent()) { knowhowId = maxKnowhowId.get() + 1; diff --git a/src/main/java/com/wooribound/domain/notification/NotificationRepository.java b/src/main/java/com/wooribound/domain/notification/NotificationRepository.java index b33de7e..7f6bf32 100644 --- a/src/main/java/com/wooribound/domain/notification/NotificationRepository.java +++ b/src/main/java/com/wooribound/domain/notification/NotificationRepository.java @@ -7,9 +7,8 @@ import java.util.List; import java.util.Optional; -public interface NotificationRepository extends JpaRepository { +public interface NotificationRepository extends JpaRepository { @Query("SELECT n FROM Notification n WHERE n.wbUser.userId = :userId") List findByUserId(@Param("userId") String userId); - Optional findByNotiId(long notiId); } diff --git a/src/main/java/com/wooribound/domain/notification/dto/NotificationDTO.java b/src/main/java/com/wooribound/domain/notification/dto/NotificationDTO.java index f4fe896..f2b0a8e 100644 --- a/src/main/java/com/wooribound/domain/notification/dto/NotificationDTO.java +++ b/src/main/java/com/wooribound/domain/notification/dto/NotificationDTO.java @@ -1,14 +1,18 @@ package com.wooribound.domain.notification.dto; +import com.wooribound.global.constant.YN; import lombok.*; +import java.util.Date; + @Builder -@Getter -@Setter @AllArgsConstructor @NoArgsConstructor +@Data public class NotificationDTO { - private int notiId; + private Long notiId; private String notice; + private YN isConfirmed; + private Date createdAt; } diff --git a/src/main/java/com/wooribound/domain/notification/service/NotificationService.java b/src/main/java/com/wooribound/domain/notification/service/NotificationService.java index 1063adb..efd162d 100644 --- a/src/main/java/com/wooribound/domain/notification/service/NotificationService.java +++ b/src/main/java/com/wooribound/domain/notification/service/NotificationService.java @@ -1,11 +1,13 @@ package com.wooribound.domain.notification.service; import com.wooribound.domain.notification.dto.NotificationDTO; +import org.springframework.security.core.Authentication; import java.util.List; public interface NotificationService { - List getNotifications(String userId); + List getNotifications(Authentication authentication); + + String readNotification (Authentication authentication, Long notiId); - String getNotificationDetail(long notiId); } diff --git a/src/main/java/com/wooribound/domain/notification/service/NotificationServiceImpl.java b/src/main/java/com/wooribound/domain/notification/service/NotificationServiceImpl.java index 34f02f7..70c5a44 100644 --- a/src/main/java/com/wooribound/domain/notification/service/NotificationServiceImpl.java +++ b/src/main/java/com/wooribound/domain/notification/service/NotificationServiceImpl.java @@ -4,39 +4,56 @@ import com.wooribound.domain.notification.NotificationRepository; import com.wooribound.domain.notification.dto.NotificationDTO; import com.wooribound.global.constant.YN; +import com.wooribound.global.exception.AuthenticationException; +import com.wooribound.global.exception.NotEntityException; +import com.wooribound.global.util.AuthenticateUtil; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class NotificationServiceImpl implements NotificationService { + private final AuthenticateUtil authenticateUtil; private final NotificationRepository notificationRepository; @Override - public List getNotifications(String userId) { - // userId에 해당하는 모든 알림 조회 후 DTO로 변환 + public List getNotifications(Authentication authentication) { + String userId = authenticateUtil.CheckWbUserAuthAndGetUserId(authentication); + return notificationRepository.findByUserId(userId).stream() .map(notification -> NotificationDTO.builder() - .notiId(Math.toIntExact(notification.getNotiId())) + .notiId(notification.getNotiId()) .notice(notification.getNotice()) + .isConfirmed(notification.getIsConfirmed()) + .createdAt(notification.getCreatedAt()) .build()) .collect(Collectors.toList()); } @Override - public String getNotificationDetail(long notiId) { - // 알림 상세 조회 및 is_confirmed를 Y로 변경 - Notification notification = notificationRepository.findByNotiId(notiId) - .orElseThrow(() -> new IllegalArgumentException("Notification not found with id: " + notiId)); + public String readNotification(Authentication authentication, Long notiId) { + String userId = authenticateUtil.CheckWbUserAuthAndGetUserId(authentication); + Optional byIdNotification = notificationRepository.findById(notiId); + + if (!byIdNotification.isPresent()) { + throw new NotEntityException(); + } - // is_confirmed를 Y로 변경 + if (!byIdNotification.get().getWbUser().getUserId().equals(userId)) { + throw new AuthenticationException(); + } + + Notification notification = byIdNotification.get(); notification.setIsConfirmed(YN.Y); notificationRepository.save(notification); - return notification.getNotice(); + return "Notification updated successfully"; } + } \ No newline at end of file diff --git a/src/main/java/com/wooribound/global/exception/NotEntityException.java b/src/main/java/com/wooribound/global/exception/NotEntityException.java new file mode 100644 index 0000000..51e4b80 --- /dev/null +++ b/src/main/java/com/wooribound/global/exception/NotEntityException.java @@ -0,0 +1,5 @@ +package com.wooribound.global.exception; + +public class NotEntityException extends RuntimeException{ + public NotEntityException() {super("존재하는 엔티티가 없습니다.");} +}