Skip to content

Commit

Permalink
Merge pull request #55 from nowgnas/LF1-854-store-average-rating-update
Browse files Browse the repository at this point in the history
♻️ Delete unused method and file
  • Loading branch information
nowgnas authored Dec 27, 2023
2 parents 0e7b59a + a138ce8 commit 95c8a52
Show file tree
Hide file tree
Showing 24 changed files with 166 additions and 332 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
testImplementation 'org.mockito:mockito-core:4.8.0'
implementation 'io.github.lotteon-maven:blooming-blooms-utils:202312260132'
implementation 'io.github.lotteon-maven:blooming-blooms-utils:202312270111'
testImplementation 'com.github.tomakehurst:wiremock:2.27.2'

}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/kr/bb/product/config/AWSConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import autovalue.shaded.org.jetbrains.annotations.NotNull;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -25,7 +26,7 @@ public class AWSConfiguration {
@Value("${cloud.aws.credentials.SECRET_ACCESS_KEY}")
private String secretAccessKey;

// java1

@NotNull
private BasicAWSCredentials getBasicAWSCredentials() {
return new BasicAWSCredentials(accessKeyId, secretAccessKey);
Expand All @@ -42,7 +43,7 @@ public AwsCredentialsProvider getAwsCredentials() {
@Bean
public AmazonSQSAsync amazonSQSAsync() {
return AmazonSQSAsyncClientBuilder.standard()
.withRegion(region)
.withRegion(Regions.AP_NORTHEAST_1)
.withCredentials(new AWSStaticCredentialsProvider(getBasicAWSCredentials()))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import kr.bb.product.common.dto.ReviewRegisterEvent;
import kr.bb.product.domain.product.application.port.out.ProductCommandOutPort;
import kr.bb.product.domain.product.entity.Product;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.mapper.ProductCommand.UpdateSubscriptionProduct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -64,4 +65,21 @@ public void updateProductSaleCount(List<ProductCount> newOrderEvent) {
}
bulkOperations.execute();
}

@Override
public void updateProductSaleStatus(Product product) {
mongoTemplate.updateFirst(
Query.query(Criteria.where("_id").is(product.getProductId())),
Update.update("is_deleted", true)
.set("product_sale_status", ProductSaleStatus.DISCONTINUED),
Product.class);
}

@Override
public void updateProductSaleStatus(Product product, ProductSaleStatus productSaleStatus) {
mongoTemplate.updateFirst(
Query.query(Criteria.where("_id").is(product.getProductId())),
Update.update("product_sale_status", productSaleStatus),
Product.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@
import kr.bb.product.domain.product.mapper.ProductCommand;
import kr.bb.product.domain.product.mapper.ProductCommand.SelectOption;
import kr.bb.product.exception.errors.ProductNotFoundException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.support.PageableExecutionUtils;
Expand Down Expand Up @@ -202,4 +211,36 @@ public Map<Long, List<Product>> findProductsByProductIdsForCartItem(List<String>
return products.stream()
.collect(Collectors.groupingBy(Product::getStoreId, Collectors.toList()));
}

@Override
public Map<Long, Double> findStoreAverageRating() {
AggregationOperation groupByStoreId =
Aggregation.group("storeId").avg("averageRating").as("averageRating");
TypedAggregation<Product> aggregation =
Aggregation.newAggregation(Product.class, groupByStoreId);

// Execute the aggregation
AggregationResults<AverageResult> aggregate =
mongoTemplate.aggregate(aggregation, AverageResult.class);
List<AverageResult> averageResults = aggregate.getMappedResults();

// Logging for troubleshooting
averageResults.forEach(
result -> {
System.out.println(
"storeId: " + result.get_id() + ", averageRating: " + result.getAverageRating());
});

return averageResults.stream()
.collect(Collectors.toMap(AverageResult::get_id, AverageResult::getAverageRating));
}

@Getter
@AllArgsConstructor
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
private static class AverageResult {
private Long _id;
private Double averageRating;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kr.bb.product.domain.product.application.handler;

import java.util.Map;
import kr.bb.product.domain.product.application.usecase.ProductQueryUseCase;
import kr.bb.product.domain.product.infrastructure.event.ProductKafkaProcessor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ProductQueryHandler {
private static final String STORE_AVERAGE_RATING_UPDATE_TOPIC = "store-average-rating-update";
private final ProductQueryUseCase productQueryUseCase;
private final ProductKafkaProcessor<Map<Long, Double>> productKafkaProcessor;

public void getStoreAverageRating() {
Map<Long, Double> storeAverageRating = productQueryUseCase.getStoreAverageRating();
productKafkaProcessor.send(STORE_AVERAGE_RATING_UPDATE_TOPIC, storeAverageRating);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import kr.bb.product.domain.flower.mapper.FlowerCommand.ProductFlowers;
import kr.bb.product.domain.flower.mapper.FlowerCommand.ProductFlowersRequestData;
import kr.bb.product.domain.product.application.port.out.ProductCommandOutPort;
import kr.bb.product.domain.product.application.port.out.ProductOutPort;
import kr.bb.product.domain.product.application.port.out.ProductQueryOutPort;
import kr.bb.product.domain.product.application.usecase.ProductCommandUseCase;
import kr.bb.product.domain.product.entity.Product;
Expand All @@ -30,7 +29,6 @@
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ProductCommandInputPort implements ProductCommandUseCase {
private final ProductOutPort productOutPort;
private final ProductMapper productMapper;
private final TagRepository tagRepository;
private final CategoryRepository categoryRepository;
Expand Down Expand Up @@ -78,14 +76,14 @@ public void updateProductSaleStatus(
String productId, ProductCommand.ProductUpdate productRequestData) {
Product product = productQueryOutPort.findByProductId(productId);
if (productRequestData.getProductSaleStatus().equals(ProductSaleStatus.DELETED)) {
productOutPort.updateProductSaleStatus(product);
productCommandOutPort.updateProductSaleStatus(product);
} else if (productRequestData.getProductSaleStatus().equals(ProductSaleStatus.SALE)) {
// sqs 재입고 알림 조회 요청
publishMessageToSQS.publishProductResaleNotificationCheckQueue(
productId, product.getProductName());
productOutPort.updateProductSaleStatus(product, productRequestData.getProductSaleStatus());
productCommandOutPort.updateProductSaleStatus(product, productRequestData.getProductSaleStatus());
} else {
productOutPort.updateProductSaleStatus(product, productRequestData.getProductSaleStatus());
productCommandOutPort.updateProductSaleStatus(product, productRequestData.getProductSaleStatus());
}
}

Expand All @@ -102,7 +100,7 @@ public void createProduct(ProductCommand.ProductRegister productRequestData) {
ProductFlowersRequestData representativeFlower = productRequestData.getRepresentativeFlower();
List<ProductFlowers> flowers = getFlowers(productRequestData, representativeFlower);

productOutPort.createProduct(
productCommandOutPort.createProduct(
productMapper.createProductRequestToEntity(productRequestData, category, tags, flowers));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import bloomingblooms.domain.wishlist.cart.GetUserCartItemsResponse;
import bloomingblooms.domain.wishlist.likes.LikedProductInfoResponse;
import bloomingblooms.errors.EntityNotFoundException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -21,6 +20,9 @@
import kr.bb.product.domain.product.application.port.out.ProductQueryOutPort;
import kr.bb.product.domain.product.application.usecase.ProductQueryUseCase;
import kr.bb.product.domain.product.entity.Product;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.infrastructure.client.StoreServiceClient;
import kr.bb.product.domain.product.infrastructure.client.WishlistServiceClient;
import kr.bb.product.domain.product.mapper.ProductCommand;
import kr.bb.product.domain.product.mapper.ProductCommand.BestSellerTopTen;
import kr.bb.product.domain.product.mapper.ProductCommand.LanguageOfFlower;
Expand All @@ -37,9 +39,6 @@
import kr.bb.product.domain.product.mapper.ProductCommand.StoreProductDetail;
import kr.bb.product.domain.product.mapper.ProductCommand.StoreProductList;
import kr.bb.product.domain.product.mapper.ProductCommand.SubscriptionProductForCustomer;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.infrastructure.client.StoreServiceClient;
import kr.bb.product.domain.product.infrastructure.client.WishlistServiceClient;
import kr.bb.product.domain.review.application.port.out.ReviewQueryOutPort;
import kr.bb.product.exception.errors.ProductPriceValidationException;
import lombok.RequiredArgsConstructor;
Expand All @@ -66,7 +65,6 @@ public class ProductQueryInputPort implements ProductQueryUseCase {
private final ProductQueryOutPort productQueryOutPort;
private final FlowerQueryOutPort flowerQueryOutPort;
private final ReviewQueryOutPort reviewQueryOutPort;
private final ObjectMapper objectMapper;

@NotNull
private static Pageable getPageable(Pageable pageable, ProductCommand.SortOption sortOption) {
Expand Down Expand Up @@ -310,6 +308,11 @@ public List<LikedProductInfoResponse> getProductInformationForLikes(List<String>
productQueryOutPort.findProductByProductIds(productIds));
}

@Override
public Map<Long, Double> getStoreAverageRating() {
return productQueryOutPort.findStoreAverageRating();
}

@Override
public GetUserCartItemsResponse getCartItemProductInformations(Map<String, Long> productIds) {
Map<Long, List<Product>> productsByProductIdsForCartItem =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kr.bb.product.common.dto.NewOrderEvent.ProductCount;
import kr.bb.product.common.dto.ReviewRegisterEvent;
import kr.bb.product.domain.product.entity.Product;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.mapper.ProductCommand;

public interface ProductCommandOutPort {
Expand All @@ -14,4 +15,8 @@ public interface ProductCommandOutPort {
void updateProductReviewData(ReviewRegisterEvent reviewRegisterEvent);

void updateProductSaleCount(List<ProductCount> newOrderEvent);

void updateProductSaleStatus(Product product);

void updateProductSaleStatus(Product product, ProductSaleStatus productSaleStatus);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.List;
import java.util.Map;
import kr.bb.product.domain.product.entity.Product;
import kr.bb.product.domain.product.mapper.ProductCommand;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.mapper.ProductCommand;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

Expand Down Expand Up @@ -38,4 +38,6 @@ Page<Product> findStoreProducts(
Map<String, String> findProductNameByProductIdsForReviewByUserId(List<String> productIds);

Map<Long, List<Product>> findProductsByProductIdsForCartItem(List<String> productId);

Map<Long, Double> findStoreAverageRating();
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ ProductCommand.SubscriptionProductForCustomer getSubscriptionProductDetail(
GetUserCartItemsResponse getCartItemProductInformations(Map<String, Long> productIds);

List<LikedProductInfoResponse> getProductInformationForLikes(List<String> productIds);

Map<Long, Double> getStoreAverageRating();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kr.bb.product.domain.product.infrastructure.event;

import lombok.RequiredArgsConstructor;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ProductKafkaProcessor<T> {
private final KafkaTemplate<String, T> kafkaTemplate;

public void send(String topicName, T data) {
kafkaTemplate.send(topicName, data);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.bb.product.domain.product.adapter.in.api;
package kr.bb.product.domain.product.infrastructure.http.api;

import bloomingblooms.response.CommonResponse;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.bb.product.domain.product.adapter.in.client;
package kr.bb.product.domain.product.infrastructure.http.client;

import bloomingblooms.domain.product.IsProductPriceValid;
import bloomingblooms.domain.product.ProductInfoDto;
Expand Down Expand Up @@ -59,7 +59,7 @@ public CommonResponse<ProductInfoDto> getSubscriptionProductInformation(
return CommonResponse.success(productQueryUseCase.getSubscriptionProductInformation(productId));
}

@PostMapping("product/likes")
@PostMapping("products/likes")
public CommonResponse<List<LikedProductInfoResponse>> getProductInformationForLikes(
@RequestBody List<String> productId) {
return CommonResponse.success(productQueryUseCase.getProductInformationForLikes(productId));
Expand Down
Loading

0 comments on commit 95c8a52

Please sign in to comment.