Skip to content

Commit

Permalink
#160 feat: [판매자] 상품 수정 API에 디자인 옵션 추가 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Mar 14, 2023
1 parent d50fd98 commit 16b8aa7
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public enum BaseResponseStatus {
INVALID_DESERT_PRICE(false, 3303, "디저트 가격은 0원 이상이어야 합니다."),
NULL_DESSERT_DESCRIPTION(false, 3304, "디저트 설명을 입력해주세요."),
DELETED_DESSERT(false, 3305, "이미 삭제된 디저트입니다."),
NULL_OPTION(false, 3306, "옵션을 입력해주세요."),
NULL_OPTION_IDX(false, 3307, "optionIdx를 입력해주세요."),
NULL_OPTION_DESCRIPTION(false, 3308, "옵션명을 입력해주세요."),
NULL_OPTION_PRICE(false, 3309, "옵션 가격을 입력해주세요."),

// calendars(2400~2499)
NULL_TITLE(false, 2400, "캘린더 제목을 입력해주세요."),
Expand All @@ -66,6 +70,8 @@ public enum BaseResponseStatus {
NULL_ORDER_IDX(false, 2700, "주문 아이디를 입력해주세요."),
NULL_ORDER_STATUS(false, 2701, "주문 상태를 선택해주세요."),

// option(2800~2899)


/**
* 3000: Response 오류
Expand Down Expand Up @@ -110,6 +116,9 @@ public enum BaseResponseStatus {
NO_MATCH_ORDER_STATUS(false, 3702, "가능한 주문 상태가 아닙니다."),
INVALID_ORDER_STATUS(false, 3703, "존재하지 않는 주문 상태 입니다."),

// option(3800~3899)
INVALID_OPTION_IDX(false, 3800, "존재하지 않는 옵션입니다."),

/**
* 4000: DB, Server 오류
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@AllArgsConstructor
@NoArgsConstructor
public class OptionDTO {
private Long optionIdx;
private String optionDescription;
private Integer optionPrice;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.codepatissier.keki.dessert.dto;

import lombok.Data;
import lombok.Getter;

import java.util.List;

@Data
@Getter
public class PatchDessertReq {
private String dessertImg;
private String dessertName;
private Integer dessertPrice;
private String dessertDescription;
private List<OptionDTO> options;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.codepatissier.keki.dessert.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
Expand All @@ -18,5 +21,13 @@ public class PostDessertReq {
private String dessertDescription;
@NotBlank(message = "디저트 이미지는 필수 항목입니다.")
private String dessertImg;
private List<OptionDTO> options;
private List<Option> options;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class Option {
private String optionDescription;
private Integer optionPrice;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public Option(Dessert dessert, String description, Integer price) {
this.description = description;
this.price = price;
}

public void setDescription(String description) {
this.description = description;
}

public void setPrice(Integer price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import org.springframework.stereotype.Repository;

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

@Repository
public interface OptionRepository extends JpaRepository<Option, Long> {
List<Option> findByDessertAndStatusOrderByOptionIdx(Dessert dessert, String activeStatus);
Optional<Option> findByOptionIdxAndStatus(Long optionIdx, String activeStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private String representPostImgUrl(List<PostImg> postImages){

private List<OptionDTO> getOptionList(Dessert dessert) {
return optionRepository.findByDessertAndStatusOrderByOptionIdx(dessert, ACTIVE_STATUS).stream()
.map(option -> new OptionDTO(option.getDescription(), option.getPrice())).collect(Collectors.toList());
.map(option -> new OptionDTO(option.getOptionIdx(), option.getDescription(), option.getPrice())).collect(Collectors.toList());
}

/**
Expand All @@ -141,7 +141,7 @@ public void addDessert(Long userIdx, PostDessertReq postDessertReq) throws BaseE
.build();
dessertRepository.save(dessert);

for(OptionDTO option : postDessertReq.getOptions())
for(PostDessertReq.Option option : postDessertReq.getOptions())
saveOption(option, dessert);
} catch (BaseException e) {
throw e;
Expand All @@ -150,7 +150,7 @@ public void addDessert(Long userIdx, PostDessertReq postDessertReq) throws BaseE
}
}

private void saveOption(OptionDTO optionDTO, Dessert dessert) throws BaseException {
private void saveOption(PostDessertReq.Option optionDTO, Dessert dessert) throws BaseException {
try {
Option option = Option.builder()
.dessert(dessert)
Expand Down Expand Up @@ -182,10 +182,11 @@ public void deleteDessert(Long userIdx, Long dessertIdx) throws BaseException {
* [판매자] 상품 수정
* 상품 이미지, 이름, 가격, 소개
*/
@Transactional(rollbackFor = Exception.class)
public void modifyDessert(PatchDessertReq patchDessertReq, Long dessertIdx, Long userIdx) throws BaseException {
try {
checkStore(userIdx);
Dessert dessert = dessertRepository.findByDessertIdxAndStatus(dessertIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_DESSERT_IDX));
Dessert dessert = dessertRepository.findByDessertIdxAndStatus(dessertIdx,ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_DESSERT_IDX));

if (patchDessertReq.getDessertImg() != null) {
if (!patchDessertReq.getDessertImg().equals("") && !patchDessertReq.getDessertImg().equals(" "))
Expand All @@ -210,6 +211,32 @@ public void modifyDessert(PatchDessertReq patchDessertReq, Long dessertIdx, Long
dessert.setDessertDescription(patchDessertReq.getDessertDescription());
else throw new BaseException(NULL_DESSERT_DESCRIPTION);
}

if (!patchDessertReq.getOptions().isEmpty() && !(patchDessertReq.getOptions() == null)) {
for (OptionDTO optionDTO : patchDessertReq.getOptions()) {
if(optionDTO.getOptionIdx() == null)
throw new BaseException(NULL_OPTION_IDX);
if(optionDTO.getOptionDescription() == null)
throw new BaseException(NULL_OPTION_DESCRIPTION);
if(optionDTO.getOptionPrice() == null)
throw new BaseException(NULL_OPTION_PRICE);
}

List<Long> optionIdxList = new ArrayList<>();
for (OptionDTO optionDTO : patchDessertReq.getOptions()) {
optionIdxList.add(optionDTO.getOptionIdx());
}

for (Long optionIdx : optionIdxList) {
Option option = optionRepository.findByOptionIdxAndStatus(optionIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_OPTION_IDX));

for(OptionDTO modifiedOption : patchDessertReq.getOptions()) {
option.setDescription(modifiedOption.getOptionDescription());
option.setPrice(modifiedOption.getOptionPrice());
}
optionRepository.save(option);
}
}
dessertRepository.save(dessert);
} catch (BaseException e) {
throw e;
Expand Down

0 comments on commit 16b8aa7

Please sign in to comment.