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

[feature/7-desserts-patchDessert] [판매자] 상품 수정 API에 디자인 요금 추가 구현 #180

Merged
merged 2 commits into from
Mar 14, 2023
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
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 Down Expand Up @@ -87,6 +91,7 @@ public enum BaseResponseStatus {

// desserts(3300~3399)
INVALID_DESSERT_IDX(false, 3300, "존재하지 않는 디저트입니다."),
INVALID_OPTION_IDX(false, 3301, "존재하지 않는 옵션입니다."),

// calendars(3400~3499)
INVALID_CALENDAR_TAG(false, 3400, "캘린더 TAG를 찾을 수 없습니다."),
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