-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [docs] add gitignore list * [feat] delete Menu * [feat] modify Menu * [refac] rename command class * [feat] add new menu * [refac] add constraints * [fix] update lowest price * [refac] delete unused class * [refac] bunding frequently used functions * [refac] divide method * [refac] modify verification method * [refac] function separation * [refac] delete unused method
- Loading branch information
Showing
15 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -153,6 +153,7 @@ Temporary Items | |
|
||
application-local.yml | ||
application-dev.yml | ||
application-prod.yml | ||
|
||
# Qclass | ||
/src/main/generated |
This file contains 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 |
---|---|---|
|
@@ -89,4 +89,4 @@ tasks.register('copyYml', Copy) { | |
include "*.yml" | ||
into 'src/main/resources' | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/org/hankki/hankkiserver/api/menu/controller/MenuController.java
This file contains 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,39 @@ | ||
package org.hankki.hankkiserver.api.menu.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.api.dto.HankkiResponse; | ||
import org.hankki.hankkiserver.api.menu.service.MenuCommandService; | ||
import org.hankki.hankkiserver.api.menu.service.command.MenuDeleteCommand; | ||
import org.hankki.hankkiserver.api.menu.service.command.MenuPatchCommand; | ||
import org.hankki.hankkiserver.api.menu.service.command.MenuPostCommand; | ||
import org.hankki.hankkiserver.api.menu.service.response.MenuPostResponse; | ||
import org.hankki.hankkiserver.api.store.controller.request.MenuPostRequest; | ||
import org.hankki.hankkiserver.common.code.CommonSuccessCode; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1") | ||
public class MenuController { | ||
|
||
private final MenuCommandService menuCommandService; | ||
|
||
@DeleteMapping("/{storeId}/menus/{id}") | ||
public HankkiResponse<Void> deleteMenu(@PathVariable("storeId") final Long storeId, @PathVariable("id") final Long id) { | ||
menuCommandService.deleteMenu(MenuDeleteCommand.of(storeId, id)); | ||
return HankkiResponse.success(CommonSuccessCode.NO_CONTENT); | ||
} | ||
|
||
@PatchMapping("/{storeId}/menus/{id}") | ||
public HankkiResponse<Void> updateMenu(@PathVariable("storeId") final Long storeId, @PathVariable("id") final Long id, | ||
@Valid @RequestBody final MenuPostRequest request) { | ||
menuCommandService.modifyMenu(MenuPatchCommand.of(storeId, id, request.name(), request.price())); | ||
return HankkiResponse.success(CommonSuccessCode.OK); | ||
} | ||
|
||
@PostMapping("/{storeId}/menus") | ||
public HankkiResponse<MenuPostResponse> createMenu(@PathVariable final Long storeId, @Valid @RequestBody final MenuPostRequest request) { | ||
return HankkiResponse.success(CommonSuccessCode.CREATED, menuCommandService.createMenu(MenuPostCommand.of(storeId, request.name(), request.price()))); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/org/hankki/hankkiserver/api/menu/service/MenuCommandService.java
This file contains 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,68 @@ | ||
package org.hankki.hankkiserver.api.menu.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.api.menu.service.command.MenuDeleteCommand; | ||
import org.hankki.hankkiserver.api.menu.service.command.MenuPatchCommand; | ||
import org.hankki.hankkiserver.api.menu.service.command.MenuPostCommand; | ||
import org.hankki.hankkiserver.api.menu.service.response.MenuPostResponse; | ||
import org.hankki.hankkiserver.api.store.service.StoreFinder; | ||
import org.hankki.hankkiserver.common.code.MenuErrorCode; | ||
import org.hankki.hankkiserver.common.exception.ConflictException; | ||
import org.hankki.hankkiserver.domain.menu.model.Menu; | ||
import org.hankki.hankkiserver.domain.store.model.Store; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MenuCommandService { | ||
|
||
private final MenuDeleter menuDeleter; | ||
private final MenuFinder menuFinder; | ||
private final MenuUpdater menuUpdater; | ||
private final StoreFinder storeFinder; | ||
|
||
@Transactional | ||
public void deleteMenu(final MenuDeleteCommand command) { | ||
Menu menu = menuFinder.findByStoreIdAndId(command.storeId(), command.id()); | ||
menuDeleter.deleteMenu(menu); | ||
updateLowestPriceInStore(storeFinder.findByIdWhereDeletedIsFalse(command.storeId())); | ||
} | ||
|
||
@Transactional | ||
public void modifyMenu(final MenuPatchCommand command) { | ||
Menu menu = menuFinder.findByStoreIdAndId(command.storeId(), command.id()); | ||
menu.update(command.name(), command.price()); | ||
updateLowestPriceInStore(storeFinder.findByIdWhereDeletedIsFalse(command.storeId())); | ||
} | ||
|
||
@Transactional | ||
public MenuPostResponse createMenu(final MenuPostCommand command) { | ||
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId()); | ||
validateMenuNotConflict(findStore, command.name()); | ||
Menu menu = Menu.create(findStore, command.name(), command.price()); | ||
menuUpdater.save(menu); | ||
updateLowestPriceInStore(findStore, menu); | ||
return MenuPostResponse.of(menu); | ||
} | ||
|
||
private void updateLowestPriceInStore(final Store findStore) { | ||
int lowestPrice = menuFinder.findAllByStore(findStore).stream() | ||
.mapToInt(Menu::getPrice) | ||
.min() | ||
.orElse(0); | ||
findStore.updateLowestPrice(lowestPrice); | ||
} | ||
|
||
private void updateLowestPriceInStore(final Store store, final Menu menu) { | ||
if (store.getLowestPrice() > menu.getPrice()) { | ||
store.updateLowestPrice(menu.getPrice()); | ||
} | ||
} | ||
|
||
private void validateMenuNotConflict(Store store, String menuName) { | ||
if (menuFinder.existsByStoreAndName(store, menuName)) { | ||
throw new ConflictException(MenuErrorCode.ALREADY_EXISTED_MENU); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/org/hankki/hankkiserver/api/menu/service/MenuDeleter.java
This file contains 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,17 @@ | ||
package org.hankki.hankkiserver.api.menu.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.hankki.hankkiserver.domain.menu.model.Menu; | ||
import org.hankki.hankkiserver.domain.menu.repository.MenuRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MenuDeleter { | ||
|
||
private final MenuRepository menuRepository; | ||
|
||
protected void deleteMenu(final Menu menu) { | ||
menuRepository.delete(menu); | ||
} | ||
} |
This file contains 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
This file contains 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
10 changes: 10 additions & 0 deletions
10
src/main/java/org/hankki/hankkiserver/api/menu/service/command/MenuDeleteCommand.java
This file contains 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,10 @@ | ||
package org.hankki.hankkiserver.api.menu.service.command; | ||
|
||
public record MenuDeleteCommand( | ||
long storeId, | ||
long id | ||
) { | ||
public static MenuDeleteCommand of(long storeId, long id) { | ||
return new MenuDeleteCommand(storeId, id); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/hankki/hankkiserver/api/menu/service/command/MenuPatchCommand.java
This file contains 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 org.hankki.hankkiserver.api.menu.service.command; | ||
|
||
public record MenuPatchCommand( | ||
long storeId, | ||
long id, | ||
String name, | ||
int price | ||
) { | ||
public static MenuPatchCommand of(final long storeId, final long id, final String name, final int price) { | ||
return new MenuPatchCommand(storeId, id, name, price); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/hankki/hankkiserver/api/menu/service/command/MenuPostCommand.java
This file contains 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,11 @@ | ||
package org.hankki.hankkiserver.api.menu.service.command; | ||
|
||
public record MenuPostCommand( | ||
long storeId, | ||
String name, | ||
int price | ||
) { | ||
public static MenuPostCommand of(final long storeId, final String name, final int price) { | ||
return new MenuPostCommand(storeId, name, price); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/org/hankki/hankkiserver/api/menu/service/response/MenuPostResponse.java
This file contains 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,14 @@ | ||
package org.hankki.hankkiserver.api.menu.service.response; | ||
|
||
import org.hankki.hankkiserver.domain.menu.model.Menu; | ||
|
||
public record MenuPostResponse ( | ||
long id, | ||
String name, | ||
int price | ||
) { | ||
|
||
public static MenuPostResponse of(final Menu menu) { | ||
return new MenuPostResponse(menu.getId(), menu.getName(), menu.getPrice()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/hankki/hankkiserver/common/code/MenuErrorCode.java
This file contains 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,16 @@ | ||
package org.hankki.hankkiserver.common.code; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum MenuErrorCode implements ErrorCode { | ||
|
||
MENU_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 메뉴입니다."), | ||
ALREADY_EXISTED_MENU(HttpStatus.CONFLICT, "이미 존재하는 메뉴입니다."); | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
This file contains 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
This file contains 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
This file contains 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