restuarant api

This commit is contained in:
haerong22
2020-11-23 19:40:03 +09:00
parent b9ae7e22da
commit d745710e33
6 changed files with 76 additions and 9 deletions

View File

@@ -0,0 +1,14 @@
package com.example.eatgo.domain;
public class MenuItem {
private final String name;
public MenuItem(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@@ -0,0 +1,7 @@
package com.example.eatgo.domain;
import java.util.List;
public interface MenuItemRepository {
List<MenuItem> findAllByRestaurantId(Long restaurantId);
}

View File

@@ -0,0 +1,13 @@
package com.example.eatgo.domain;
import java.util.ArrayList;
import java.util.List;
public class MenuItemRepositoryImpl implements MenuItemRepository {
@Override
public List<MenuItem> findAllByRestaurantId(Long restaurantId) {
List<MenuItem> menuItems = new ArrayList<>();
menuItems.add(new MenuItem("kimchi"));
return menuItems;
}
}

View File

@@ -1,11 +1,17 @@
package com.example.eatgo.domain;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.ArrayList;
import java.util.List;
public class Restaurant {
private final String name;
private final String address;
private final Long id;
private String name;
private String address;
private Long id;
private List<MenuItem> menuItems = new ArrayList<>();
public Restaurant(Long id, String name, String address) {
this.name = name;
@@ -28,4 +34,18 @@ public class Restaurant {
public String getAddress() {
return address;
}
public List<MenuItem> getMenuItems() {
return menuItems;
}
public void addMenuItem(MenuItem menuItem) {
menuItems.add(menuItem);
}
public void setMenuItems(List<MenuItem> menuItems) {
for(MenuItem menuItem : menuItems) {
addMenuItem(menuItem);
}
}
}

View File

@@ -1,8 +1,9 @@
package com.example.eatgo.interfaces;
import com.example.eatgo.domain.MenuItem;
import com.example.eatgo.domain.MenuItemRepository;
import com.example.eatgo.domain.Restaurant;
import com.example.eatgo.domain.RestaurantRepository;
import com.example.eatgo.domain.RestaurantRepositoryImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -14,14 +15,20 @@ import java.util.List;
@RequiredArgsConstructor
public class RestaurantController {
private final RestaurantRepository repository;
private final RestaurantRepository restaurantRepository;
private final MenuItemRepository menuItemRepository;
@GetMapping("/restaurants")
public List<Restaurant> list() {
return repository.findAll();
return restaurantRepository.findAll();
}
@GetMapping("restaurants/{id}")
public Restaurant detail(@PathVariable Long id) {
return repository.findById(id);
Restaurant restaurant = restaurantRepository.findById(id);
List<MenuItem> menuItemList = menuItemRepository.findAllByRestaurantId(id);
restaurant.setMenuItems(menuItemList);
return restaurant;
}
}

View File

@@ -1,5 +1,7 @@
package com.example.eatgo.interfaces;
import com.example.eatgo.domain.MenuItemRepository;
import com.example.eatgo.domain.MenuItemRepositoryImpl;
import com.example.eatgo.domain.RestaurantRepository;
import com.example.eatgo.domain.RestaurantRepositoryImpl;
import org.hamcrest.core.StringContains;
@@ -19,12 +21,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest(RestaurantController.class)
class RestaurantControllerTest {
@ Autowired
@Autowired
private MockMvc mvc;
@SpyBean(RestaurantRepositoryImpl.class)
private RestaurantRepository restaurantRepository;
@SpyBean(MenuItemRepositoryImpl.class)
private MenuItemRepository menuItemRepository;
@Test
public void list() throws Exception {
mvc.perform(get("/restaurants"))
@@ -38,7 +43,8 @@ class RestaurantControllerTest {
mvc.perform(get("/restaurants/1004"))
.andExpect(status().isOk())
.andExpect(content().string(StringContains.containsString("\"id\":1004")) )
.andExpect(content().string(StringContains.containsString("\"name\":\"Bob zip\"")) );
.andExpect(content().string(StringContains.containsString("\"name\":\"Bob zip\"")) )
.andExpect(content().string(StringContains.containsString("kimchi")));
mvc.perform(get("/restaurants/2020"))
.andExpect(status().isOk())