Skip to content

Commit

Permalink
Merge pull request #162 from prgrms-web-devcourse-final-project/refac…
Browse files Browse the repository at this point in the history
…tor/#161-notification

[Refactor/#161] 키워드/타겟 편지 알림 조회 시 라벨 이미지 응답 추가
  • Loading branch information
hgh1472 authored Dec 7, 2024
2 parents bb3416b + caa0c9b commit 99e7636
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import postman.bottler.letter.dto.response.LetterDetailResponseDTO;
import postman.bottler.letter.dto.response.LetterRecommendHeadersResponseDTO;
import postman.bottler.letter.exception.LetterNotFoundException;
import postman.bottler.notification.dto.request.NotificationLabelRequestDTO;
import postman.bottler.user.service.UserService;

@Slf4j
Expand Down Expand Up @@ -76,4 +77,10 @@ public List<LetterRecommendHeadersResponseDTO> getRecommendHeaders(List<Long> le
public boolean checkLetterExists(Long letterId) {
return letterRepository.checkLetterExists(letterId);
}

public List<NotificationLabelRequestDTO> getLabels(List<Long> ids) {
return letterRepository.findAllByIds(ids).stream()
.map(find -> new NotificationLabelRequestDTO(find.getId(), find.getLabel()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,11 @@ public Page<FindReceivedMapLetterDTO> findActiveReceivedMapLettersByUserId(Long
public List<MapLetterAndDistance> guestFindLettersByUserLocation(BigDecimal latitude, BigDecimal longitude) {
return mapLetterJpaRepository.guestFindLettersByUserLocation(latitude, longitude);
}

@Override
public List<MapLetter> findAllByIds(List<Long> ids) {
return mapLetterJpaRepository.findAllById(ids).stream()
.map(MapLetterEntity::toDomain)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public interface MapLetterRepository {
Page<FindReceivedMapLetterDTO> findActiveReceivedMapLettersByUserId(Long userId, PageRequest pageRequest);

List<MapLetterAndDistance> guestFindLettersByUserLocation(BigDecimal latitude, BigDecimal longitude);

List<MapLetter> findAllByIds(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -40,6 +39,7 @@
import postman.bottler.mapletter.exception.LetterAlreadyReplyException;
import postman.bottler.mapletter.exception.MapLetterAlreadyArchivedException;
import postman.bottler.mapletter.exception.PageRequestException;
import postman.bottler.notification.dto.request.NotificationLabelRequestDTO;
import postman.bottler.reply.dto.ReplyType;
import postman.bottler.user.service.UserService;

Expand Down Expand Up @@ -387,4 +387,12 @@ public OneLetterResponseDTO guestFindOneMapLetter(Long letterId, BigDecimal lati
String profileImg = userService.getProfileImageUrlById(mapLetter.getCreateUserId());
return OneLetterResponseDTO.from(mapLetter, profileImg, false);
}

@Transactional(readOnly = true)
public List<NotificationLabelRequestDTO> getLabels(List<Long> ids) {
List<MapLetter> finds = mapLetterRepository.findAllByIds(ids);
return finds.stream()
.map(find -> new NotificationLabelRequestDTO(find.getId(), find.getLabel()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -48,7 +49,7 @@ public ApiResponse<NotificationResponseDTO> create(
}

@Operation(summary = "알림 조회", description = "사용자의 알림을 조회합니다.")
@GetMapping
@PatchMapping
public ApiResponse<List<NotificationResponseDTO>> getNotifications(
@AuthenticationPrincipal CustomUserDetails customUserDetails) {
List<NotificationResponseDTO> userNotifications = notificationService.getUserNotifications(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.Getter;
import lombok.Setter;
import postman.bottler.notification.exception.NoLetterIdException;

@Getter
public class LetterNotification extends Notification {
private final long letterId;

@Setter
private String label;

protected LetterNotification(NotificationType type, long receiver, Long letterId, Boolean isRead) {
super(type, receiver, isRead);
validateLetterId(letterId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public Boolean isLetterNotification() {
return type.isLetterNotification();
}

public Boolean isKeywordLetterNotification() {
return type.equals(NotificationType.NEW_LETTER);
}

public Boolean isMapLetterNotification() {
return type.equals(NotificationType.TARGET_LETTER);
}

public Notification read() {
return Notification.of(id, type, receiver,
isLetterNotification() ? ((LetterNotification) this).getLetterId() : null, createdAt, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import postman.bottler.notification.dto.request.NotificationLabelRequestDTO;
import postman.bottler.notification.dto.response.NotificationResponseDTO;

@Builder
Expand Down Expand Up @@ -32,6 +33,55 @@ public Notifications markAsRead() {
return new Notifications(changed);
}

public List<Long> extractMapIds() {
List<Long> ids = new ArrayList<>();
for (Notification notification : notifications) {
if (notification.isMapLetterNotification()) {
ids.add(((LetterNotification) notification).getLetterId());
}
}
return ids;
}

public List<Long> extractKeywordIds() {
List<Long> ids = new ArrayList<>();
for (Notification notification : notifications) {
if (notification.isKeywordLetterNotification()) {
ids.add(((LetterNotification) notification).getLetterId());
}
}
return ids;
}

public void setMapLabels(List<NotificationLabelRequestDTO> mapLabels) {
for (Notification notification : notifications) {
if (!notification.isMapLetterNotification()) {
continue;
}
mapLabels.stream()
.filter(label -> label.letterId() == ((LetterNotification) notification).getLetterId())
.forEach(label -> ((LetterNotification) notification).setLabel(label.label()));
}
}

public void setKeywordLabels(List<NotificationLabelRequestDTO> keywordLabels) {
for (Notification notification : notifications) {
if (!notification.isKeywordLetterNotification()) {
continue;
}
keywordLabels.stream()
.filter(label -> label.letterId() == ((LetterNotification) notification).getLetterId())
.forEach(label -> ((LetterNotification) notification).setLabel(label.label()));
}
}

public List<Long> getLetterIds() {
return notifications.stream()
.filter(Notification::isLetterNotification)
.map(notification -> ((LetterNotification) notification).getLetterId())
.toList();
}

public List<NotificationResponseDTO> createDTO() {
return notifications.stream()
.map(NotificationResponseDTO::from)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package postman.bottler.notification.dto.request;

public record NotificationLabelRequestDTO(
Long letterId,
String label
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public record NotificationResponseDTO(
Long receiver,
LocalDateTime createdAt,
Long letterId,
Boolean isRead
Boolean isRead,
String label
) {
public static NotificationResponseDTO from(final Notification notification) {
return new NotificationResponseDTO(
Expand All @@ -21,6 +22,7 @@ public static NotificationResponseDTO from(final Notification notification) {
notification.getReceiver(),
notification.getCreatedAt(),
notification instanceof LetterNotification ? ((LetterNotification) notification).getLetterId() : null,
notification.getIsRead());
notification.getIsRead(),
notification instanceof LetterNotification ? ((LetterNotification) notification).getLabel() : null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import postman.bottler.letter.service.LetterRepository;
import postman.bottler.mapletter.service.MapLetterRepository;
import postman.bottler.notification.domain.Notification;
import postman.bottler.notification.domain.NotificationType;
import postman.bottler.notification.domain.Notifications;
import postman.bottler.notification.domain.PushMessages;
import postman.bottler.notification.domain.Subscriptions;
import postman.bottler.notification.dto.request.NotificationLabelRequestDTO;
import postman.bottler.notification.dto.request.RecommendNotificationRequestDTO;
import postman.bottler.notification.dto.response.NotificationResponseDTO;

Expand All @@ -21,6 +24,9 @@ public class NotificationService {
private final SubscriptionRepository subscriptionRepository;
private final PushNotificationProvider pushNotificationProvider;

private final MapLetterRepository mapLetterRepository;
private final LetterRepository keywordLetterRepository;

@Transactional
public NotificationResponseDTO sendNotification(NotificationType type, Long userId, Long letterId) {
Notification notification = Notification.create(type, userId, letterId);
Expand All @@ -36,10 +42,17 @@ public NotificationResponseDTO sendNotification(NotificationType type, Long user
@Transactional
public List<NotificationResponseDTO> getUserNotifications(Long userId) {
Notifications notifications = notificationRepository.findByReceiver(userId);
List<Long> mapIds = notifications.extractMapIds();
List<NotificationLabelRequestDTO> mapLabels = mapLetterRepository.findAllByIds(mapIds).stream()
.map(letter -> new NotificationLabelRequestDTO(letter.getId(), letter.getLabel())).toList();
notifications.setMapLabels(mapLabels);
List<Long> keywordIds = notifications.extractKeywordIds();
List<NotificationLabelRequestDTO> keywordLabels = keywordLetterRepository.findAllByIds(keywordIds)
.stream().map(letter -> new NotificationLabelRequestDTO(letter.getId(), letter.getLabel())).toList();
notifications.setKeywordLabels(keywordLabels);
notifications.orderByCreatedAt();
List<NotificationResponseDTO> result = notifications.createDTO();
Notifications changed = notifications.markAsRead();
notificationRepository.updateNotifications(changed);
notificationRepository.updateNotifications(notifications.markAsRead());
return result;
}

Expand Down

0 comments on commit 99e7636

Please sign in to comment.