refactor(store): 아이템 생성 부분 Casecade 리팩터링

This commit is contained in:
bum12ark
2022-03-08 14:45:07 +09:00
parent 053f33654a
commit 30df46b5e9
6 changed files with 36 additions and 37 deletions

View File

@@ -60,4 +60,11 @@ public class Category extends BaseEntity {
return new Category(id,name,order,store);
}
public static Category of(String name, Integer order, Store store) {
Category category = new Category();
category.name = name;
category.order = order;
category.store = store;
return category;
}
}

View File

@@ -11,11 +11,9 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import static javax.persistence.CascadeType.PERSIST;
import static javax.persistence.CascadeType.REMOVE;
import static javax.persistence.FetchType.LAZY;
@Entity
@@ -45,8 +43,8 @@ public class Item extends BaseEntity {
@JoinColumn(name = "store_id")
private Store store;
@OneToMany(mappedBy = "item" ,cascade = {REMOVE,PERSIST} )
private List<ItemOption> itemOptions;
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL)
private List<ItemOption> itemOptions = new ArrayList<>();
// == 연관관계 편의 메소드 ==//
public void addItemOption(ItemOption itemOption) {
@@ -71,19 +69,15 @@ public class Item extends BaseEntity {
}
// == 생성 메소드 == //
public static Item createdItem(Category category, Store store, List<ItemOption> itemOptions) {
public static Item of(String name, Long price, Category category, Store store, List<ItemOption> itemOptions) {
Item item = new Item();
item.setCategory(category);
item.setStore(store);
itemOptions.forEach(item::addItemOption);
return item;
}
public static Item createdFullItem(Category category, Store store, List<ItemOption> itemOptions, String name , Long price) {
Item item = new Item();
item.setItemNameAndPriceAndCategory( name, price ,category);
item.setStore(store);
itemOptions.forEach(item::addItemOption);
item.name = name;
item.price = price;
item.category = category;
item.store = store;
for (ItemOption itemOption : itemOptions) {
item.addItemOption(itemOption);
}
return item;
}
}

View File

@@ -96,7 +96,6 @@ public class ItemServiceImpl implements ItemService {
Long itemPrice,
Long categoryId,
List<ItemOptionDto> itemOptionDtos) {
//find Store
Store store = storeRepository.findById(storeId)
.orElseThrow(() -> new NotExistItemException("존재하지 않는 매장 입니다."));
@@ -105,11 +104,15 @@ public class ItemServiceImpl implements ItemService {
Category category = categoryRepository.findById(categoryId)
.orElseThrow(() -> new NotExistItemException("존재하지 않는 카테고리 입니다."));
//create Item
Item item = Item.createdFullItem(category,store,new ArrayList<>() ,itemName, itemPrice);
List<ItemOption> itemOptions = itemOptionDtos
.stream()
.map(itemOptionDto -> {
return ItemOption.of(itemOptionDto.getOptionType(), itemOptionDto.getName());
})
.collect(Collectors.toList());
//add ItemOption
itemOptionDtos.forEach((itemOptionDto ->
itemOptionRepository.save(ItemOptionDto.createItemOption(itemOptionDto, item))));
Item createdItem = Item.of(itemName, itemPrice, category, store, itemOptions);
itemRepository.save(createdItem);
}
}

View File

@@ -129,10 +129,8 @@ public class ItemOwnerApiController {
private String name;
public ItemOptionResponse(ItemOptionDto itemOptionDto) {
this.id = itemOptionDto.getId();
this.optionType = itemOptionDto.getOptionType();
this.price = itemOptionDto.getPrice();
this.name = itemOptionDto.getName();
}
}
@@ -182,13 +180,11 @@ public class ItemOwnerApiController {
private Long id;
private String name;
private OptionType optionType;
private Long price;
public static ItemOptionDto createItemOptionDto(ItemOptionRequest itemOptionRequest){
return ItemOptionDto.builder()
.id(itemOptionRequest.getId())
.name(itemOptionRequest.getName())
.price(itemOptionRequest.getPrice())
.optionType(itemOptionRequest.getOptionType())
.build();

View File

@@ -20,20 +20,15 @@ public class ItemOptionDto {
private OptionType optionType;
private Long price;
private String name;
public ItemOptionDto (ItemOption itemOption){
this.id = itemOption.getId();
this.optionType = itemOption.getOptionType();
this.price = itemOption.getPrice();
this.name = itemOption.getName();
}
public static ItemOption createItemOption (ItemOptionDto itemOptionDto, Item item){
return new ItemOption(itemOptionDto.getOptionType(),itemOptionDto.getPrice(),itemOptionDto.getName(),item);
return new ItemOption(itemOptionDto.getOptionType(), itemOptionDto.getName(),item);
}
}

View File

@@ -22,8 +22,6 @@ public class ItemOption extends BaseEntity {
@Enumerated(EnumType.STRING)
private OptionType optionType;
private Long price;
private String name;
@ManyToOne(fetch = LAZY , cascade = CascadeType.ALL)
@@ -36,10 +34,16 @@ public class ItemOption extends BaseEntity {
item.getItemOptions().add(this);
}
public ItemOption(OptionType optionType, Long price, String name, Item item) {
public ItemOption(OptionType optionType, String name, Item item) {
this.optionType = optionType;
this.price = price;
this.name = name;
this.item = item;
}
public static ItemOption of(OptionType type, String name) {
ItemOption itemOption = new ItemOption();
itemOption.optionType = type;
itemOption.name = name;
return itemOption;
}
}