Skip to content

Commit

Permalink
[Weekly/11] Feature/curation/test (#127)
Browse files Browse the repository at this point in the history
* feat: 큐레이션서비스 유닛 테스트

* feat: 테스트 수정
  • Loading branch information
Daolove0323 authored Nov 13, 2024
1 parent 2265a7a commit 5eab29b
Show file tree
Hide file tree
Showing 59 changed files with 271 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import java.util.stream.Collectors;
import org.ktc2.cokaen.wouldyouin.Image.api.ImageDomain;
import org.ktc2.cokaen.wouldyouin.image.api.ImageDomain;
import org.ktc2.cokaen.wouldyouin._common.api.ApiResponse;
import org.ktc2.cokaen.wouldyouin._common.api.ApiResponseBody;
import org.ktc2.cokaen.wouldyouin._common.exception.BusinessException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
import org.ktc2.cokaen.wouldyouin.Image.persist.AdvertisementImage;
import org.ktc2.cokaen.wouldyouin.image.persist.AdvertisementImage;
import org.ktc2.cokaen.wouldyouin.advertisement.persist.Advertisement;

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.ktc2.cokaen.wouldyouin.Image.application.AdvertisementImageService;
import org.ktc2.cokaen.wouldyouin.Image.persist.AdvertisementImage;
import org.ktc2.cokaen.wouldyouin.image.application.AdvertisementImageService;
import org.ktc2.cokaen.wouldyouin.image.persist.AdvertisementImage;
import org.ktc2.cokaen.wouldyouin._common.exception.EntityNotFoundException;
import org.ktc2.cokaen.wouldyouin.advertisement.api.dto.AdvertisementRequest;
import org.ktc2.cokaen.wouldyouin.advertisement.api.dto.AdvertisementResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.ktc2.cokaen.wouldyouin.Image.persist.AdvertisementImage;
import org.ktc2.cokaen.wouldyouin.image.persist.AdvertisementImage;
import org.ktc2.cokaen.wouldyouin.advertisement.api.dto.AdvertisementRequest;

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.ktc2.cokaen.wouldyouin.Image.persist.CurationImage;
import org.ktc2.cokaen.wouldyouin.image.persist.CurationImage;
import org.ktc2.cokaen.wouldyouin.curation.persist.CurationCard;

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;
import lombok.Builder;
import org.ktc2.cokaen.wouldyouin.Image.persist.CurationImage;
import org.ktc2.cokaen.wouldyouin.image.persist.CurationImage;
import org.ktc2.cokaen.wouldyouin.curation.persist.CurationCard;

@Builder
Expand All @@ -12,15 +12,11 @@ public class CurationCardResponse {
private String content;
private List<String> imageUrls;

public static CurationCardResponse from(CurationCard curationCard) {
public static CurationCardResponse from(CurationCard curationCard, List<String> imageUrls) {
return CurationCardResponse.builder()
.subtitle(curationCard.getSubtitle())
.content(curationCard.getContent())
.imageUrls(
curationCard.getCurationImages().stream()
.map(CurationImage::getName)
.toList()
)
.imageUrls(imageUrls)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ public class CurationResponse {
private final LocalDateTime createdTime;
private final LocalDateTime modifiedDate;

public static CurationResponse from(Curation curation) {
public static CurationResponse from(Curation curation, List<CurationCardResponse> curationCards) {
return CurationResponse.builder()
.id(curation.getId())
.curator(CurationCuratorResponse.from(curation.getCurator()))
.title(curation.getTitle())
.content(curation.getContent())
.curationCards(curation.getCurationCards().stream()
.map(CurationCardResponse::from).toList())
.curationCards(curationCards)
.area(curation.getArea())
.hashtags(curation.getHashtags())
.eventsInfo(curation.getEvents().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ public class CurationSliceResponse {
private final List<CurationResponse> curations;
private final SliceInfo sliceInfo;

public static CurationSliceResponse from(Slice<Curation> curations, int size, Long lastId) {
public static CurationSliceResponse from(List<CurationResponse> curations, int size, Long lastId) {
return CurationSliceResponse.builder()
.curations(curations.stream()
.map(CurationResponse::from).toList())
.curations(curations)
.sliceInfo(SliceInfo.builder()
.sliceSize(size)
.lastId(lastId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.ktc2.cokaen.wouldyouin.curation.application;

import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.ktc2.cokaen.wouldyouin.Image.application.CurationImageService;
import org.ktc2.cokaen.wouldyouin.Image.persist.CurationImage;
import org.ktc2.cokaen.wouldyouin.Image.persist.EventImage;
import org.ktc2.cokaen.wouldyouin.image.application.CurationImageService;
import org.ktc2.cokaen.wouldyouin.image.persist.CurationImage;
import org.ktc2.cokaen.wouldyouin._common.exception.EntityNotFoundException;
import org.ktc2.cokaen.wouldyouin.auth.MemberIdentifier;
import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCardRequest;
Expand All @@ -23,26 +21,16 @@ public class CurationCardService {
private final CurationCardRepository curationCardRepository;
private final CurationImageService curationImageService;

@Transactional(readOnly = true)
public CurationCard getByIdOrThrow(Long id) {
return curationCardRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("해당하는 큐레이션 카드를 찾을 수 없습니다."));
}

@Transactional(readOnly = true)
public CurationCardResponse getById(Long id) {
return CurationCardResponse.from(getByIdOrThrow(id));
}

@Transactional
public void setCuration(CurationCard curationCard, Curation curation) {
curationCard.setCuration(curation);
CurationCard curationCard = getByIdOrThrow(id);
return CurationCardResponse.from(curationCard, getImageUrls(curationCard));
}

@Transactional
public CurationCard create(CurationCardRequest request) {
List<CurationImage> images = request.getImageIds().stream()
.map(curationImageService::getById)
.toList();
.map(curationImageService::getById).toList();
CurationCard curationCard = curationCardRepository.save(request.toEntity(images));
images.forEach(image -> curationImageService.setCuration(image, curationCard));
return curationCard;
Expand All @@ -54,4 +42,19 @@ public void delete(MemberIdentifier identifier, Long id) {
target.getCurationImages().forEach(image -> curationImageService.deleteImage(identifier, image.getId()));
curationCardRepository.deleteById(id);
}

@Transactional
public void setCuration(CurationCard curationCard, Curation curation) {
curationCard.setCuration(curation);
}

private CurationCard getByIdOrThrow(Long id) {
return curationCardRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("해당하는 큐레이션 카드를 찾을 수 없습니다."));
}

public List<String> getImageUrls(CurationCard curationCard) {
return curationCard.getCurationImages().stream()
.map(curationImageService::getImageUrl)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.ktc2.cokaen.wouldyouin.Image.application.CurationImageService;
import org.ktc2.cokaen.wouldyouin.Image.persist.CurationImage;
import org.ktc2.cokaen.wouldyouin.image.application.CurationImageService;
import org.ktc2.cokaen.wouldyouin.image.persist.CurationImage;
import org.ktc2.cokaen.wouldyouin._common.exception.EntityNotFoundException;
import org.ktc2.cokaen.wouldyouin._common.exception.UnauthorizedException;
import org.ktc2.cokaen.wouldyouin._common.vo.Area;
import org.ktc2.cokaen.wouldyouin.auth.MemberIdentifier;
import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCardResponse;
import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCreateRequest;
import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationEditRequest;
import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationResponse;
Expand Down Expand Up @@ -36,29 +37,25 @@ public class CurationService {
private final CurationCardService curationCardService;
private final CurationImageService curationImageService;

@Transactional(readOnly = true)
public Curation getByIdOrThrow(Long id) throws EntityNotFoundException {
return curationRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("해당하는 큐레이션을 찾을 수 없습니다."));
}

@Transactional(readOnly = true)
public CurationResponse getById(Long curationId) {
return CurationResponse.from(getByIdOrThrow(curationId));
Curation curation = getByIdOrThrow(curationId);
return CurationResponse.from(curation, getCurationCardResponses(curation));
}

@Transactional(readOnly = true)
public CurationSliceResponse getAllByAreaOrderByCreatedDateDesc(Area area, Pageable pageable, Long oldLastId) {
Slice<Curation> curations = curationRepository.findAllByAreaOrderByCreatedDateDesc(area, oldLastId, pageable);
Long newLastId = getLastId(curations, oldLastId);
return CurationSliceResponse.from(curations, curations.getSize(), newLastId);
return CurationSliceResponse.from(getCurationResponses(curations), curations.getSize(), newLastId);
}

@Transactional(readOnly = true)
public CurationSliceResponse getAllByCuratorIdOrderByCreatedDateDesc(Long curatorId, Pageable pageable, Long lastId) {
Slice<Curation> curations = curationRepository.findAllByCuratorOrderByCreatedDateDesc(
curatorService.getByIdOrThrow(curatorId), lastId, pageable);
Long newLastId = getLastId(curations, lastId);
return CurationSliceResponse.from(curations, curations.getSize(), newLastId);
return CurationSliceResponse.from(getCurationResponses(curations), curations.getSize(), newLastId);
}

@Transactional
Expand All @@ -73,33 +70,21 @@ public CurationResponse create(MemberIdentifier identifier, CurationCreateReques
Curation curation = curationRepository.save(
curationCreateRequest.toEntity(curator, curationCards, events, getThumbnailUrl(curationCards)));
curationCards.forEach(curationCard -> curationCardService.setCuration(curationCard, curation));
return CurationResponse.from(curation);
}

private String getThumbnailUrl(List<CurationCard> curationCards) {
return Optional.ofNullable(curationCards)
.map(List::getFirst)
.map(CurationCard::getCurationImages)
.map(List::getFirst)
.map(CurationImage::getName)
.map(curationImageService::createThumbnail)
.orElse("");
return CurationResponse.from(curation, getCurationCardResponses(curation));
}

@Transactional
public CurationResponse update(MemberIdentifier identifier, Long curationId, CurationEditRequest curationEditRequest) {
Curation curation = getByIdOrThrow(curationId);
validateCuratorId(identifier, curation);
List<CurationCard> curationCards = curationEditRequest.getCurationCards().stream()
.map(curationCardService::create)
.toList();
.map(curationCardService::create).toList();
List<Event> events = curationEditRequest.getEventIds().stream()
.map(eventService::getByIdOrThrow)
.toList();
.map(eventService::getByIdOrThrow).toList();
curation.getCurationCards().forEach(card -> curationCardService.delete(identifier, card.getId()));
curation.updateFrom(curationEditRequest, curationCards, events, getThumbnailUrl(curationCards));
curationCards.forEach(curationCard -> curationCardService.setCuration(curationCard, curation));
return CurationResponse.from(curation);
return CurationResponse.from(curation, getCurationCardResponses(curation));
}

@Transactional
Expand All @@ -123,4 +108,32 @@ private void validateCuratorId(MemberIdentifier identifier, Curation curation) {
throw new UnauthorizedException("큐레이션에 접근할 권한이 없습니다.");
}
}

private Curation getByIdOrThrow(Long id) throws EntityNotFoundException {
return curationRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("해당하는 큐레이션을 찾을 수 없습니다."));
}

private List<CurationResponse> getCurationResponses(Slice<Curation> curations) {
return curations.getContent().stream()
.map(curation -> CurationResponse.from(curation, getCurationCardResponses(curation)))
.toList();
}

private List<CurationCardResponse> getCurationCardResponses(Curation curation) {
return curation.getCurationCards().stream()
.map(curationCard -> CurationCardResponse.from(
curationCard, curationCard.getCurationImages().stream()
.map(curationImageService::getImageUrl).toList()))
.toList();
}

private String getThumbnailUrl(List<CurationCard> curationCards) {
return Optional.ofNullable(curationCards)
.map(List::getFirst)
.map(CurationCard::getCurationImages)
.map(List::getFirst)
.map(CurationImage::getName)
.map(curationImageService::createThumbnail)
.orElse("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.ktc2.cokaen.wouldyouin.Image.persist.CurationImage;
import org.ktc2.cokaen.wouldyouin.image.persist.CurationImage;

@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import org.ktc2.cokaen.wouldyouin.Image.persist.EventImage;
import org.ktc2.cokaen.wouldyouin.image.persist.EventImage;
import org.ktc2.cokaen.wouldyouin._common.vo.Area;
import org.ktc2.cokaen.wouldyouin._common.vo.Category;
import org.ktc2.cokaen.wouldyouin._common.vo.Location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse;
import org.ktc2.cokaen.wouldyouin._common.vo.Area;
import org.ktc2.cokaen.wouldyouin._common.vo.Category;
import org.ktc2.cokaen.wouldyouin._common.vo.Location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse;
import org.ktc2.cokaen.wouldyouin._common.api.SliceInfo;

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.ktc2.cokaen.wouldyouin.Image.application.EventImageService;
import org.ktc2.cokaen.wouldyouin.Image.application.MemberImageService;
import org.ktc2.cokaen.wouldyouin.Image.persist.EventImage;
import org.ktc2.cokaen.wouldyouin.image.application.EventImageService;
import org.ktc2.cokaen.wouldyouin.image.application.MemberImageService;
import org.ktc2.cokaen.wouldyouin.image.persist.EventImage;
import org.ktc2.cokaen.wouldyouin._common.exception.EntityNotFoundException;
import org.ktc2.cokaen.wouldyouin._common.exception.NoLeftSeatException;
import org.ktc2.cokaen.wouldyouin._common.exception.UnauthorizedException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.ktc2.cokaen.wouldyouin.Image.persist.EventImage;
import org.ktc2.cokaen.wouldyouin.image.persist.EventImage;
import org.ktc2.cokaen.wouldyouin._common.vo.Area;
import org.ktc2.cokaen.wouldyouin._common.vo.Category;
import org.ktc2.cokaen.wouldyouin._common.vo.Location;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package org.ktc2.cokaen.wouldyouin.Image.api;
package org.ktc2.cokaen.wouldyouin.image.api;

import java.nio.file.Paths;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse;
import org.ktc2.cokaen.wouldyouin.Image.application.ImageServiceFactory;
import org.ktc2.cokaen.wouldyouin.Image.application.ImageStorageService;
import org.ktc2.cokaen.wouldyouin.image.api.dto.ImageResponse;
import org.ktc2.cokaen.wouldyouin.image.application.ImageServiceFactory;
import org.ktc2.cokaen.wouldyouin.image.application.ImageStorageService;
import org.ktc2.cokaen.wouldyouin._common.api.ApiResponse;
import org.ktc2.cokaen.wouldyouin._common.api.ApiResponseBody;
import org.ktc2.cokaen.wouldyouin.auth.Authorize;
import org.ktc2.cokaen.wouldyouin.auth.MemberIdentifier;
import org.ktc2.cokaen.wouldyouin.member.persist.MemberType;
import org.ktc2.cokaen.wouldyouin.payment.application.PaymentService;
import org.ktc2.cokaen.wouldyouin.payment.dto.KakaoPayRequest;
import org.ktc2.cokaen.wouldyouin.payment.dto.KakaoPayResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.ktc2.cokaen.wouldyouin.Image.api;
package org.ktc2.cokaen.wouldyouin.image.api;

public enum ImageDomain {
MEMBER,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.ktc2.cokaen.wouldyouin.Image.api.dto;
package org.ktc2.cokaen.wouldyouin.image.api.dto;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.ktc2.cokaen.wouldyouin.Image.api.dto;
package org.ktc2.cokaen.wouldyouin.image.api.dto;

import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
import org.ktc2.cokaen.wouldyouin.Image.persist.Image;
import org.ktc2.cokaen.wouldyouin._common.util.UriUtil;
import org.ktc2.cokaen.wouldyouin.image.persist.Image;

@Getter
@Builder
Expand Down
Loading

0 comments on commit 5eab29b

Please sign in to comment.