Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

product select query 수정 #59

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:202312271442'
implementation 'io.github.lotteon-maven:blooming-blooms-utils:202312280417'
testImplementation 'com.github.tomakehurst:wiremock:2.27.2'

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
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.ProductUpdate;
import kr.bb.product.domain.product.mapper.ProductCommand.UpdateSubscriptionProduct;
import kr.bb.product.domain.tag.entity.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.BulkOperations;
Expand Down Expand Up @@ -67,19 +69,25 @@ public void updateProductSaleCount(List<ProductCount> newOrderEvent) {
}

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

@Override
public void updateProductSaleStatus(Product product, ProductSaleStatus productSaleStatus) {
public void updateProductSaleStatus(
String productId, ProductUpdate productRequestData, List<Tag> tags) {
mongoTemplate.updateFirst(
Query.query(Criteria.where("_id").is(product.getProductId())),
Update.update("product_sale_status", productSaleStatus),
Query.query(Criteria.where("_id").is(productId)),
Update.update("product_sale_status", productRequestData.getProductSaleStatus())
.set("tag", tags)
.set("product_summary", productRequestData.getProductSummary())
.set("product_price", productRequestData.getProductPrice())
.set("product_thumbnail", productRequestData.getProductThumbnail())
.set("product_name", productRequestData.getProductName())
.set("product_description_image", productRequestData.getProductDescriptionImage()),
Product.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
import java.util.List;
import java.util.Optional;
import kr.bb.product.domain.product.entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

public interface ProductMongoRepository extends MongoRepository<Product, String> {
@Query("{ '_id' : ?0 }")
Optional<Product> findByProductId(String productId);

@Query(
"{ 'category.categoryId' : ?0, 'product_sale_status' : 'SALE', 'is_subscription' : false }")
Page<Product> findByCategoryId(Long categoryId, Pageable pageable);

@Query("{ 'tag.tagId' : ?0, 'product_sale_status' : 'SALE', 'is_subscription' : false}")
Page<Product> findProductsByTagId(Long tagId, Pageable pageable);

@Query("{'store_id': ?0}")
List<Product> findProductByStoreId(Long storeId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import kr.bb.product.domain.product.entity.ProductSaleStatus;
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;
Expand Down Expand Up @@ -93,7 +92,7 @@ public Page<Product> findProductsByCategory(Long categoryId, Long storeId, Pagea
Query query = new Query();
if (storeId != null) query.addCriteria(Criteria.where("store_id").is(storeId));
query.addCriteria(Criteria.where("category.categoryId").is(categoryId));
query.addCriteria(Criteria.where("product_sale_status").is(ProductSaleStatus.SALE));
query.addCriteria(Criteria.where("product_sale_status").ne(ProductSaleStatus.DELETED));
query.with(pageable);
List<Product> products = mongoTemplate.find(query, Product.class);
return PageableExecutionUtils.getPage(
Expand All @@ -107,7 +106,7 @@ public Page<Product> findProductsByTag(Long tagId, Long categoryId, Pageable pag
Query query = new Query();
query.addCriteria(Criteria.where("tag.tagId").is(tagId));
if (categoryId != null) query.addCriteria(Criteria.where("category.categoryId").is(categoryId));
query.addCriteria(Criteria.where("product_sale_status").is(ProductSaleStatus.SALE));
query.addCriteria(Criteria.where("product_sale_status").ne(ProductSaleStatus.DELETED));
query.with(pageable);

List<Product> products = mongoTemplate.find(query, Product.class);
Expand All @@ -119,9 +118,7 @@ public Page<Product> findProductsByTag(Long tagId, Long categoryId, Pageable pag

@Override
public Product findByProductId(String productId) {
return productMongoRepository
.findByProductId(productId)
.orElseThrow(ProductNotFoundException::new);
return mongoTemplate.findOne(Query.query(Criteria.where("_id").is(productId)), Product.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import kr.bb.product.domain.product.application.port.out.ProductCommandOutPort;
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;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.infrastructure.message.ProductSQSPublisher;
import kr.bb.product.domain.product.mapper.ProductCommand;
Expand Down Expand Up @@ -54,8 +53,8 @@ private List<ProductFlowers> getFlowers(
}

@NotNull
private List<Tag> getTags(ProductCommand.ProductRegister productRequestData) {
return tagRepository.findAllById(productRequestData.getProductTag());
private List<Tag> getTags(List<Long> productRequestData) {
return tagRepository.findAllById(productRequestData);
}

private Category getCategory(ProductCommand.ProductRegister productRequestData) {
Expand All @@ -72,18 +71,19 @@ private Category getCategory(ProductCommand.ProductRegister productRequestData)
*/
@Override
@Transactional
public void updateProductSaleStatus(
public void updateProduct(
String productId, ProductCommand.ProductUpdate productRequestData) {
Product product = productQueryOutPort.findByProductId(productId);
List<Tag> tags = getTags(productRequestData.getProductTag());

if (productRequestData.getProductSaleStatus().equals(ProductSaleStatus.DELETED)) {
productCommandOutPort.updateProductSaleStatus(product);
productCommandOutPort.updateProductSaleStatus(productId, productRequestData);
} else if (productRequestData.getProductSaleStatus().equals(ProductSaleStatus.SALE)) {
// sqs 재입고 알림 조회 요청
publishMessageToSQS.publishProductResaleNotificationCheckQueue(
productId, product.getProductName());
productCommandOutPort.updateProductSaleStatus(product, productRequestData.getProductSaleStatus());
productId, productRequestData.getProductName());
productCommandOutPort.updateProductSaleStatus(productId, productRequestData, tags);
} else {
productCommandOutPort.updateProductSaleStatus(product, productRequestData.getProductSaleStatus());
productCommandOutPort.updateProductSaleStatus(productId, productRequestData, tags);
}
}

Expand All @@ -96,7 +96,7 @@ public void updateProductSaleStatus(
@Transactional
public void createProduct(ProductCommand.ProductRegister productRequestData) {
Category category = getCategory(productRequestData);
List<Tag> tags = getTags(productRequestData);
List<Tag> tags = getTags(productRequestData.getProductTag());
ProductFlowersRequestData representativeFlower = productRequestData.getRepresentativeFlower();
List<ProductFlowers> flowers = getFlowers(productRequestData, representativeFlower);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ private static Pageable getPageable(Pageable pageable, ProductCommand.SortOption
return PageRequest.of(
pageable.getPageNumber(),
pageable.getPageSize(),
Sort.by(direction, sortOption.getSortOption()));
Sort.by(Direction.DESC, "product_sale_status")
.and(Sort.by(direction, sortOption.getSortOption())));
}

private static List<ProductListItem> getProduct(Page<Product> byCategory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
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;
import kr.bb.product.domain.product.mapper.ProductCommand.ProductUpdate;
import kr.bb.product.domain.tag.entity.Tag;

public interface ProductCommandOutPort {
void createProduct(Product product);
Expand All @@ -16,7 +17,7 @@ public interface ProductCommandOutPort {

void updateProductSaleCount(List<ProductCount> newOrderEvent);

void updateProductSaleStatus(Product product);
void updateProductSaleStatus(String productId, ProductUpdate productRequestData);

void updateProductSaleStatus(Product product, ProductSaleStatus productSaleStatus);
void updateProductSaleStatus(String productId, ProductUpdate productRequestData, List<Tag> tags);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import kr.bb.product.domain.product.mapper.ProductCommand;

public interface ProductCommandUseCase {
void updateProductSaleStatus(String productId, ProductCommand.ProductUpdate productRequestData);
void updateProduct(String productId, ProductCommand.ProductUpdate productRequestData);

void createProduct(ProductCommand.ProductRegister productRequestData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void createProduct(
public void updateProductSaleStatus(
@PathVariable String productId,
@RequestBody ProductCommand.ProductUpdate productRequestData) {
productCommandUseCase.updateProductSaleStatus(productId, productRequestData);
productCommandUseCase.updateProduct(productId, productRequestData);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ public static class ProductUpdate {
private String productThumbnail;
private Long productPrice;
private ProductSaleStatus productSaleStatus;
private Long categoryId;
private List<Long> productTag;
private ProductFlowersRequestData representativeFlower;
private List<ProductFlowersRequestData> flowers;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import com.amazonaws.services.sqs.model.AmazonSQSException;
import java.util.List;
import kr.bb.product.domain.product.adapter.out.mongo.ProductMongoRepository;
import kr.bb.product.domain.product.entity.Product;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.mapper.ProductCommand;
import kr.bb.product.domain.product.mapper.ProductCommand.ProductUpdate;
import kr.bb.product.domain.product.mapper.ProductCommand.SubscriptionProduct;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -92,21 +91,23 @@ void updateSubscriptionProduct() {
@Test
@DisplayName("상품 판매 상태 수정")
void updateProductSaleStatus() {
productMongoRepository.deleteAll();
Product save =
productMongoRepository.save(
Product.builder()
.productId("123")
.productSaleStatus(ProductSaleStatus.DISCONTINUED)
.productSaleStatus(ProductSaleStatus.SALE)
.productName("name")
.build());
ProductUpdate name =
ProductUpdate.builder()
.productSaleStatus(ProductSaleStatus.SALE)
.productName("name")
.productSaleStatus(ProductSaleStatus.DISCONTINUED)
.productName("name1")
.productTag(List.of(1L))
.build();

assertThrows(
AmazonSQSException.class,
() -> productCommandInputPort.updateProductSaleStatus("123", name));
productCommandInputPort.updateProduct("123", name);
Product product = productMongoRepository.findByProductId("123").get();
assertThat(product.getProductName()).isEqualTo(name.getProductName());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kr.bb.product.domain.product.application.port.in;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.amazonaws.services.sqs.model.AmazonSQSException;
import java.util.ArrayList;
import java.util.List;
import kr.bb.product.domain.flower.mapper.FlowerCommand.ProductFlowersRequestData;
Expand All @@ -10,6 +12,7 @@
import kr.bb.product.domain.product.entity.Product;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.mapper.ProductCommand;
import kr.bb.product.domain.product.mapper.ProductCommand.ProductUpdate;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -62,12 +65,8 @@ private ProductCommand.ProductUpdate updateProductDiscontinued() {
list.add(ProductFlowersRequestData.builder().flowerId(2L).flowerCount(2L).build());

return ProductCommand.ProductUpdate.builder()
.categoryId(1L)
.productTag(tagList)
.productSaleStatus(ProductSaleStatus.DISCONTINUED)
.representativeFlower(
ProductFlowersRequestData.builder().flowerCount(3L).flowerId(1L).build())
.flowers(list)
.productName("Example Product")
.productSummary("Product Summary")
.productDescriptionImage("image")
Expand All @@ -88,12 +87,8 @@ private ProductCommand.ProductUpdate updateProductDeleted() {
list.add(ProductFlowersRequestData.builder().flowerId(2L).flowerCount(2L).build());

return ProductCommand.ProductUpdate.builder()
.categoryId(1L)
.productTag(tagList)
.productSaleStatus(ProductSaleStatus.DELETED)
.representativeFlower(
ProductFlowersRequestData.builder().flowerCount(3L).flowerId(1L).build())
.flowers(list)
.productName("Example Product")
.productSummary("Product Summary")
.productDescriptionImage("image")
Expand Down Expand Up @@ -131,7 +126,7 @@ void updateProductSaleStatusDeleted() {
Product product = createProduct();

// Act
productStoreInputPort.updateProductSaleStatus(product.getProductId(), productRequestData);
productStoreInputPort.updateProduct(product.getProductId(), productRequestData);
Product byProductId = productQueryOutPort.findByProductId(product.getProductId());
assertThat(byProductId.getProductSaleStatus()).isEqualTo(ProductSaleStatus.DISCONTINUED);
}
Expand All @@ -144,12 +139,30 @@ void updateProductSaleStatusNotDeleted() {
Product product = createProduct();

// Act
productStoreInputPort.updateProductSaleStatus(product.getProductId(), productRequestData);
productStoreInputPort.updateProduct(product.getProductId(), productRequestData);
Product byProductId = productQueryOutPort.findByProductId(product.getProductId());
assertThat(byProductId.getProductSaleStatus()).isEqualTo(ProductSaleStatus.DISCONTINUED);
assertThat(byProductId.getProductSaleStatus()).isEqualTo(ProductSaleStatus.DELETED);
assertThat(byProductId.getIsDeleted()).isEqualTo(true);
}

@Test
@DisplayName("Update product sale status - ProductSaleStatus SALE")
void updateProductSaleStatusSALE() {
// Arrange

ProductUpdate build =
ProductUpdate.builder()
.productTag(List.of(1L))
.productSaleStatus(ProductSaleStatus.SALE)
.build();
Product product = createProduct();

// Act
assertThrows(
AmazonSQSException.class,
() -> productStoreInputPort.updateProduct(product.getProductId(), build));
}

private Product createProduct() {
// Create and return a mock Product object
productStoreInputPort.createProduct(getProductRequestData());
Expand Down
Loading