-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: menu sorting 구현 #273
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
Changes from all commits
01d7db0
a7fc589
1bc506f
14b2050
d9de302
9652741
8e24938
609be66
4d189d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.nowait.applicationadmin.menu.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.PositiveOrZero; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| @Builder | ||
| public class MenuSortUpdateRequest { | ||
| @NotNull | ||
| private Long menuId; | ||
| @NotNull | ||
| @PositiveOrZero | ||
| private Long sortOrder; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package com.nowait.applicationadmin.menu.service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
@@ -10,6 +11,7 @@ | |
| import com.nowait.applicationadmin.menu.dto.MenuImageUploadResponse; | ||
| import com.nowait.applicationadmin.menu.dto.MenuReadDto; | ||
| import com.nowait.applicationadmin.menu.dto.MenuReadResponse; | ||
| import com.nowait.applicationadmin.menu.dto.MenuSortUpdateRequest; | ||
| import com.nowait.applicationadmin.menu.dto.MenuUpdateRequest; | ||
| import com.nowait.common.enums.Role; | ||
| import com.nowait.domaincorerdb.menu.entity.Menu; | ||
|
|
@@ -60,7 +62,7 @@ public MenuReadResponse getAllMenusByStoreId(Long storeId, MemberDetails memberD | |
|
|
||
| // 사용자 역할이 SUPER_ADMIN이거나, storeId가 일치하는지 확인 | ||
| validateMenuViewAuthorization(user, storeId); | ||
| List<Menu> menus = menuRepository.findAllByStoreIdAndDeletedFalse(storeId); | ||
| List<Menu> menus = menuRepository.findAllByStoreIdAndDeletedFalseOrderBySortOrder(storeId); | ||
|
|
||
| List<MenuReadDto> menuReadResponse = menus.stream() | ||
| .map(menu -> { | ||
|
|
@@ -123,6 +125,48 @@ public MenuReadDto updateMenu(Long menuId, MenuUpdateRequest request, MemberDeta | |
| return MenuReadDto.fromEntity(saved, imageDto); | ||
| } | ||
|
|
||
| @Transactional | ||
| public String updateMenuSortOrder(List<MenuSortUpdateRequest> requests, MemberDetails memberDetails) { | ||
| User user = userRepository.findById(memberDetails.getId()).orElseThrow(UserNotFoundException::new); | ||
|
|
||
| if (!Role.SUPER_ADMIN.equals(user.getRole())) { | ||
| throw new MenuUpdateUnauthorizedException(); | ||
| } | ||
|
|
||
| if (requests == null || requests.isEmpty()) { | ||
| throw new MenuParamEmptyException(); | ||
| } | ||
|
|
||
| if (requests.stream().map(MenuSortUpdateRequest::getMenuId).distinct().count() != requests.size()) { | ||
| throw new IllegalArgumentException("중복된 메뉴 ID가 포함되어 있습니다."); | ||
| } | ||
|
|
||
| if (requests.stream().anyMatch(r -> r.getSortOrder() == null || r.getSortOrder() < 0)) { | ||
| throw new IllegalArgumentException("잘못된 정렬 순서가 포함되어 있습니다. 정렬 순서는 0 이상의 정수여야 합니다."); | ||
| } | ||
|
|
||
| List<Long> ids = requests.stream().map(MenuSortUpdateRequest::getMenuId).toList(); | ||
| List<Menu> menus = menuRepository.findAllById(ids); | ||
| if (menus.size() != ids.size()) { | ||
| throw new MenuNotFoundException(); | ||
| } | ||
|
|
||
| Long storeId = menus.get(0).getStoreId(); | ||
| if (!menus.stream().allMatch(m -> storeId.equals(m.getStoreId()))) { | ||
| throw new IllegalArgumentException("다른 매장의 메뉴가 있습니다."); | ||
|
Comment on lines
+149
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 소프트 삭제 메뉴까지 업데이트될 수 있는 위험
권장 수정:
- List<Menu> menus = menuRepository.findAllById(ids);
+ List<Menu> menus = menuRepository.findAllByIdInAndDeletedFalse(ids);
if (menus.size() != ids.size()) {
throw new MenuNotFoundException();
}대안:
|
||
| } | ||
|
|
||
| Map<Long, Long> idToSort = requests.stream() | ||
| .collect(java.util.stream.Collectors.toMap(MenuSortUpdateRequest::getMenuId, | ||
| MenuSortUpdateRequest::getSortOrder)); | ||
|
|
||
| menus.forEach(m -> m.updateSortOrder(idToSort.get(m.getId()))); | ||
|
|
||
| menuRepository.saveAll(menus); | ||
|
|
||
| return "메뉴 순서가 성공적으로 업데이트되었습니다."; | ||
| } | ||
|
|
||
| @Transactional | ||
| public String deleteMenu(Long menuId, MemberDetails memberDetails) { | ||
| User user = userRepository.findById(memberDetails.getId()).orElseThrow(UserNotFoundException::new); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||
| package com.nowait.applicationadmin.order.dto; | ||||||
|
|
||||||
| import java.time.LocalDateTime; | ||||||
| import java.util.HashMap; | ||||||
| import java.util.LinkedHashMap; | ||||||
| import java.util.Map; | ||||||
|
|
||||||
|
|
@@ -19,14 +20,18 @@ public class OrderResponseDto { | |||||
| private String depositorName; | ||||||
| private Integer totalPrice; | ||||||
| private OrderStatus status; | ||||||
| private Map<String, MenuDetail> menuDetails; | ||||||
| private HashMap<String, MenuDetail> menuDetails; | ||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| private LocalDateTime createdAt; | ||||||
|
|
||||||
| public static OrderResponseDto fromEntity(UserOrder userOrder) { | ||||||
| Map<String, MenuDetail> menuDetails = new LinkedHashMap<>(); | ||||||
| HashMap<String, MenuDetail> menuDetails = new LinkedHashMap<>(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 선언-구현 타입 불일치 — 목적(순서 보존)에 맞게 Map 또는 LinkedHashMap으로 선언 정리 변수 타입은 HashMap인데 실제 인스턴스는 LinkedHashMap입니다. 읽기 혼란을 줄이고 의도를 드러내기 위해 인터페이스(Map)로 선언하거나 LinkedHashMap으로 맞춰 주세요. (아래 필드 타입 변경과도 일관성을 가집니다.) - HashMap<String, MenuDetail> menuDetails = new LinkedHashMap<>();
+ Map<String, MenuDetail> menuDetails = new LinkedHashMap<>();📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| for (OrderItem item : userOrder.getOrderItems()) { | ||||||
| String displayName = item.getMenu().getAdminDisplayName(); | ||||||
| String adminDisplayName = item.getMenu().getAdminDisplayName(); | ||||||
| String displayName = (adminDisplayName == null || adminDisplayName.isBlank()) | ||||||
| ? item.getMenu().getName() // 관리자 표시명이 없거나 공백이면 일반 메뉴명 사용 | ||||||
| : adminDisplayName; | ||||||
|
|
||||||
| int quantity = item.getQuantity(); | ||||||
| int price = item.getMenu().getPrice(); // 메뉴 단가 | ||||||
|
|
||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.