Skip to content

Commit

Permalink
[FEAT] 조회 가능한 알림은 3일 이내로 제한 (#173)
Browse files Browse the repository at this point in the history
* feat: 응답에 안읽은 알림의 총 개수 필드 추가

* feat: 3일 이내 알림을 안읽은 개수와 함께 반환

* style: 포맷팅 적용

* refactor: 불필요한 접근 제어자 제거

* refactor: 양방향 참조 리스트에 final 적용
  • Loading branch information
Profile-exe authored Sep 30, 2024
1 parent 37174e8 commit 506e4a0
Show file tree
Hide file tree
Showing 56 changed files with 190 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class AlreadyLogoutException extends BusinessException {

public static BusinessException EXCEPTION = new AlreadyLogoutException();

public AlreadyLogoutException() {
private AlreadyLogoutException() {
super(AuthErrorCode.ALREADY_LOGOUT);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package econo.buddybridge.auth.service;

import econo.buddybridge.auth.OAuthProvider;
import econo.buddybridge.auth.dto.OAuthInfoResponse;
import econo.buddybridge.auth.dto.OAuthLoginParams;
import econo.buddybridge.auth.dto.kakao.UserInfoWithKakaoToken;
import econo.buddybridge.member.dto.MemberResDto;
Expand All @@ -25,4 +24,3 @@ public void logout(OAuthProvider provider) {
OAuthInfoService.logout(provider);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public class LastChatMessageNotFoundException extends BusinessException {

public static BusinessException EXCEPTION = new LastChatMessageNotFoundException();

private LastChatMessageNotFoundException() { super(ChatMessageErrorCode.LAST_CHAT_MESSAGE_NOT_FOUND); }
private LastChatMessageNotFoundException() {
super(ChatMessageErrorCode.LAST_CHAT_MESSAGE_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import java.util.List;

public record CommentCustomPage(
List<CommentResDto> content,
Long cursor,
Boolean nextPage
List<CommentResDto> content,
Long cursor,
Boolean nextPage
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
public record CommentReqDto(
String content
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import java.util.List;

public record MyPageCommentCustomPage(
List<MyPageCommentResDto> content,
Long totalElements,
Boolean last
List<MyPageCommentResDto> content,
Long totalElements,
Boolean last
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import econo.buddybridge.post.entity.AssistanceType;
import econo.buddybridge.post.entity.PostStatus;
import econo.buddybridge.post.entity.PostType;
import lombok.Builder;

import java.time.LocalDateTime;
import lombok.Builder;

@Builder
public record MyPageCommentResDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class CommentDeleteNotAllowedException extends BusinessException {
private CommentDeleteNotAllowedException() {
super(CommentErrorCode.COMMENT_DELETE_NOT_ALLOWED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class CommentInvalidDirectionException extends BusinessException {
private CommentInvalidDirectionException() {
super(CommentErrorCode.COMMENT_INVALID_DIRECTION);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AllowAnonymous {

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,4 @@ private ErrorResponse makeErrorResponse(MethodArgumentNotValidException ex, Erro

return errorResponse;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public record ValidationError(
String field,
String message
) {

public static ValidationError of(final FieldError fieldError) {
return new ValidationError(fieldError.getField(), fieldError.getDefaultMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ public ApiResponse<ApiResponse.CustomBody<ChatMessageCustomPage>> getChatMessage
ChatMessageCustomPage chatMessages = matchingRoomService.getMatchingRoomMessages(memberId, matchingId, size, cursor);
return ApiResponseGenerator.success(chatMessages, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package econo.buddybridge.matching.dto;

import lombok.Builder;

import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;

@Builder
public record MatchingCustomPage(
List<MatchingResDto> matchings,
LocalDateTime cursor,
Boolean nextPage
) {

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package econo.buddybridge.matching.dto;

public record MatchingReqDto(
Long postId,
Long takerId,
Long giverId
Long postId,
Long takerId,
Long giverId
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import econo.buddybridge.chat.chatmessage.entity.MessageType;
import econo.buddybridge.matching.entity.MatchingStatus;
import econo.buddybridge.post.entity.PostType;
import lombok.Builder;

import java.time.LocalDateTime;
import lombok.Builder;

@Builder
public record MatchingResDto(
Expand All @@ -19,6 +18,7 @@ public record MatchingResDto(
MatchingStatus matchingStatus,
ReceiverDto receiver
) {

@QueryProjection
public MatchingResDto {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
public record MatchingUpdateDto(
MatchingStatus matchingStatus
) {

}
29 changes: 20 additions & 9 deletions src/main/java/econo/buddybridge/matching/entity/Matching.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
import econo.buddybridge.member.entity.Member;
import econo.buddybridge.post.entity.Post;
import econo.buddybridge.post.entity.PostStatus;
import jakarta.persistence.*;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Table(name = "MATCHING")
Expand All @@ -26,7 +37,7 @@ public class Matching extends BaseEntity {
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="post_id")
@JoinColumn(name = "post_id")
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -42,20 +53,20 @@ public class Matching extends BaseEntity {
private MatchingStatus matchingStatus;

@OneToMany(mappedBy = "matching", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ChatMessage> chatMessages = new ArrayList<>();
private final List<ChatMessage> chatMessages = new ArrayList<>();

@Builder
public Matching(Post post, Member taker, Member giver, MatchingStatus matchingStatus){
public Matching(Post post, Member taker, Member giver, MatchingStatus matchingStatus) {
this.post = post;
this.taker = taker;
this.giver = giver;
this.matchingStatus = matchingStatus;
}

// 매칭 상태 변경
public void updateMatching(MatchingStatus matchingStatus){
public void updateMatching(MatchingStatus matchingStatus) {
this.matchingStatus = matchingStatus;
switch (matchingStatus){
switch (matchingStatus) {
case PENDING, FAILED:
post.changeStatus(PostStatus.RECRUITING);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum MatchingStatus {

private final String matchingStatus;

private MatchingStatus(String matchingStatus){
MatchingStatus(String matchingStatus) {
this.matchingStatus = matchingStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class MatchingNotFoundException extends BusinessException {

public static BusinessException EXCEPTION = new MatchingNotFoundException();

private MatchingNotFoundException() { super(MatchingErrorCode.MATCHING_NOT_FOUND); }

private MatchingNotFoundException() {
super(MatchingErrorCode.MATCHING_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public class MatchingUnauthorizedAccessException extends BusinessException {

public static BusinessException EXCEPTION = new MatchingUnauthorizedAccessException();

private MatchingUnauthorizedAccessException() { super(MatchingErrorCode.MATCHING_UNAUTHORIZED_ACCESS); }
private MatchingUnauthorizedAccessException() {
super(MatchingErrorCode.MATCHING_UNAUTHORIZED_ACCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import econo.buddybridge.matching.dto.MatchingCustomPage;
import econo.buddybridge.matching.entity.MatchingStatus;
import java.time.LocalDateTime;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

@Repository
public interface MatchingRepositoryCustom {

MatchingCustomPage findMatchings(Long memberId, Integer size, LocalDateTime cursor, MatchingStatus matchingStatus, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@Service
@RequiredArgsConstructor
public class MatchingService {

private final ChatMessageRepository chatMessageRepository;
private final MatchingRepository matchingRepository;
private final MemberService memberService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class MemberNotFoundException extends BusinessException {
private MemberNotFoundException() {
super(MemberErrorCode.MEMBER_NOT_FOUND);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class EmitterController {
@GetMapping(value = "/connect", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter connect(
@RequestHeader(value = "Last-Event-ID", required = false, defaultValue = "")
String lastEventId,
String lastEventId,
HttpServletRequest request,
HttpServletResponse response
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.List;

public record NotificationCustomPage(
List<NotificationResDto> content,
Long cursor,
Boolean nextPage
List<NotificationResDto> content,
Long cursor,
Boolean nextPage,
Long totalUnreadCount
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import java.time.LocalDateTime;

public record NotificationResDto(
Long id,
String content,
String url,
Boolean isRead,
NotificationType type,
LocalDateTime createdAt
Long id,
String content,
String url,
Boolean isRead,
NotificationType type,
LocalDateTime createdAt
) {

@QueryProjection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Notification extends BaseEntity {
private NotificationType type;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="receiver_id")
@JoinColumn(name = "receiver_id")
private Member receiver;

public static Notification createNotification(Member receiver, String content, String url, NotificationType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class NotificationAccessDeniedException extends BusinessException {
private NotificationAccessDeniedException() {
super(NotificationErrorCode.NOTIFICATION_ACCESS_DENIED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class NotificationNotFoundException extends BusinessException {
private NotificationNotFoundException() {
super(NotificationErrorCode.NOTIFICATION_NOT_FOUND);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ public interface EmitterRepository {
Map<String, Object> findAllEventCacheStartWithByMemberId(String memberId);

void deleteById(String emitterId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
import econo.buddybridge.notification.dto.NotificationCustomPage;

public interface NotificationRepositoryCustom {

NotificationCustomPage findByMemberId(Long memberId, Integer size, Long cursor, Boolean isRead);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import econo.buddybridge.notification.dto.NotificationCustomPage;
import econo.buddybridge.notification.dto.NotificationResDto;
import econo.buddybridge.notification.dto.QNotificationResDto;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;

Expand All @@ -29,7 +30,8 @@ public NotificationCustomPage findByMemberId(Long memberId, Integer size, Long c
)
)
.from(notification)
.where(notification.receiver.id.eq(memberId), buildCursorPredicate(cursor), buildIsReadPredicate(isRead))
.where(notification.receiver.id.eq(memberId), notification.createdAt.gt(LocalDateTime.now().minusDays(3)), // 3일 이내 알림만 조회
buildCursorPredicate(cursor), buildIsReadPredicate(isRead))
.orderBy(notification.id.desc())
.limit(size + 1)
.fetch();
Expand All @@ -42,7 +44,15 @@ public NotificationCustomPage findByMemberId(Long memberId, Integer size, Long c

Long nextCursor = nextPage ? content.getLast().id() : -1L;

return new NotificationCustomPage(content, nextCursor, nextPage);
Long totalUnreadCount = queryFactory
.select(notification.count())
.from(notification)
.where(notification.receiver.id.eq(memberId),
notification.createdAt.gt(LocalDateTime.now().minusDays(3)),
notification.isRead.eq(false))
.fetchOne();

return new NotificationCustomPage(content, nextCursor, nextPage, totalUnreadCount);
}

private BooleanExpression buildCursorPredicate(Long cursor) {
Expand Down
Loading

0 comments on commit 506e4a0

Please sign in to comment.