From 4c9709ab363d06830a18182b0556de25fca88fb9 Mon Sep 17 00:00:00 2001 From: HaLin Kim Date: Wed, 4 Oct 2023 20:34:53 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20notification=20=EC=97=94=ED=8B=B0?= =?UTF-8?q?=ED=8B=B0=EC=97=90=20FetchType=20=EB=AA=85=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/domain/notification/domain/Notification.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/com/graphy/backend/domain/notification/domain/Notification.java b/backend/src/main/java/com/graphy/backend/domain/notification/domain/Notification.java index 90eafdb2..1b62b649 100644 --- a/backend/src/main/java/com/graphy/backend/domain/notification/domain/Notification.java +++ b/backend/src/main/java/com/graphy/backend/domain/notification/domain/Notification.java @@ -7,6 +7,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; import javax.persistence.*; @@ -15,6 +16,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor +@Where(clause = "is_deleted = false") @SQLDelete(sql = "UPDATE notification SET is_deleted = true WHERE notification_id = ?") public class Notification extends BaseEntity { @@ -23,7 +25,7 @@ public class Notification extends BaseEntity { @Column(name = "notification_id") private Long id; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id", nullable = false) private Member member; From 5b1e8fc708f4f09d247fc5334fc9ad88854ae3ee Mon Sep 17 00:00:00 2001 From: HaLin Kim Date: Wed, 4 Oct 2023 20:34:30 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EC=95=8C=EB=A6=BC=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20API=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/NotificationController.java | 34 +++++++++++++++++++ .../repository/NotificationRepository.java | 4 ++- .../service/NotificationService.java | 13 +++++++ .../backend/global/result/ResultCode.java | 5 ++- 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 backend/src/main/java/com/graphy/backend/domain/notification/controller/NotificationController.java diff --git a/backend/src/main/java/com/graphy/backend/domain/notification/controller/NotificationController.java b/backend/src/main/java/com/graphy/backend/domain/notification/controller/NotificationController.java new file mode 100644 index 00000000..695f0232 --- /dev/null +++ b/backend/src/main/java/com/graphy/backend/domain/notification/controller/NotificationController.java @@ -0,0 +1,34 @@ +package com.graphy.backend.domain.notification.controller; + +import com.graphy.backend.domain.auth.util.annotation.CurrentUser; +import com.graphy.backend.domain.member.domain.Member; +import com.graphy.backend.domain.notification.dto.response.GetNotificationResponse; +import com.graphy.backend.domain.notification.service.NotificationService; +import com.graphy.backend.global.common.PageRequest; +import com.graphy.backend.global.result.ResultCode; +import com.graphy.backend.global.result.ResultResponse; +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.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@Tag(name = "NotificationController", description = "알림 관련 API") +@RestController +@RequestMapping("/api/v1/notifications") +@RequiredArgsConstructor +public class NotificationController { + + private final NotificationService notificationService; + + @Operation(summary = "findNotificationList", description = "알림 목록 조회") + @GetMapping + public ResponseEntity notificationList(PageRequest pageRequest, @CurrentUser Member loginUser) { + List result = notificationService.findNotificationList(pageRequest, loginUser); + return ResponseEntity.ok(ResultResponse.of(ResultCode.NOTIFICATION_PAGING_GET_SUCCESS, result)); + } +} diff --git a/backend/src/main/java/com/graphy/backend/domain/notification/repository/NotificationRepository.java b/backend/src/main/java/com/graphy/backend/domain/notification/repository/NotificationRepository.java index 72c5f814..620e7c64 100644 --- a/backend/src/main/java/com/graphy/backend/domain/notification/repository/NotificationRepository.java +++ b/backend/src/main/java/com/graphy/backend/domain/notification/repository/NotificationRepository.java @@ -1,8 +1,10 @@ package com.graphy.backend.domain.notification.repository; import com.graphy.backend.domain.notification.domain.Notification; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; public interface NotificationRepository extends JpaRepository { - + Page findAllByMemberId(Long memberId, Pageable pageable); } diff --git a/backend/src/main/java/com/graphy/backend/domain/notification/service/NotificationService.java b/backend/src/main/java/com/graphy/backend/domain/notification/service/NotificationService.java index 712441f9..12aa204d 100644 --- a/backend/src/main/java/com/graphy/backend/domain/notification/service/NotificationService.java +++ b/backend/src/main/java/com/graphy/backend/domain/notification/service/NotificationService.java @@ -4,14 +4,19 @@ import com.graphy.backend.domain.member.service.MemberService; import com.graphy.backend.domain.notification.domain.Notification; import com.graphy.backend.domain.notification.dto.NotificationDto; +import com.graphy.backend.domain.notification.dto.response.GetNotificationResponse; import com.graphy.backend.domain.notification.repository.NotificationRepository; +import com.graphy.backend.global.common.PageRequest; import com.graphy.backend.global.error.exception.BusinessException; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.mail.MessagingException; +import java.util.List; + import static com.graphy.backend.global.error.ErrorCode.SEND_EMAIL_FAIL; @RequiredArgsConstructor @@ -33,4 +38,12 @@ public void addNotification(NotificationDto dto, Long memberId) { throw new BusinessException(SEND_EMAIL_FAIL); } } + + public List findNotificationList(PageRequest pageRequest, Member loginUser) { + memberService.findMemberById(loginUser.getId()); + Page result + = notificationRepository.findAllByMemberId(loginUser.getId(), pageRequest.of()); + List notifications = result.getContent(); + return GetNotificationResponse.from(notifications); + } } diff --git a/backend/src/main/java/com/graphy/backend/global/result/ResultCode.java b/backend/src/main/java/com/graphy/backend/global/result/ResultCode.java index 5b68ecce..943365e2 100644 --- a/backend/src/main/java/com/graphy/backend/global/result/ResultCode.java +++ b/backend/src/main/java/com/graphy/backend/global/result/ResultCode.java @@ -58,7 +58,10 @@ public enum ResultCode { // Message MESSAGE_CREATE_SUCCESS("MSG001", "쪽지 전송 성공"), MESSAGE_GET_SUCCESS("MSG002", "쪽지 단건 조회 성공"), - MESSAGE_PAGING_GET_SUCCESS("MSG003", "쪽지 페이징 조회 성공"); + MESSAGE_PAGING_GET_SUCCESS("MSG003", "쪽지 페이징 조회 성공"), + + // Notification + NOTIFICATION_PAGING_GET_SUCCESS("N001", "알림 페이징 조회 성공"); private final String code; private final String message; From 7e3f8957cb962c85b4cd795c4d07011dff14bd86 Mon Sep 17 00:00:00 2001 From: HaLin Kim Date: Wed, 4 Oct 2023 20:34:12 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EC=95=8C=EB=A6=BC=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20response=20dto=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/GetNotificationResponse.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 backend/src/main/java/com/graphy/backend/domain/notification/dto/response/GetNotificationResponse.java diff --git a/backend/src/main/java/com/graphy/backend/domain/notification/dto/response/GetNotificationResponse.java b/backend/src/main/java/com/graphy/backend/domain/notification/dto/response/GetNotificationResponse.java new file mode 100644 index 00000000..e8b3c5be --- /dev/null +++ b/backend/src/main/java/com/graphy/backend/domain/notification/dto/response/GetNotificationResponse.java @@ -0,0 +1,36 @@ +package com.graphy.backend.domain.notification.dto.response; + +import com.graphy.backend.domain.notification.domain.Notification; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.stream.Collectors; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class GetNotificationResponse { + private Long id; + private String type; + private String content; + private boolean isRead; + + public static GetNotificationResponse from(Notification notification) { + return GetNotificationResponse.builder() + .id(notification.getId()) + .type(notification.getType().toString()) + .content(notification.getContent()) + .isRead(notification.isRead()) + .build(); + } + + public static List from(List notifications) { + return notifications.stream() + .map(GetNotificationResponse::from) + .collect(Collectors.toList()); + } +}