feat(owner-vue, store-service): 메뉴 등록
메뉴 등록 /수정
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
:name="modalSet.words.register"
|
||||
@save="itemSave"
|
||||
@addItemOption="addItemOption"
|
||||
@init="getModalData"
|
||||
/>
|
||||
|
||||
</v-subheader>
|
||||
@@ -162,7 +163,7 @@ export default {
|
||||
var vm =this;
|
||||
this.modalData = {
|
||||
itemName : '',
|
||||
itemPrice : 0,
|
||||
itemPrice : '',
|
||||
categoryId: 0,
|
||||
categoryList : [],
|
||||
requiredOption : [],
|
||||
@@ -190,9 +191,7 @@ export default {
|
||||
responseType:'json'
|
||||
})
|
||||
.then(function (response) {
|
||||
console.log(response)
|
||||
var item = response.data.data;
|
||||
console.log(item)
|
||||
vm.modalData.itemId = item.id;
|
||||
vm.modalData.itemName = item.name;
|
||||
vm.modalData.itemPrice = item.price;
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
>
|
||||
<v-text-field
|
||||
v-model="modalData.itemName"
|
||||
:rules="[() => !!modalData.itemName || 'This field is required']"
|
||||
label="이름*"
|
||||
required
|
||||
/>
|
||||
@@ -38,6 +39,7 @@
|
||||
>
|
||||
<v-text-field
|
||||
v-model="modalData.itemPrice"
|
||||
:rules="[() => !!modalData.itemPrice || 'This field is required']"
|
||||
label="가격*"
|
||||
hint="example of helper text only on focus"
|
||||
/>
|
||||
@@ -49,6 +51,7 @@
|
||||
<v-select
|
||||
v-model="modalData.categoryId"
|
||||
:items="modalData.categoryList"
|
||||
:rules="[() => !!modalData.categoryId || 'This field is required']"
|
||||
item-text="name"
|
||||
item-value="categoryId"
|
||||
label="카테고리*"
|
||||
@@ -149,7 +152,7 @@ export default {
|
||||
modalData: {
|
||||
itemName : String,
|
||||
itemPrice : Number,
|
||||
category: String,
|
||||
categoryId: String,
|
||||
categoryList : Array,
|
||||
requiredOption : Array,
|
||||
otherOption : Array,
|
||||
@@ -160,8 +163,6 @@ export default {
|
||||
},
|
||||
methods:{
|
||||
save : function () {
|
||||
console.log('save!')
|
||||
this.dialog = false
|
||||
this.$emit('save')
|
||||
},
|
||||
addItemOption : function (itemOptionValue,optionType){
|
||||
|
||||
@@ -14,6 +14,8 @@ import javax.persistence.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static javax.persistence.CascadeType.PERSIST;
|
||||
import static javax.persistence.CascadeType.REMOVE;
|
||||
import static javax.persistence.FetchType.LAZY;
|
||||
|
||||
@Entity
|
||||
@@ -43,7 +45,7 @@ public class Item extends BaseEntity {
|
||||
@JoinColumn(name = "store_id")
|
||||
private Store store;
|
||||
|
||||
@OneToMany(mappedBy = "item")
|
||||
@OneToMany(mappedBy = "item" ,cascade = {REMOVE,PERSIST} )
|
||||
private List<ItemOption> itemOptions;
|
||||
|
||||
// == 연관관계 편의 메소드 ==//
|
||||
@@ -62,9 +64,10 @@ public class Item extends BaseEntity {
|
||||
category.getItems().add(this);
|
||||
}
|
||||
|
||||
public void setItemNameAndPrice(String name , Long price){
|
||||
public void setItemNameAndPriceAndCategory(String name , Long price,Category category){
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
// == 생성 메소드 == //
|
||||
@@ -75,4 +78,12 @@ public class Item extends BaseEntity {
|
||||
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);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,5 +14,7 @@ public interface ItemService {
|
||||
|
||||
Page<ItemDto> findItemList(Long storeId,String word, Pageable pageable);
|
||||
|
||||
void putItem(Long itemId, String itemName, Long itemPrice, List<ItemOptionDto> itemOption);
|
||||
void putItem(Long itemId, String itemName, Long itemPrice, Long categoryId, List<ItemOptionDto> itemOptionDtos);
|
||||
|
||||
void createItem( Long storeId, String itemName, Long itemPrice, Long categoryId, List<ItemOptionDto> itemOptionDtos);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.justpickup.storeservice.domain.item.service;
|
||||
|
||||
import com.justpickup.storeservice.domain.category.entity.Category;
|
||||
import com.justpickup.storeservice.domain.category.repository.CategoryRepository;
|
||||
import com.justpickup.storeservice.domain.item.dto.ItemDto;
|
||||
import com.justpickup.storeservice.domain.item.entity.Item;
|
||||
import com.justpickup.storeservice.domain.item.exception.NotExistItemException;
|
||||
@@ -8,6 +10,7 @@ import com.justpickup.storeservice.domain.item.repository.ItemRepositoryCustom;
|
||||
import com.justpickup.storeservice.domain.itemoption.dto.ItemOptionDto;
|
||||
import com.justpickup.storeservice.domain.itemoption.entity.ItemOption;
|
||||
import com.justpickup.storeservice.domain.itemoption.repository.ItemOptionRepository;
|
||||
import com.justpickup.storeservice.domain.store.entity.Store;
|
||||
import com.justpickup.storeservice.domain.store.repository.StoreRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -17,8 +20,8 @@ import org.springframework.data.support.PageableExecutionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@@ -28,11 +31,13 @@ import java.util.stream.Collectors;
|
||||
public class ItemServiceImpl implements ItemService {
|
||||
|
||||
private final ItemRepository itemRepository;
|
||||
private final ItemOptionRepository itemOptionRepository;
|
||||
private final ItemRepositoryCustom itemRepositoryCustom;
|
||||
private final ItemOptionRepository itemOptionRepository;
|
||||
private final CategoryRepository categoryRepository;
|
||||
private final StoreRepository storeRepository;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public ItemDto findItemByItemId(Long itemId) {
|
||||
Item findItem = itemRepository.findById(itemId)
|
||||
@@ -52,20 +57,48 @@ public class ItemServiceImpl implements ItemService {
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void putItem(Long itemId, String itemName, Long itemPrice, List<ItemOptionDto> itemOptionDtos) {
|
||||
public void putItem(Long itemId,
|
||||
String itemName,
|
||||
Long itemPrice,
|
||||
Long categoryId,
|
||||
List<ItemOptionDto> itemOptionDtos) {
|
||||
|
||||
Item item = itemRepository.findById(itemId)
|
||||
.orElseThrow(() -> new NotExistItemException("존재하지 않는 아이템 입니다."));
|
||||
|
||||
item.setItemNameAndPrice(itemName,itemPrice);
|
||||
itemOptionDtos.stream()
|
||||
.map(itemOptionDto -> {
|
||||
if(itemOptionDto.getId()==null)
|
||||
return ItemOptionDto.createItemOption(itemOptionDto,item);
|
||||
else
|
||||
return itemOptionRepository.findById(itemOptionDto.getId())
|
||||
.orElseThrow(() -> new NotExistItemException("존재하지 않는 아이템 옵션 입니다."));
|
||||
})
|
||||
.forEach(itemOptionRepository::save);
|
||||
Category category = categoryRepository.findById(categoryId)
|
||||
.orElseThrow(() -> new NotExistItemException("존재하지 않는 카테고리 입니다."));
|
||||
|
||||
item.setItemNameAndPriceAndCategory(itemName,itemPrice,category);
|
||||
|
||||
itemOptionDtos
|
||||
.forEach(itemOptionDto -> {
|
||||
if (itemOptionRepository.existsById(itemOptionDto.getId()))
|
||||
itemOptionRepository.save(ItemOptionDto.createItemOption(itemOptionDto, item));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void createItem(Long storeId,
|
||||
String itemName,
|
||||
Long itemPrice,
|
||||
Long categoryId,
|
||||
List<ItemOptionDto> itemOptionDtos) {
|
||||
|
||||
//find Store
|
||||
Store store = storeRepository.findById(storeId)
|
||||
.orElseThrow(() -> new NotExistItemException("존재하지 않는 매장 입니다."));
|
||||
|
||||
//find Category
|
||||
Category category = categoryRepository.findById(categoryId)
|
||||
.orElseThrow(() -> new NotExistItemException("존재하지 않는 카테고리 입니다."));
|
||||
|
||||
//create Item
|
||||
Item item = Item.createdFullItem(category,store,new ArrayList<>() ,itemName, itemPrice);
|
||||
|
||||
//add ItemOption
|
||||
itemOptionDtos.forEach((itemOptionDto ->
|
||||
itemOptionRepository.save(ItemOptionDto.createItemOption(itemOptionDto, item))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -131,19 +133,20 @@ public class ItemController {
|
||||
}
|
||||
|
||||
@PutMapping("/item")
|
||||
public ResponseEntity<Result> putItem(@RequestBody ItemRequest putItemRequest){
|
||||
public ResponseEntity<Result> putItem(@RequestBody @Valid ItemRequest itemRequest){
|
||||
|
||||
List<ItemOptionDto> itemOption = putItemRequest.getRequiredOption().stream().map(ItemRequest.ItemOptionRequest::createItemDto).collect(Collectors.toList());
|
||||
itemOption.addAll(putItemRequest.getOtherOption().stream().map(ItemRequest.ItemOptionRequest::createItemDto).collect(Collectors.toList()));
|
||||
List<ItemOptionDto> itemOption = itemRequest.getRequiredOption().stream().map(ItemRequest.ItemOptionRequest::createItemDto).collect(Collectors.toList());
|
||||
itemOption.addAll(itemRequest.getOtherOption().stream().map(ItemRequest.ItemOptionRequest::createItemDto).collect(Collectors.toList()));
|
||||
|
||||
itemService.putItem(putItemRequest.getItemId()
|
||||
, putItemRequest.getItemName()
|
||||
, putItemRequest.getItemPrice()
|
||||
itemService.putItem(itemRequest.getItemId()
|
||||
, itemRequest.getItemName()
|
||||
, itemRequest.getItemPrice()
|
||||
, itemRequest.getCategoryId()
|
||||
, itemOption);
|
||||
|
||||
|
||||
return ResponseEntity.status(HttpStatus.NO_CONTENT)
|
||||
.body(Result.createSuccessResult(null));
|
||||
.body(Result.success());
|
||||
}
|
||||
|
||||
@Data
|
||||
@@ -152,8 +155,12 @@ public class ItemController {
|
||||
@Builder
|
||||
public static class ItemRequest {
|
||||
private Long itemId;
|
||||
@NotNull
|
||||
private String itemName;
|
||||
@NotNull
|
||||
private Long itemPrice;
|
||||
@NotNull
|
||||
private Long categoryId;
|
||||
private List<ItemOptionRequest> requiredOption;
|
||||
private List<ItemOptionRequest> otherOption;
|
||||
|
||||
@@ -179,4 +186,24 @@ public class ItemController {
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/item")
|
||||
public ResponseEntity createItem( @RequestBody @Valid ItemRequest itemRequest){
|
||||
|
||||
Long storeId = 1L;
|
||||
|
||||
List<ItemOptionDto> itemOption = itemRequest.getRequiredOption().stream().map(ItemRequest.ItemOptionRequest::createItemDto).collect(Collectors.toList());
|
||||
itemOption.addAll(itemRequest.getOtherOption().stream().map(ItemRequest.ItemOptionRequest::createItemDto).collect(Collectors.toList()));
|
||||
|
||||
itemService.createItem(storeId
|
||||
, itemRequest.getItemName()
|
||||
, itemRequest.getItemPrice()
|
||||
, itemRequest.getCategoryId()
|
||||
, itemOption);
|
||||
|
||||
|
||||
return ResponseEntity.status(HttpStatus.CREATED)
|
||||
.body(Result.success());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class ItemOption extends BaseEntity {
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne(fetch = LAZY)
|
||||
@ManyToOne(fetch = LAZY , cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "item_id")
|
||||
private Item item;
|
||||
|
||||
|
||||
@@ -33,4 +33,12 @@ public class Result<T> {
|
||||
.data(data)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static Result success(){
|
||||
return Result.builder()
|
||||
.code(Code.SUCCESS)
|
||||
.message("성공")
|
||||
.data(null)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user