Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 메뉴 카테고리 목록 조회 #153

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import in.koreatech.koin.domain.shop.dto.MenuCategoriesResponse;
import in.koreatech.koin.domain.shop.dto.ShopMenuResponse;
import in.koreatech.koin.domain.shop.service.ShopService;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,4 +21,10 @@ public ResponseEntity<ShopMenuResponse> findMenu(@PathVariable Long shopId, @Pat
ShopMenuResponse shopMenu = shopService.findMenu(menuId);
return ResponseEntity.ok(shopMenu);
}

@GetMapping("/shops/{shopId}/menus/categories")
public ResponseEntity<MenuCategoriesResponse> findMenuCategories(@PathVariable Long shopId) {
MenuCategoriesResponse menuCategories = shopService.getMenuCategories(shopId);
return ResponseEntity.ok(menuCategories);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package in.koreatech.koin.domain.shop.dto;

import java.util.List;

import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.domain.shop.model.MenuCategory;

@JsonNaming(value = SnakeCaseStrategy.class)
public record MenuCategoriesResponse(Long count, List<MenuCategoryResponse> menuCategories) {
public static MenuCategoriesResponse from(List<MenuCategory> menuCategories) {
List<MenuCategoryResponse> categories = menuCategories.stream()
.map(menuCategory -> MenuCategoryResponse.of(menuCategory.getId(), menuCategory.getName()))
.toList();

return new MenuCategoriesResponse((long)categories.size(), categories);
}

private record MenuCategoryResponse(Long id, String name) {
public static MenuCategoryResponse of(Long id, String name) {
return new MenuCategoryResponse(id, name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package in.koreatech.koin.domain.shop.repository;

import java.util.List;

import org.springframework.data.repository.Repository;

import in.koreatech.koin.domain.shop.model.MenuCategory;

public interface MenuCategoryRepository extends Repository<MenuCategory, Long> {
List<MenuCategory> findAllByShopId(Long shopId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import in.koreatech.koin.domain.shop.dto.MenuCategoriesResponse;
import in.koreatech.koin.domain.shop.dto.ShopMenuResponse;
import in.koreatech.koin.domain.shop.model.Menu;
import in.koreatech.koin.domain.shop.model.MenuCategory;
import in.koreatech.koin.domain.shop.model.MenuCategoryMap;
import in.koreatech.koin.domain.shop.dto.ShopMenuResponse;
import in.koreatech.koin.domain.shop.repository.MenuCategoryRepository;
import in.koreatech.koin.domain.shop.repository.MenuRepository;
import lombok.RequiredArgsConstructor;

Expand All @@ -18,6 +20,7 @@
public class ShopService {

private final MenuRepository menuRepository;
private final MenuCategoryRepository menuCategoryRepository;

public ShopMenuResponse findMenu(Long menuId) {
Menu menu = menuRepository.findById(menuId)
Expand All @@ -37,4 +40,10 @@ private ShopMenuResponse createShopMenuResponse(Menu menu, List<MenuCategory> me
}
return ShopMenuResponse.createForSingleOption(menu, menuCategories);
}

public MenuCategoriesResponse getMenuCategories(Long shopId) {
//TODO 존재하는 상점인지 검증하고, 없다면 401를 반환하여야 한다. 작업시점: 상점 도메인 조회 기능 추가시
List<MenuCategory> menuCategories = menuCategoryRepository.findAllByShopId(shopId);
return MenuCategoriesResponse.from(menuCategories);
}
}
62 changes: 62 additions & 0 deletions src/test/java/in/koreatech/koin/acceptance/ShopApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,66 @@ void findMenuMultipleOption() {
}
);
}

@Test
@DisplayName("상점의 메뉴 카테고리들을 조회한다.")
void findShopMenuCategories() {
// given
final long SHOP_ID = 1L;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

실제로 Shop Entity를 하나 save() 하고 그 ID를 활용하는건 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상점 객체는 아직 안만들기도 했고, 일부로 의존하지 않도록 의존관계가 아니라 id만 유지하도록 해서 필요하지 않을 것 같습니다!


Menu menu = Menu.builder()
.shopId(SHOP_ID)
.name("짜장면")
.description("맛있는 짜장면")
.build();

MenuCategory menuCategory1 = MenuCategory.builder()
.shopId(SHOP_ID)
.name("이벤트 메뉴")
.build();

MenuCategory menuCategory2 = MenuCategory.builder()
.shopId(SHOP_ID)
.name("메인 메뉴")
.build();

MenuCategoryMap menuCategoryMap1 = MenuCategoryMap.create();
MenuCategoryMap menuCategoryMap2 = MenuCategoryMap.create();

// when then
menuCategoryMap1.map(menu, menuCategory1);
menuCategoryMap2.map(menu, menuCategory2);

menuRepository.save(menu);

ExtractableResponse<Response> response = RestAssured
.given()
.log().all()
.when()
.log().all()
.get("/shops/{shopId}/menus/categories", menu.getShopId())
.then()
.log().all()
.statusCode(HttpStatus.OK.value())
.extract();

SoftAssertions.assertSoftly(
softly -> {
softly.assertThat(response.body().jsonPath().getLong("count")).isEqualTo(2);

softly.assertThat(response.body().jsonPath().getList("menu_categories"))
.hasSize(2);

softly.assertThat(response.body().jsonPath().getLong("menu_categories[0].id"))
.isEqualTo(menuCategory1.getId());
softly.assertThat(response.body().jsonPath().getString("menu_categories[0].name"))
.isEqualTo(menuCategory1.getName());

softly.assertThat(response.body().jsonPath().getLong("menu_categories[1].id"))
.isEqualTo(menuCategory2.getId());
softly.assertThat(response.body().jsonPath().getString("menu_categories[1].name"))
.isEqualTo(menuCategory2.getName());
}
);
}
}
Loading