Skip to content

Commit

Permalink
FIX: (#96) 리뷰 등록시 제로음료 타입의 중첩 JSON 구조를 변경한다
Browse files Browse the repository at this point in the history
  • Loading branch information
anxi01 committed Aug 19, 2024
1 parent 411e69d commit ba59503
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 48 deletions.
25 changes: 12 additions & 13 deletions src/main/java/com/zerozero/core/domain/vo/Review.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.zerozero.core.domain.vo;

import com.zerozero.core.domain.shared.ValueObject;
import com.zerozero.core.domain.vo.ZeroDrink.Type;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serial;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -33,18 +38,8 @@ public class Review extends ValueObject implements Serializable {
@Schema(description = "리뷰 내용", example = "제로 음료 판매중!")
private String content;

@Schema(description = "제로 음료수 목록", example = "[\n" +
" {\n" +
" \"type\": \"COCA_COLA_ZERO\"\n" +
" },\n" +
" {\n" +
" \"type\": \"PEPSI_ZERO\"\n" +
" },\n" +
" {\n" +
" \"type\": \"SPRITE_ZERO\"\n" +
" }\n" +
"]")
private ZeroDrink[] zeroDrinks;
@Schema(description = "제로 음료수 목록", example = "[\"COCA_COLA_ZERO\", \"PEPSI_ZERO\", \"SPRITE_ZERO\"]")
private List<Type> zeroDrinks;

@Schema(description = "작성한 사용자 ID")
private UUID userId;
Expand All @@ -56,7 +51,11 @@ public static Review of(com.zerozero.core.domain.entity.Review review) {
return Review.builder()
.id(review.getId())
.content(review.getContent())
.zeroDrinks(review.getZeroDrinks())
.zeroDrinks(Optional.ofNullable(review.getZeroDrinks())
.map(zeroDrinks -> Arrays.stream(zeroDrinks)
.map(ZeroDrink::getType)
.collect(Collectors.toList()))
.orElse(null))
.userId(review.getUserId())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.zerozero.core.application.BaseResponse;
import com.zerozero.core.domain.vo.AccessToken;
import com.zerozero.core.domain.vo.ZeroDrink;
import com.zerozero.core.domain.vo.ZeroDrink.Type;
import com.zerozero.core.exception.error.GlobalErrorCode;
import com.zerozero.review.application.CreateStoreReviewUseCase;
import com.zerozero.review.application.CreateStoreReviewUseCase.CreateStoreReviewErrorCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.AccessLevel;
Expand Down Expand Up @@ -50,7 +52,12 @@ public ResponseEntity<CreateStoreReviewResponse> createStoreReview(@RequestParam
CreateStoreReviewUseCase.CreateStoreReviewRequest.builder()
.storeId(storeId)
.content(request.getContent())
.zeroDrinks(request.getZeroDrinks())
.zeroDrinks(Optional.ofNullable(request.getZeroDrinks())
.map(zeroDrinks -> zeroDrinks.stream().map(zeroDrink -> ZeroDrink.builder()
.type(zeroDrink)
.build())
.toArray(ZeroDrink[]::new))
.orElse(null))
.accessToken(accessToken)
.build());
if (createStoreReviewResponse == null || !createStoreReviewResponse.isSuccess()) {
Expand Down Expand Up @@ -86,17 +93,7 @@ public static class CreateStoreReviewRequest implements BaseRequest {
@Schema(description = "리뷰 내용", example = "제로콜라 판매 중!")
private String content;

@Schema(description = "제로 음료수 목록", example = "[\n" +
" {\n" +
" \"type\": \"COCA_COLA_ZERO\"\n" +
" },\n" +
" {\n" +
" \"type\": \"PEPSI_ZERO\"\n" +
" },\n" +
" {\n" +
" \"type\": \"SPRITE_ZERO\"\n" +
" }\n" +
"]")
private ZeroDrink[] zeroDrinks;
@Schema(description = "제로 음료수 목록", example = "[\"COCA_COLA_ZERO\", \"PEPSI_ZERO\", \"SPRITE_ZERO\"]")
private List<Type> zeroDrinks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.zerozero.core.application.BaseResponse;
import com.zerozero.core.domain.vo.AccessToken;
import com.zerozero.core.domain.vo.ZeroDrink;
import com.zerozero.core.domain.vo.ZeroDrink.Type;
import com.zerozero.core.exception.error.GlobalErrorCode;
import com.zerozero.review.application.UpdateStoreReviewUseCase;
import com.zerozero.review.application.UpdateStoreReviewUseCase.UpdateStoreReviewErrorCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.AccessLevel;
Expand Down Expand Up @@ -50,7 +52,12 @@ public ResponseEntity<UpdateStoreReviewResponse> updateStoreReview(@PathVariable
UpdateStoreReviewUseCase.UpdateStoreReviewRequest.builder()
.reviewId(reviewId)
.content(request.getContent())
.zeroDrinks(request.getZeroDrink())
.zeroDrinks(Optional.ofNullable(request.getZeroDrinks())
.map(zeroDrinks -> zeroDrinks.stream().map(zeroDrink -> ZeroDrink.builder()
.type(zeroDrink)
.build())
.toArray(ZeroDrink[]::new))
.orElse(null))
.accessToken(accessToken)
.build());
if (updateStoreReviewResponse == null || !updateStoreReviewResponse.isSuccess()) {
Expand Down Expand Up @@ -86,17 +93,7 @@ public static class UpdateStoreReviewRequest implements BaseRequest {
@Schema(description = "리뷰 내용", example = "제로콜라 판매 중!")
private String content;

@Schema(description = "제로 음료수 목록", example = "[\n" +
" {\n" +
" \"type\": \"COCA_COLA_ZERO\"\n" +
" },\n" +
" {\n" +
" \"type\": \"PEPSI_ZERO\"\n" +
" },\n" +
" {\n" +
" \"type\": \"SPRITE_ZERO\"\n" +
" }\n" +
"]")
private ZeroDrink[] zeroDrink;
@Schema(description = "제로 음료수 목록", example = "[\"COCA_COLA_ZERO\", \"PEPSI_ZERO\", \"SPRITE_ZERO\"]")
private List<Type> zeroDrinks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.zerozero.core.domain.vo.AccessToken;
import com.zerozero.core.domain.vo.Store;
import com.zerozero.core.domain.vo.User;
import com.zerozero.core.domain.vo.ZeroDrink;
import com.zerozero.core.domain.vo.ZeroDrink.Type;
import com.zerozero.core.exception.error.GlobalErrorCode;
import com.zerozero.review.application.ReadStoreReviewUseCase;
Expand All @@ -20,6 +19,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -97,16 +97,19 @@ private ReadStoreInfoResponse toResponse(ReadStoreInfoUseCase.ReadStoreInfoRespo
.build();
}

private ZeroDrink[] getTop3ZeroDrinks(ReadStoreReviewResponse.Review[] reviews) {
ZeroDrink[] allZeroDrinks = Arrays.stream(reviews).flatMap(review -> Arrays.stream(review.getReview().getZeroDrinks())).toArray(ZeroDrink[]::new);
return Arrays.stream(allZeroDrinks)
.collect(Collectors.groupingBy(ZeroDrink::getType, Collectors.counting()))
private List<Type> getTop3ZeroDrinks(ReadStoreReviewResponse.Review[] reviews) {
List<Type> allZeroDrinks = Arrays.stream(reviews)
.flatMap(review -> review.getReview().getZeroDrinks().stream())
.collect(Collectors.toList());

return allZeroDrinks.stream()
.collect(Collectors.groupingBy(type -> type, Collectors.counting()))
.entrySet().stream()
.sorted(Map.Entry.<Type, Long>comparingByValue().reversed())
.sorted(Map.Entry.<Type, Long>comparingByValue().reversed()
.thenComparing(Map.Entry.comparingByKey()))
.limit(3)
.map(Map.Entry::getKey)
.flatMap(type -> Arrays.stream(allZeroDrinks).filter(drink -> drink.getType() == type))
.toArray(ZeroDrink[]::new);
.collect(Collectors.toList());
}

@ToString
Expand All @@ -125,7 +128,7 @@ public static class ReadStoreInfoResponse extends BaseResponse<GlobalErrorCode>
private Review[] reviews;

@Schema(description = "제로 음료수 순위")
private ZeroDrink[] zeroDrinks;
private List<Type> zeroDrinks;

record Review(com.zerozero.core.domain.vo.Review review, User user) {

Expand Down

0 comments on commit ba59503

Please sign in to comment.