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 May 16, 2023
1 parent c5599cd commit 897f766
Showing 1 changed file with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,28 +215,23 @@ public void modifyDessert(PatchDessertReq patchDessertReq, Long dessertIdx, Long

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)
if (optionDTO.getOptionDescription() == null)
throw new BaseException(NULL_OPTION_DESCRIPTION);
if(optionDTO.getOptionPrice() == null)
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)); // 없으면 오류말고, addDessert로 새로 옵션 만들기

for(OptionDTO modifiedOption : patchDessertReq.getOptions()) {
option.setDescription(modifiedOption.getOptionDescription());
option.setPrice(modifiedOption.getOptionPrice());
if (optionDTO.getOptionIdx() == null) { // 새로운 option인 경우
PostDessertReq.Option newOption = new PostDessertReq.Option(optionDTO.getOptionDescription(), optionDTO.getOptionPrice());
saveOption(newOption, dessert);
} else { // 이미 존재하는 option인 경우
Long optionIdx = optionDTO.getOptionIdx();
Option option = optionRepository.findByOptionIdxAndStatus(optionIdx, ACTIVE_STATUS)
.orElseThrow(() -> new BaseException(INVALID_OPTION_IDX));

option.setDescription(optionDTO.getOptionDescription());
option.setPrice(optionDTO.getOptionPrice());
optionRepository.save(option);
}
optionRepository.save(option);
}
}
dessertRepository.save(dessert);
Expand Down

0 comments on commit 897f766

Please sign in to comment.