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 3 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
@@ -1,6 +1,5 @@
package in.koreatech.koin.domain.shop.model;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand Down Expand Up @@ -29,7 +28,7 @@ public class MenuCategoryMap {
@JoinColumn(name = "shop_menu_id")
private Menu menu;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@ManyToOne(fetch = FetchType.LAZY)
Copy link
Member

Choose a reason for hiding this comment

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

상점 저장시 메뉴 카테고리가 저장되도록 cascade ALL 옵션 걸어줬는데 이것도 바꾸는 게 좋을까요??
(수정된 방식에서도 메뉴 카테고리를 암시적으로 먼저 저장해줘야 에러가 안나는 상황인데, 이게 좋지 않을 것 같아서
메뉴와 메뉴카테고리를 저장하는 순서가 어떤 순서든지 저장가능하도록 하는 게 베스트 같아서요)

CasCade 옵션이 말썽이군요 😅
상점과 메뉴 카테고리는 같은 생명주기를 가지지 않는다고 생각해요
해당 상황에서는 cascade 옵션을 제거해줘도 문제없다고 생각합니다.

메뉴와 메뉴카테고리를 저장하는 순서가 어떤 순서든지 저장가능하도록 하는 게 베스트 같아서요

해당 의견에 동의합니다~
이렇게되면 shopMenuCategoryMap은 사용하지 않게 되려나요?

shop_menu_category_map 상의 연결 컬럼이 nullable하게 구성되어야할거 같네요? 제가 맞게 이해한걸까요~?
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

저는 ~~map 테이블은 N:M 관계를 매핑하기 위한 중간 테이블 목적으로 설계해서 필요하다고 생각합니다~

@JoinColumn(name = "shop_menu_category_id")
private MenuCategory menuCategory;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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);

MenuCategory save(MenuCategory menuCategory);
}
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);
}
}
70 changes: 70 additions & 0 deletions src/test/java/in/koreatech/koin/acceptance/ShopApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import in.koreatech.koin.domain.shop.model.MenuCategoryMap;
import in.koreatech.koin.domain.shop.model.MenuImage;
import in.koreatech.koin.domain.shop.model.MenuOption;
import in.koreatech.koin.domain.shop.repository.MenuCategoryRepository;
import in.koreatech.koin.domain.shop.repository.MenuRepository;
import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
Expand All @@ -22,6 +23,9 @@ class ShopApiTest extends AcceptanceTest {
@Autowired
private MenuRepository menuRepository;

@Autowired
private MenuCategoryRepository menuCategoryRepository;

@Test
@DisplayName("옵션이 하나 있는 상점의 메뉴를 조회한다.")
void findMenuSingleOption() {
Expand Down Expand Up @@ -54,6 +58,7 @@ void findMenuSingleOption() {

menuCategoryMap.map(menu, menuCategory);

menuCategoryRepository.save(menuCategory);
menuRepository.save(menu);

ExtractableResponse<Response> response = RestAssured
Expand Down Expand Up @@ -133,6 +138,7 @@ void findMenuMultipleOption() {

menuCategoryMap.map(menu, menuCategory);

menuCategoryRepository.save(menuCategory);
menuRepository.save(menu);

ExtractableResponse<Response> response = RestAssured
Expand Down Expand Up @@ -177,4 +183,68 @@ 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);

menuCategoryRepository.save(menuCategory1);
menuCategoryRepository.save(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