레스토랑 예약 사이트 만들기 2 - 메뉴 정보 추가(관리자)

레스토랑에 메뉴정보를 담기

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Restaurant {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
private Long categoryId;

@NotEmpty
private String name;

@NotEmpty
private String address;

@Transient
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<MenuItem> menuItems;

public void addMenuItem(MenuItem menuItem){
menuItems.add(menuItem);
}

public void setMenuItems(List<MenuItem> menuItems){
this.menuItems = new ArrayList<>(menuItems);
}

public String getInformation(){
return name + " in " + address;
}
}

메뉴에 대한 정보를 담고있는 MenuItem 클래스

@Builder
@Entity
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class MenuItem {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long restaurantId;
private String name;

@Transient
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean destroy;

public MenuItem(String name){
this.name = name;
}
}

레스토랑에서 메뉴정보 가져오기

public interface MenuItemRepository extends JpaRepository<MenuItem, Long> {
List<MenuItem> findAllByRestaurantId(Long restaurantId);
}

레스토랑에 메뉴등록 요청 보내기

추가하고자 하는 데이터는 Http Body에 넣어서 보낸다.

@RestController
@RequiredArgsConstructor
public class MenuItemController {
private final MenuItemService menuItemService;

@PatchMapping("/restaurants/{restaurantId}/menuitems")
public String bulkUpdate(@PathVariable Long restaurantId
, @RequestBody List<MenuItem> menuItems){
menuItemService.bulkUpdate(restaurantId, menuItems);
return "";
}
}

레스토랑 메뉴등록 요청에 대한 테스트 코드

@WebMvcTest(MenuItemController.class)
class MenuItemControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private MenuItemService menuItemService;

@Test
public void bulkUpdate() throws Exception {
ResultActions resultActions = mockMvc.perform(patch("/restaurants/1/menuitems")
.contentType(MediaType.APPLICATION_JSON)
.content("[]"));

resultActions
.andExpect(status().isOk());

verify(menuItemService).bulkUpdate(eq(1L), any());
}
}

레스토랑 정보를 가져와 메뉴정보 넣기

@Service
@RequiredArgsConstructor
public class MenuItemService {
private final MenuItemRepository menuItemRepository;

public void bulkUpdate(Long restaurantId, List<MenuItem> menuItems) {
for(MenuItem menuItem : menuItems){
updateOrDeleteMenuItems(restaurantId, menuItem);
}
}

private void updateOrDeleteMenuItems(Long restaurantId, MenuItem menuItem) {
if(menuItem.isDestroy()){
menuItemRepository.deleteById(menuItem.getId());
}else {
menuItem.setRestaurantId(restaurantId);
menuItemRepository.save(menuItem);
}
}
}
@Test
public void bulkUpdate() {
List<MenuItem> menuItems = new ArrayList<>();
menuItems.add(MenuItem.builder()
.name("Kimchi")
.build());
menuItems.add(MenuItem.builder()
.name("Gukbob")
.build());

menuItemService.bulkUpdate(1L, menuItems);

verify(menuItemRepository, times(2)).save(any());
}

@Test
public void 메뉴를_삭제한다() {
List<MenuItem> menuItems = new ArrayList<>();
menuItems.add(MenuItem.builder()
.name("Kimchi")
.build());
menuItems.add(MenuItem.builder()
.name("Gukbob")
.build());
menuItems.add(MenuItem.builder()
.id(1004L)
.destroy(true)
.build());

menuItemService.bulkUpdate(1L, menuItems);

verify(menuItemRepository, times(2)).save(any());
verify(menuItemRepository, times(1)).deleteById(eq(1004L));
}
Share