fix(store): store 엔티티 연관관계 메소드 변경 및 테스트 데이터 정합성 문제 해결
- 연관관계 메소드 리스트에 같은 데이터가 두번 들어가는 문제점 해결 - 테스트 데이터 정합성 문제 해결
This commit is contained in:
@@ -8,6 +8,7 @@ import lombok.Getter;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static javax.persistence.FetchType.LAZY;
|
import static javax.persistence.FetchType.LAZY;
|
||||||
@@ -31,12 +32,11 @@ public class Category extends BaseEntity {
|
|||||||
private Store store;
|
private Store store;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
|
||||||
private List<Item> items;
|
private List<Item> items = new ArrayList<>();
|
||||||
|
|
||||||
// == 연관관계 편의 메소드 == //
|
// == 연관관계 편의 메소드 == //
|
||||||
public void setStore(Store store) {
|
public void setStore(Store store) {
|
||||||
this.store = store;
|
this.store = store;
|
||||||
store.getCategories().add(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addItem(Item item) {
|
public void addItem(Item item) {
|
||||||
|
|||||||
@@ -54,12 +54,10 @@ public class Item extends BaseEntity {
|
|||||||
|
|
||||||
public void setStore(Store store) {
|
public void setStore(Store store) {
|
||||||
this.store = store;
|
this.store = store;
|
||||||
store.getItems().add(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCategory(Category category) {
|
public void setCategory(Category category) {
|
||||||
this.category = category;
|
this.category = category;
|
||||||
category.getItems().add(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setItemNameAndPriceAndCategory(String name , Long price,Category category){
|
public void setItemNameAndPriceAndCategory(String name , Long price,Category category){
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public class ItemOption extends BaseEntity {
|
|||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@ManyToOne(fetch = LAZY , cascade = CascadeType.ALL)
|
@ManyToOne(fetch = LAZY )
|
||||||
@JoinColumn(name = "item_id")
|
@JoinColumn(name = "item_id")
|
||||||
private Item item;
|
private Item item;
|
||||||
|
|
||||||
|
|||||||
@@ -48,13 +48,13 @@ public class Store extends BaseEntity {
|
|||||||
//== user-service.user pk ==//
|
//== user-service.user pk ==//
|
||||||
private Long userId;
|
private Long userId;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "store")
|
@OneToMany(mappedBy = "store", cascade = CascadeType.ALL)
|
||||||
private List<Category> categories = new ArrayList<>();
|
private List<Category> categories = new ArrayList<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "store")
|
@OneToMany(mappedBy = "store", cascade = CascadeType.ALL)
|
||||||
private List<Item> items = new ArrayList<>();
|
private List<Item> items = new ArrayList<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "store")
|
@OneToMany(mappedBy = "store", cascade = CascadeType.ALL)
|
||||||
private List<FavoriteStore> favoriteStores = new ArrayList<>();
|
private List<FavoriteStore> favoriteStores = new ArrayList<>();
|
||||||
|
|
||||||
// == 연관관계 편의 메소드 == //
|
// == 연관관계 편의 메소드 == //
|
||||||
|
|||||||
@@ -22,11 +22,10 @@ import org.springframework.stereotype.Component;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
//@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SqlCommandLineRunner implements CommandLineRunner {
|
public class SqlCommandLineRunner implements CommandLineRunner {
|
||||||
@@ -105,17 +104,14 @@ public class SqlCommandLineRunner implements CommandLineRunner {
|
|||||||
Category 디카페인 = categoryRepository.save(Category.of("디카페인", 1, store));
|
Category 디카페인 = categoryRepository.save(Category.of("디카페인", 1, store));
|
||||||
Category 티 = categoryRepository.save(Category.of("티", 2, store));
|
Category 티 = categoryRepository.save(Category.of("티", 2, store));
|
||||||
|
|
||||||
ItemOption ice = ItemOption.of(OptionType.REQUIRED, "ICE");
|
Item 아메리카노 = Item.of("아메리카노", 1500L, 카페인, store, List.of(getIceOption(), getHotOption()));
|
||||||
ItemOption hot = ItemOption.of(OptionType.REQUIRED, "HOT");
|
Item 카페라떼 = Item.of("카페라떼", 2000L, 카페인, store, List.of(getIceOption(), getHotOption()));
|
||||||
|
Item 카페모카 = Item.of("카페모카", 3900L, 카페인, store, List.of(getIceOption(), getHotOption()));
|
||||||
Item 아메리카노 = Item.of("아메리카노", 1500L, 카페인, store, List.of(ice, hot));
|
Item 콜드브루 = Item.of("콜드브루", 2500L, 카페인, store, List.of(getIceOption()));
|
||||||
Item 카페라떼 = Item.of("카페라떼", 2000L, 카페인, store, List.of(ice, hot));
|
Item 녹차라떼 = Item.of("녹차라떼", 3000L, 디카페인, store, List.of(getIceOption(), getHotOption()));
|
||||||
Item 카페모카 = Item.of("카페모카", 3900L, 카페인, store, List.of(ice, hot));
|
Item 딸기라떼 = Item.of("딸기라떼", 3000L, 디카페인, store, List.of(getIceOption(), getHotOption()));
|
||||||
Item 콜드브루 = Item.of("콜드브루", 2500L, 카페인, store, List.of(ice));
|
Item 녹차 = Item.of("녹차", 3000L, 티, store, List.of(getHotOption()));
|
||||||
Item 녹차라떼 = Item.of("녹차라떼", 3000L, 디카페인, store, List.of(ice, hot));
|
Item 히비스커스 = Item.of("히비스커스 티", 3000L, 티, store, List.of(getIceOption(), getHotOption()));
|
||||||
Item 딸기라떼 = Item.of("딸기라떼", 3000L, 디카페인, store, List.of(ice, hot));
|
|
||||||
Item 녹차 = Item.of("녹차", 3000L, 티, store, List.of(hot));
|
|
||||||
Item 히비스커스 = Item.of("히비스커스 티", 3000L, 티, store, List.of(hot));
|
|
||||||
|
|
||||||
List<Item> items = List.of(아메리카노, 카페라떼, 카페모카, 콜드브루, 녹차라떼, 딸기라떼, 녹차, 히비스커스);
|
List<Item> items = List.of(아메리카노, 카페라떼, 카페모카, 콜드브루, 녹차라떼, 딸기라떼, 녹차, 히비스커스);
|
||||||
itemRepository.saveAll(items);
|
itemRepository.saveAll(items);
|
||||||
@@ -124,6 +120,14 @@ public class SqlCommandLineRunner implements CommandLineRunner {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ItemOption getIceOption() {
|
||||||
|
return ItemOption.of(OptionType.REQUIRED, "ICE");
|
||||||
|
}
|
||||||
|
|
||||||
|
private ItemOption getHotOption() {
|
||||||
|
return ItemOption.of(OptionType.REQUIRED, "HOT");
|
||||||
|
}
|
||||||
|
|
||||||
void createFavoriteStore(FavoriteStoreRepository favoriteStoreRepository, List<Store> stores) {
|
void createFavoriteStore(FavoriteStoreRepository favoriteStoreRepository, List<Store> stores) {
|
||||||
List<Long> userList = List.of(1L,2L,3L,4L,5L,6L,7L);
|
List<Long> userList = List.of(1L,2L,3L,4L,5L,6L,7L);
|
||||||
userList.forEach(userId -> stores.forEach(store -> favoriteStoreRepository.save(FavoriteStore.of(userId, store))));
|
userList.forEach(userId -> stores.forEach(store -> favoriteStoreRepository.save(FavoriteStore.of(userId, store))));
|
||||||
|
|||||||
Reference in New Issue
Block a user