Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BE/#255 알림 목록 조회 기능 구현 #259

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<ResultResponse> notificationList(PageRequest pageRequest, @CurrentUser Member loginUser) {
List<GetNotificationResponse> result = notificationService.findNotificationList(pageRequest, loginUser);
return ResponseEntity.ok(ResultResponse.of(ResultCode.NOTIFICATION_PAGING_GET_SUCCESS, result));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;

Expand All @@ -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 {

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<GetNotificationResponse> from(List<Notification> notifications) {
return notifications.stream()
.map(GetNotificationResponse::from)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -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<Notification, Long> {

Page<Notification> findAllByMemberId(Long memberId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,4 +38,12 @@ public void addNotification(NotificationDto dto, Long memberId) {
throw new BusinessException(SEND_EMAIL_FAIL);
}
}

public List<GetNotificationResponse> findNotificationList(PageRequest pageRequest, Member loginUser) {
memberService.findMemberById(loginUser.getId());
Page<Notification> result
= notificationRepository.findAllByMemberId(loginUser.getId(), pageRequest.of());
List<Notification> notifications = result.getContent();
return GetNotificationResponse.from(notifications);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading