Skip to content

Commit

Permalink
fix: 알림 종류 구분 없이 알림 내역 페이징 처리 (#241) (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip authored Oct 17, 2024
1 parent a792222 commit b3c71aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import com.api.ttoklip.global.success.Message;
import com.api.ttoklip.global.success.SuccessResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -38,11 +41,14 @@ public SuccessResponse<Message> updateMemberFCMToken(

@GetMapping("/my-notification")
public SuccessResponse<NotificationFrontResponses> getNotification(
@RequestParam final String notificationCategory
@Parameter(description = "페이지 번호 (0부터 시작, 기본값 0)", example = "0")
@RequestParam(required = false, defaultValue = "0") final int page,
@Parameter(description = "페이지당 개수 (기본값 5)", example = "0")
@RequestParam(required = false, defaultValue = "5") final int size
) {
return new SuccessResponse<>(
notificationService.findNotificationByCategory(notificationCategory)
);
Long currentMemberId = getCurrentMember().getId();
Pageable pageRequest = PageRequest.of(page, size);
return new SuccessResponse<>(notificationService.findNotification(currentMemberId, pageRequest));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import static com.api.ttoklip.domain.member.domain.QMember.member;
import static com.api.ttoklip.domain.notification.entity.QNotification.notification;
import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember;

import com.api.ttoklip.domain.notification.entity.NotiCategory;
import com.api.ttoklip.domain.notification.entity.Notification;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

Expand All @@ -19,21 +16,20 @@ public class NotificationRepositoryImpl {

private final JPAQueryFactory queryFactory;

private List<Notification> findRecentNotifications(NotiCategory notiCategory, Pageable pageable) {
private List<Notification> findRecentNotifications(final Long currentMemberId, final Pageable pageable) {
return queryFactory
.selectFrom(notification)
.leftJoin(notification.member, member).fetchJoin()
.where(
notification.notiCategory.eq(notiCategory),
notification.member.id.eq(getCurrentMember().getId())
notification.member.id.eq(currentMemberId)
)
.orderBy(notification.createdDate.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
}

public List<Notification> findTop5RecentNotifications(NotiCategory notiCategory) {
return findRecentNotifications(notiCategory, PageRequest.of(0, 5));
public List<Notification> findTop5RecentNotifications(final Long currentMemberId, final Pageable pageRequest) {
return findRecentNotifications(currentMemberId, pageRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.api.ttoklip.domain.common.comment.Comment;
import com.api.ttoklip.domain.honeytip.post.domain.HoneyTip;
import com.api.ttoklip.domain.member.domain.Member;
import com.api.ttoklip.domain.member.service.MemberService;
import com.api.ttoklip.domain.notification.dto.response.NotificationFrontResponse;
import com.api.ttoklip.domain.notification.dto.response.NotificationFrontResponses;
import com.api.ttoklip.domain.notification.entity.NotiCategory;
Expand All @@ -16,6 +17,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -26,6 +28,7 @@ public class NotificationService {

private final NotificationRepository notificationRepository;
private final NotificationRepositoryImpl notificationRepositoryImpl;
private final MemberService memberService;

@Transactional
public void register(final NotiCategory notiCategory, final Member member, final Long targetClassId,
Expand Down Expand Up @@ -64,15 +67,19 @@ private String getTargetType(Object target) {
throw new ApiException(ErrorType._BAD_CATEGORY_NOTIFICATION_TYPE);
}

public NotificationFrontResponses findNotification(final Long currentMemberId, final Pageable pageRequest) {
Member currentMember = memberService.findById(currentMemberId);

public NotificationFrontResponses findNotificationByCategory(final String value) {
NotiCategory category = NotiCategory.getNotificationByCategory(value);
List<Notification> top5RecentNotifications = notificationRepositoryImpl.findTop5RecentNotifications(category);
List<Notification> top5RecentNotifications = notificationRepositoryImpl.findTop5RecentNotifications(
currentMember.getId(), pageRequest
);

List<NotificationFrontResponse> responses = top5RecentNotifications.stream()
.map(noti -> NotificationFrontResponse.of(noti.getId(), noti.getTargetIndex(), noti.getTargetType(),
noti.getTitle(),
noti.getText(), noti.isStatus())).toList();
.map(noti -> NotificationFrontResponse.of(
noti.getId(), noti.getTargetIndex(), noti.getTargetType(),
noti.getTitle(), noti.getText(), noti.isStatus()
)
).toList();

return NotificationFrontResponses.from(responses);
}
Expand Down

0 comments on commit b3c71aa

Please sign in to comment.