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

refactor: 상품 반환시 자신의 상품인지 확인할 수 있도록 컬럼 추가 #91

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ subprojects {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.0'
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'

// jasypt
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public List<ProductPagingSimpleResponse> findAllProductsInCategory(final Long pr
return productRepository.findAllProductsInCategoryWithPaging(productId, categoryId, pageSize);
}

public ProductSpecificResponse findById(final Long productId) {
return productRepository.findSpecificProductById(productId)
public ProductSpecificResponse findById(final Long productId, final Long memberId) {
return productRepository.findSpecificProductById(productId, memberId)
.orElseThrow(ProductNotFoundException::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface ProductRepository {

Optional<Product> findByIdWithPessimisticLock(Long productId);

Optional<ProductSpecificResponse> findSpecificProductById(Long productId);
Optional<ProductSpecificResponse> findSpecificProductById(Long productId, Long memberId);

void deleteProductById(Long productId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ public record ProductSpecificResponse(
Long categoryId,
CategoryName categoryName,
String ownerNickname,
Boolean isMyProduct,
LocalDateTime createDate
) {

public ProductSpecificResponse(final Long id, final Location location, final String title, final String content, final Integer price, final ProductStatus productStatus, final Integer visitedCount, final Integer contactCount, final Long categoryId, final CategoryName categoryName, final String ownerNickname, final LocalDateTime createDate) {
this(id, location.getContent(), title, content, price, productStatus, visitedCount, contactCount, categoryId, categoryName, ownerNickname, createDate);
public ProductSpecificResponse(final Long id, final Location location, final String title, final String content, final Integer price, final ProductStatus productStatus, final Integer visitedCount, final Integer contactCount, final Long categoryId, final CategoryName categoryName, final String ownerNickname, final Boolean isMyProduct, final LocalDateTime createDate) {
this(id, location.getContent(), title, content, price, productStatus, visitedCount, contactCount, categoryId, categoryName, ownerNickname, isMyProduct, createDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private BooleanExpression ltProductId(final Long productId) {
return product.id.lt(productId);
}

public Optional<ProductSpecificResponse> findSpecificProductById(final Long id) {
public Optional<ProductSpecificResponse> findSpecificProductById(final Long id, final Long memberId) {
return Optional.ofNullable(jpaQueryFactory.select(constructor(ProductSpecificResponse.class,
product.id,
product.description.location,
Expand All @@ -63,6 +63,7 @@ public Optional<ProductSpecificResponse> findSpecificProductById(final Long id)
product.categoryId,
category.name,
member.nickname,
member.id.eq(memberId),
product.createdAt
)).from(product)
.where(product.id.eq(id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public Optional<Product> findByIdWithPessimisticLock(final Long productId) {
}

@Override
public Optional<ProductSpecificResponse> findSpecificProductById(final Long productId) {
return productQueryRepository.findSpecificProductById(productId);
public Optional<ProductSpecificResponse> findSpecificProductById(final Long productId, final Long memberId) {
return productQueryRepository.findSpecificProductById(productId, memberId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public ResponseEntity<Long> uploadProduct(@PathVariable("categoryId") final Long
@GetMapping("/{categoryId}/products/{productId}")
public ResponseEntity<ProductSpecificResponse> findProductById(@PathVariable("productId") final Long productId,
@PathVariable("categoryId") final Long categoryId,
@AuthMember final Long memberId,
@ViewCountChecker final Boolean canAddViewCount) {
productService.addViewCount(productId, canAddViewCount);
return ResponseEntity.ok(productQueryService.findById(productId));
return ResponseEntity.ok(productQueryService.findById(productId, memberId));
}

@PatchMapping("/{categoryId}/products/{productId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.server.helper;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.function.Executable;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
public class ConcurrencyHelper {

private static final int THREAD = 10;
Expand All @@ -25,7 +23,7 @@ public static int execute(final Executable executable, final AtomicLong successC
executable.execute();
successCount.getAndIncrement();
} catch (final Throwable e) {
log.info(e.getClass().getName());
System.out.println(e.getClass().getName());
} finally {
latch.countDown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void setup() {
@Test
void 상품_상세_조회() {
// when
ProductSpecificResponse result = productQueryService.findById(product.getId());
ProductSpecificResponse result = productQueryService.findById(product.getId(), 1L);

// then
assertThat(result.id()).isEqualTo(product.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public class ProductFixture {
}

public static ProductSpecificResponse 상품_상세정보_생성() {
return new ProductSpecificResponse(1L, Location.BUILDING_CENTER, "상품명", "상품 참 좋아요~", 10000, ProductStatus.WAITING, 4, 1, 1L, CategoryName.A000, "귀여운_피그미123", LocalDateTime.now());
return new ProductSpecificResponse(1L, Location.BUILDING_CENTER, "상품명", "상품 참 좋아요~", 10000, ProductStatus.WAITING, 4, 1, 1L, CategoryName.A000, "귀여운_피그미123", true, LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public Optional<Product> findByIdWithPessimisticLock(final Long productId) {
}

@Override
public Optional<ProductSpecificResponse> findSpecificProductById(final Long productId) {
public Optional<ProductSpecificResponse> findSpecificProductById(final Long productId, final Long memberId) {
if (map.containsKey(id)) {
Product product = map.get(id);
return Optional.of(new ProductSpecificResponse(product.getId(), product.getDescription().getLocation(), product.getDescription().getTitle(), product.getDescription().getContent(), product.getPrice().getPrice(), product.getProductStatus(), product.getStatisticCount().getVisitedCount(), product.getStatisticCount().getContactCount(), product.getCategoryId(), CategoryName.A000, "owner", LocalDateTime.now()));
return Optional.of(new ProductSpecificResponse(product.getId(), product.getDescription().getLocation(), product.getDescription().getTitle(), product.getDescription().getContent(), product.getPrice().getPrice(), product.getProductStatus(), product.getStatisticCount().getVisitedCount(), product.getStatisticCount().getContactCount(), product.getCategoryId(), CategoryName.A000, "owner", true, LocalDateTime.now()));
}

return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void setup() {
Product product = productRepository.save(상품_생성());

// when
Optional<ProductSpecificResponse> result = productQueryRepository.findSpecificProductById(product.getId());
Optional<ProductSpecificResponse> result = productQueryRepository.findSpecificProductById(product.getId(), member.getId());

// then
assertSoftly(softly -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ProductControllerWebMvcTest extends MockBeanInjection {
Long categoryId = 1L;
Long productId = 1L;
ProductSpecificResponse response = 상품_상세정보_생성();
when(productQueryService.findById(any())).thenReturn(response);
when(productQueryService.findById(any(), any())).thenReturn(response);

// when & then
mockMvc.perform(get("/api/categories/{categoryId}/products/{productId}", categoryId, productId)
Expand Down Expand Up @@ -165,6 +165,7 @@ class ProductControllerWebMvcTest extends MockBeanInjection {
fieldWithPath("categoryId").description("카테고리 id"),
fieldWithPath("categoryName").description("카테고리 이름"),
fieldWithPath("ownerNickname").description("판매자 닉네임"),
fieldWithPath("isMyProduct").description("자신이 등록한 상품인지 (Boolean)"),
fieldWithPath("createDate").description("상품 등록일")
)
));
Expand Down
Loading