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

[Feat] PATCH /m/products/sale API 추가 #99

Merged
merged 1 commit into from
Feb 11, 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
7 changes: 6 additions & 1 deletion src/main/java/com/ecolink/core/common/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ public enum ErrorCode {
PRODUCT_NOT_FOUND(HttpStatus.INTERNAL_SERVER_ERROR, "P-001", "주어진 이름으로 제품을 찾을 수 없습니다."),
INVALID_PRICE_UNIT(HttpStatus.BAD_REQUEST, "P-002", "올바르지 않은 가격 단위입니다. 10원 단위로 입력해주세요"),
INVALID_PRODUCT_NAME_FORMAT(HttpStatus.BAD_REQUEST, "P-003", "제품명은 영문(대소문자), 숫자, 한글로만 구성되어야합니다."),
INVALID_PRODUCT_NAME_LENGTH(HttpStatus.BAD_REQUEST, "P-004", "제품명의 길이가 유효하지 않습니다.");
INVALID_PRODUCT_NAME_LENGTH(HttpStatus.BAD_REQUEST, "P-004", "제품명의 길이가 유효하지 않습니다."),

/**
*
*/
STORE_PRODUCT_NOT_FOUND(HttpStatus.BAD_REQUEST, "SP-001","주어진 식별자로 매장 제품을 찾을 수 없습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -20,6 +22,7 @@
import com.ecolink.core.common.response.ApiCursorPageResponse;
import com.ecolink.core.common.response.ApiResponse;
import com.ecolink.core.product.dto.request.GetStoreProductRequest;
import com.ecolink.core.product.dto.request.PatchSaleStatusRequest;
import com.ecolink.core.product.dto.request.PostStoreProductRequest;
import com.ecolink.core.product.dto.response.GetStoreProductResponse;
import com.ecolink.core.product.service.StoreProductCudService;
Expand Down Expand Up @@ -89,4 +92,17 @@ public ApiResponse<Long> registerStoreProduct(
return ApiResponse.ok(storeProductCudService.registerStoreProduct(storeId, request, images, principal));
}

@Tag(name = "${swagger.tag.manager}")
@Operation(summary = "매장제품 판매 상태 변경 API- 인증 필요",
description = "매장제품 판매 상태 변경 API - 인증 필요",
security = {@SecurityRequirement(name = "session-token")})
@PreAuthorize("hasRole('MANAGER')")
@PatchMapping(value = "/m/products/sale")
public ApiResponse<Void> changeSaleStatus(
@RequestBody @Valid PatchSaleStatusRequest request,
@AuthenticationPrincipal UserPrincipal principal) {
storeProductCudService.changeSaleStatus(request, principal);
return ApiResponse.ok();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ public StoreProduct(int price, Store store, Product product, Tag tag) {
this.main = false;
}

public void updateSaleStatus(boolean onSale) {
this.onSale = onSale;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ecolink.core.product.dto.request;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;

@Getter
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public class PatchSaleStatusRequest {

@Schema(description = "매장 제품의 ID", example = "1")
private final Long storeProductId;
@Schema(description = "변경할 판매 상태", example = "true")
private final boolean onSale;

public PatchSaleStatusRequest(Long storeProductId, boolean onSale) {
this.storeProductId = storeProductId;
this.onSale = onSale;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -12,9 +13,14 @@
public interface StoreProductRepository extends JpaRepository<StoreProduct, Long> {

@Query("select sp from StoreProduct sp "
+ "join fetch sp.product "
+ "join fetch sp.store s "
+ "where s.id in :ids")
+ "join fetch sp.product "
+ "join fetch sp.store s "
+ "where s.id in :ids")
List<StoreProduct> findByStoreIdIn(@Param("ids") Collection<Long> ids);

@Query("select sp from StoreProduct sp "
+ "join fetch sp.store "
+ "where sp.id = :id")
Optional<StoreProduct> findByIdWithStore(@Param("id") Long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.ecolink.core.file.service.MultiPhotoService;
import com.ecolink.core.product.domain.StoreProduct;
import com.ecolink.core.product.domain.StoreProductPhoto;
import com.ecolink.core.product.dto.request.PatchSaleStatusRequest;
import com.ecolink.core.product.dto.request.PostStoreProductRequest;
import com.ecolink.core.store.service.StoreService;
import com.ecolink.core.tag.constant.TagCategory;
Expand Down Expand Up @@ -50,4 +51,12 @@ public Long registerStoreProduct(Long storeId, PostStoreProductRequest request,
return storeProduct.getId();
}

@Transactional
public void changeSaleStatus(PatchSaleStatusRequest request, UserPrincipal principal) {
StoreProduct product = storeProductService.getByIdWithStore(request.getStoreProductId());
if (!AuthorityUtil.hasAdminAuthority(principal) && !principal.isManagerOf(product.getStore().getId()))
throw new ManagerForbiddenException(ErrorCode.NOT_MANAGER_OF_STORE);
product.updateSaleStatus(request.isOnSale());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecolink.core.common.error.ErrorCode;
import com.ecolink.core.common.error.exception.EntityNotFoundException;
import com.ecolink.core.product.domain.StoreProduct;
import com.ecolink.core.product.dto.request.GetStoreProductRequest;
import com.ecolink.core.product.dto.response.GetStoreProductResponse;
Expand Down Expand Up @@ -96,4 +98,9 @@ public StoreProduct createStoreProduct(int price, Store store, Product product,
return storeProductRepository.save(new StoreProduct(price, store, product, tag));
}

public StoreProduct getByIdWithStore(Long storeProductId) {
return storeProductRepository.findByIdWithStore(storeProductId).
orElseThrow(() -> new EntityNotFoundException(ErrorCode.STORE_PRODUCT_NOT_FOUND));
}

}
Loading