-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 주막 주문 API 구현 #86
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
Open
jhssong
wants to merge
12
commits into
main
Choose a base branch
from
feat/26-pub-order-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fcb8290
chore: Add .DS_Store to .gitignore
jhssong ec8ca30
refactor: 주막 주문 엔티티 패키지 분리
jhssong ebb5f0b
feat: Add quantity field in PubOrder
jhssong deccd4c
feat: 주문 요청/응답 DTO 추가
jhssong 917c32e
feat: 주막 주문 API CRD 구현
jhssong 4b59bff
fix: PubOrderCommandController에 final 키워드 추가
jhssong 1512eb9
chore: PubOrderRepository에 불필요한 import문 제거
jhssong 1dd069a
Merge branch 'main' into feat/26-pub-order-api
jhssong 9b0b888
fix: Quantity 값이 null인 경우 예외처리
jhssong 3743e10
fix: PubOrderRequestDto에 NotEmpty와 Valid 추가
jhssong 1bff5e9
fix: findAllPubMenuByIds를 통해 N+1 쿼리 문제 해결
jhssong b93244c
fix: 메뉴 존재 여부를 사전에 체크
jhssong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| .DS_Store | ||
|
|
||
| .*env | ||
| .env* | ||
| HELP.md | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/kr/co/knuserver/application/pubOrder/PubOrderCommandService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package kr.co.knuserver.application.pubOrder; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
| import kr.co.knuserver.application.pubMenu.PubMenuQueryService; | ||
| import kr.co.knuserver.application.pubTableSession.PubTableSessionQueryService; | ||
| import kr.co.knuserver.domain.pubMenu.entity.PubMenu; | ||
| import kr.co.knuserver.domain.pubOrder.entity.PubOrder; | ||
| import kr.co.knuserver.domain.pubOrder.repository.PubOrderRepository; | ||
| import kr.co.knuserver.domain.pubTableSession.entity.PubTableSession; | ||
| import kr.co.knuserver.global.exception.BusinessErrorCode; | ||
| import kr.co.knuserver.global.exception.BusinessException; | ||
| import kr.co.knuserver.presentation.pubOrder.dto.OrderMenus; | ||
| import kr.co.knuserver.presentation.pubOrder.dto.OrderedMenus; | ||
| import kr.co.knuserver.presentation.pubOrder.dto.PubOrderRequestDto; | ||
| import kr.co.knuserver.presentation.pubOrder.dto.PubOrderResponseDto; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| public class PubOrderCommandService { | ||
|
|
||
| private final PubOrderRepository pubOrderRepository; | ||
| private final PubOrderQueryService pubOrderQueryService; | ||
| private final PubTableSessionQueryService pubTableSessionQueryService; | ||
| private final PubMenuQueryService pubMenuQueryService; | ||
|
|
||
| public PubOrderResponseDto createOrder(PubOrderRequestDto request) { | ||
| PubTableSession pubTableSession = pubTableSessionQueryService.getPubTableSessionById(request.pubTableSessionId()); | ||
|
|
||
| List<Long> menuIds = request.orderMenus().stream() | ||
| .map(OrderMenus::menuId) | ||
| .distinct() | ||
| .toList(); | ||
| List<PubMenu> foundMenus = pubMenuQueryService.findAllPubMenuByIds(menuIds); | ||
|
|
||
| if (foundMenus.size() != menuIds.size()) { | ||
| throw new BusinessException(BusinessErrorCode.PUB_MENU_NOT_FOUND); | ||
| } | ||
|
|
||
| Map<Long, PubMenu> pubMenuMap = foundMenus.stream() | ||
| .collect(Collectors.toMap(PubMenu::getId, Function.identity())); | ||
|
|
||
| List<PubOrder> pubOrders = new ArrayList<>(); | ||
| for (OrderMenus orderMenus : request.orderMenus()) { | ||
| pubOrders.add(PubOrder.createPubOrder(pubTableSession.getId(), orderMenus.menuId(), orderMenus.quantity())); | ||
| } | ||
| List<PubOrder> savedPubOrders = pubOrderRepository.saveAll(pubOrders); | ||
|
|
||
| List<OrderedMenus> orderedMenus = savedPubOrders.stream().map((pubOrder -> { | ||
| PubMenu pubMenu = pubMenuMap.get(pubOrder.getPubMenuId()); | ||
| return OrderedMenus.fromEntity(pubOrder, pubMenu); | ||
| })).toList(); | ||
| return PubOrderResponseDto.fromEntity(request.pubTableSessionId(), orderedMenus); | ||
| } | ||
jhssong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public void deleteOrder(Long pubOrderId) { | ||
| PubOrder pubOrder = pubOrderQueryService.findByOrderId(pubOrderId); | ||
| pubOrderRepository.delete(pubOrder); | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
src/main/java/kr/co/knuserver/application/pubOrder/PubOrderQueryService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package kr.co.knuserver.application.pubOrder; | ||
|
|
||
| import java.util.List; | ||
| import kr.co.knuserver.application.pubMenu.PubMenuQueryService; | ||
| import kr.co.knuserver.domain.pubMenu.entity.PubMenu; | ||
| import kr.co.knuserver.domain.pubOrder.entity.PubOrder; | ||
| import kr.co.knuserver.domain.pubOrder.repository.PubOrderRepository; | ||
| import kr.co.knuserver.global.exception.BusinessErrorCode; | ||
| import kr.co.knuserver.global.exception.BusinessException; | ||
| import kr.co.knuserver.presentation.pubOrder.dto.OrderedMenus; | ||
| import kr.co.knuserver.presentation.pubOrder.dto.PubOrderResponseDto; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Propagation; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true, propagation = Propagation.SUPPORTS) | ||
| public class PubOrderQueryService { | ||
|
|
||
| private final PubOrderRepository pubOrderRepository; | ||
| private final PubMenuQueryService pubMenuQueryService; | ||
|
|
||
| public PubOrder findByOrderId(Long orderId) { | ||
| return pubOrderRepository.findById(orderId).orElseThrow(() -> new BusinessException(BusinessErrorCode.PUB_ORDER_NOT_FOUND)); | ||
| } | ||
|
|
||
| public PubOrderResponseDto getAllByPubTableSessionId(Long pubTableSessionId) { | ||
| List<PubOrder> pubOrderList = pubOrderRepository.findAllByPubTableSessionId(pubTableSessionId); | ||
| List<OrderedMenus> orderedMenus = pubOrderList.stream().map((pubOrder -> { | ||
| PubMenu pubMenu = pubMenuQueryService.findPubMenuById(pubOrder.getPubMenuId()); | ||
| return OrderedMenus.fromEntity(pubOrder, pubMenu); | ||
| })).toList(); | ||
jhssong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return PubOrderResponseDto.fromEntity(pubTableSessionId, orderedMenus); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/kr/co/knuserver/domain/pubOrder/repository/PubOrderRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package kr.co.knuserver.domain.pubOrder.repository; | ||
|
|
||
| import java.util.List; | ||
| import kr.co.knuserver.domain.pubOrder.entity.PubOrder; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface PubOrderRepository extends JpaRepository<PubOrder, Long> { | ||
| List<PubOrder> findAllByPubTableSessionId(Long pubTableSessionId); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...main/java/kr/co/knuserver/presentation/pubOrder/controller/PubOrderCommandController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package kr.co.knuserver.presentation.pubOrder.controller; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import kr.co.knuserver.application.pubOrder.PubOrderCommandService; | ||
| import kr.co.knuserver.presentation.pubOrder.dto.PubOrderRequestDto; | ||
| import kr.co.knuserver.presentation.pubOrder.dto.PubOrderResponseDto; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/admin/v1/pubOrder") | ||
| @RequiredArgsConstructor | ||
| public class PubOrderCommandController { | ||
|
|
||
| private final PubOrderCommandService pubOrderCommandService; | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<PubOrderResponseDto> createOrder(@Valid @RequestBody PubOrderRequestDto request) { | ||
| return ResponseEntity.ok().body(pubOrderCommandService.createOrder(request)); | ||
| } | ||
|
|
||
| @DeleteMapping("/{pubOrderId}") | ||
| public ResponseEntity<Void> deleteOrder(@PathVariable("pubOrderId") Long pubOrderId) { | ||
| pubOrderCommandService.deleteOrder(pubOrderId); | ||
| return ResponseEntity.ok().build(); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/kr/co/knuserver/presentation/pubOrder/controller/PubOrderQueryController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package kr.co.knuserver.presentation.pubOrder.controller; | ||
|
|
||
| import kr.co.knuserver.application.pubOrder.PubOrderQueryService; | ||
| import kr.co.knuserver.presentation.pubOrder.dto.PubOrderResponseDto; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/admin/v1/pubOrder") | ||
| @RequiredArgsConstructor | ||
| public class PubOrderQueryController { | ||
|
|
||
| private final PubOrderQueryService pubOrderQueryService; | ||
|
|
||
| @GetMapping("/{pubTableSessionId}") | ||
| public ResponseEntity<PubOrderResponseDto> getAllByPubTableSessionId(@PathVariable("pubTableSessionId") Long pubTableSessionId) { | ||
| return ResponseEntity.ok(pubOrderQueryService.getAllByPubTableSessionId(pubTableSessionId)); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/kr/co/knuserver/presentation/pubOrder/dto/OrderMenus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package kr.co.knuserver.presentation.pubOrder.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record OrderMenus( | ||
| @NotNull Long menuId, | ||
| @NotNull Integer quantity | ||
| ) { | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/kr/co/knuserver/presentation/pubOrder/dto/OrderedMenus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package kr.co.knuserver.presentation.pubOrder.dto; | ||
|
|
||
| import kr.co.knuserver.domain.pubMenu.entity.PubMenu; | ||
| import kr.co.knuserver.domain.pubOrder.entity.PubOrder; | ||
| import lombok.Builder; | ||
|
|
||
| @Builder | ||
| public record OrderedMenus( | ||
| Long orderId, | ||
| Long menuId, | ||
| String name, | ||
| int price, | ||
| Integer quantity | ||
| ) { | ||
| public static OrderedMenus fromEntity(PubOrder pubOrder, PubMenu pubMenu) { | ||
| return OrderedMenus.builder() | ||
| .orderId(pubOrder.getId()) | ||
| .menuId(pubMenu.getId()) | ||
| .name(pubMenu.getName()) | ||
| .price(pubMenu.getPrice()) | ||
| .quantity(pubOrder.getQuantity()) | ||
| .build(); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/kr/co/knuserver/presentation/pubOrder/dto/PubOrderRequestDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package kr.co.knuserver.presentation.pubOrder.dto; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.NotEmpty; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import java.util.List; | ||
|
|
||
| public record PubOrderRequestDto( | ||
| @NotNull Long pubTableSessionId, | ||
| @NotEmpty List<@Valid OrderMenus> orderMenus | ||
| ) { | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/kr/co/knuserver/presentation/pubOrder/dto/PubOrderResponseDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package kr.co.knuserver.presentation.pubOrder.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
| import java.util.List; | ||
| import kr.co.knuserver.domain.pubOrder.entity.PubOrder; | ||
| import lombok.Builder; | ||
|
|
||
| @Builder | ||
| public record PubOrderResponseDto( | ||
| @NotNull Long pubTableSessionId, | ||
| @NotNull List<OrderedMenus> orderedMenus | ||
| ) { | ||
| public static PubOrderResponseDto fromEntity(Long pubTableSessionId, List<OrderedMenus> orderedMenus) { | ||
| return PubOrderResponseDto.builder() | ||
| .pubTableSessionId(pubTableSessionId) | ||
| .orderedMenus(orderedMenus) | ||
| .build(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.