Skip to content

Commit

Permalink
Merge pull request #195 from Make-A-Wish-Sopt/feature/jiyoung-#194-de…
Browse files Browse the repository at this point in the history
…velop

[FEAT] 선물 조회하기
  • Loading branch information
wlwpfh authored Sep 11, 2024
2 parents b7b3938 + 8d96788 commit 1c8ae6e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public ResponseEntity<ApiResponse> getPresents(
}

@Operation(summary = "해당 소원에 대한 케이크 조회")
@GetMapping("/{wishId}/{cakeId}")
@GetMapping("/{wishId}/{presentId}")
public ResponseEntity<ApiResponse> getEachPresent(
@Parameter(hidden = true) @AuthenticationPrincipal InternalMemberDetails memberDetails,
@PathVariable("wishId") Long wishId,
@PathVariable("cakeId") Long cakeId
@PathVariable("presentId") Long presentId
) {
val response = cakeService.getEachPresent(memberDetails.getId(), wishId, cakeId);
val response = cakeService.getEachPresent(memberDetails.getId(), wishId, presentId);
return ResponseEntity.ok(ApiResponse.success(SUCCESS_GET_PRESENT_MESSAGE.getMessage(), response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import com.sopterm.makeawish.domain.Cake;

import com.sopterm.makeawish.domain.Present;
import lombok.Builder;

@Builder
public record PresentDTO(
Long presentId,
Long cakeId,
Long count
String name
) {
public static PresentDTO from(Cake cake, Long count){
public static PresentDTO from(Present present, Cake cake, String name){
return PresentDTO.builder()
.presentId(present.getId())
.cakeId(cake.getId())
.count(count)
.name(name)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
@Builder
public record PresentResponseDTO(
String name,
String message
String message,
Long cakeId,
Long giftMenuId
) {
public static PresentResponseDTO from(Present present){
return PresentResponseDTO.builder()
.name(present.getName())
.cakeId(present.getCake().getId())
.giftMenuId(present.getGiftMenu().getId())
.message(present.getMessage())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public record MainWishResponseDTO(
Long wishId,
int cakeCount,
long dayCount,
int price,
int percent,
WishStatus status
) {

Expand All @@ -26,8 +24,6 @@ public static MainWishResponseDTO from(Wish wish) {
.wishId(wish.getId())
.cakeCount(wish.getPresents().size())
.dayCount(getRemainDay(wish))
.price(getPriceAppliedFee(wish.getTotalPrice()))
.percent(getPricePercent(wish.getTotalPrice(), wish.getPresentPrice()))
.status(wish.getStatus(0))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import java.util.List;

public interface PresentRepository extends JpaRepository<Present, Long> {
List<Present> findPresentsByWishIdAndCakeId(Long wishId, Long cakeId);
Present findPresentByWishIdAndId(Long wishId, Long presentId);
}
25 changes: 5 additions & 20 deletions src/main/java/com/sopterm/makeawish/service/CakeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,9 @@ public List<PresentDTO> getPresents(Long userId, Long wishId) {
if (!isRightWisher(userId, wish))
throw new IllegalArgumentException(INCORRECT_WISH.getMessage());

val allCake = getAllCakes().stream().collect(
Collectors.toMap(
CakeResponseDTO::toEntity,
count -> 0L
));

val cakes = getAllPresent(wish);
allCake.putAll(cakes);

return allCake.entrySet().stream()
.map(cake -> PresentDTO.from(cake.getKey(), cake.getValue()))
.sorted(Comparator.comparing(PresentDTO::cakeId))
.toList();
}

private Map<Cake, Long> getAllPresent(Wish wish) {
return wish.getPresents().stream()
.collect(Collectors.groupingBy(Present::getCake, Collectors.counting()));
.map(present -> PresentDTO.from(present, present.getCake(), present.getName()))
.collect(Collectors.toList());
}

private boolean isRightWisher(Long userId, Wish wish) {
Expand All @@ -155,13 +140,13 @@ public Cake getPayCake(Long cakeId) {
return getCake(cakeId);
}

public List<PresentResponseDTO> getEachPresent(Long userId, Long wishId, Long cakeId) {
public PresentResponseDTO getEachPresent(Long userId, Long wishId, Long presentId) {
val wish = wishService.getWish(wishId);
if (!isRightWisher(userId, wish)) {
throw new IllegalArgumentException(INCORRECT_WISH.getMessage());
}
val presents = presentRepository.findPresentsByWishIdAndCakeId(wishId, cakeId);
return presents.stream().map(PresentResponseDTO::from).collect(Collectors.toList());
val present = presentRepository.findPresentByWishIdAndId(wishId, presentId);
return PresentResponseDTO.from(present);
}

@Transactional
Expand Down

0 comments on commit 1c8ae6e

Please sign in to comment.