From 0653cc3c7413f2b424ea7fb89af16ee399111ffb Mon Sep 17 00:00:00 2001 From: kgy1008 Date: Tue, 1 Oct 2024 15:10:30 +0900 Subject: [PATCH] [feat] delete Menu --- build.gradle | 2 +- .../api/menu/controller/MenuController.java | 24 +++++++++++++++++++ .../api/menu/service/MenuCommandService.java | 18 ++++++++++++++ .../api/menu/service/MenuDeleter.java | 17 +++++++++++++ .../api/menu/service/MenuFinder.java | 6 +++++ .../common/code/MenuErrorCode.java | 15 ++++++++++++ 6 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/hankki/hankkiserver/api/menu/controller/MenuController.java create mode 100644 src/main/java/org/hankki/hankkiserver/api/menu/service/MenuCommandService.java create mode 100644 src/main/java/org/hankki/hankkiserver/api/menu/service/MenuDeleter.java create mode 100644 src/main/java/org/hankki/hankkiserver/common/code/MenuErrorCode.java diff --git a/build.gradle b/build.gradle index 977947ea..4d4bc95b 100644 --- a/build.gradle +++ b/build.gradle @@ -89,4 +89,4 @@ tasks.register('copyYml', Copy) { include "*.yml" into 'src/main/resources' } -} +} \ No newline at end of file diff --git a/src/main/java/org/hankki/hankkiserver/api/menu/controller/MenuController.java b/src/main/java/org/hankki/hankkiserver/api/menu/controller/MenuController.java new file mode 100644 index 00000000..7af4967d --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/menu/controller/MenuController.java @@ -0,0 +1,24 @@ +package org.hankki.hankkiserver.api.menu.controller; + +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.api.dto.HankkiResponse; +import org.hankki.hankkiserver.api.menu.service.MenuCommandService; +import org.hankki.hankkiserver.common.code.CommonSuccessCode; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1") +public class MenuController { + + private final MenuCommandService menuCommandService; + + @DeleteMapping("/menus/{id}") + public HankkiResponse deleteMenu(@PathVariable final Long id) { + menuCommandService.deleteMenu(id); + return HankkiResponse.success(CommonSuccessCode.NO_CONTENT); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuCommandService.java b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuCommandService.java new file mode 100644 index 00000000..47305589 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuCommandService.java @@ -0,0 +1,18 @@ +package org.hankki.hankkiserver.api.menu.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class MenuCommandService { + + private final MenuDeleter menuDeleter; + private final MenuFinder menuFinder; + + @Transactional + public void deleteMenu(final long menuId) { + menuDeleter.deleteMenu(menuFinder.findById(menuId)); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuDeleter.java b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuDeleter.java new file mode 100644 index 00000000..17368789 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuDeleter.java @@ -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); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuFinder.java b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuFinder.java index fdc66473..98e173b0 100644 --- a/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuFinder.java +++ b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuFinder.java @@ -1,6 +1,8 @@ package org.hankki.hankkiserver.api.menu.service; import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.common.code.MenuErrorCode; +import org.hankki.hankkiserver.common.exception.NotFoundException; import org.hankki.hankkiserver.domain.menu.model.Menu; import org.hankki.hankkiserver.domain.menu.repository.MenuRepository; import org.hankki.hankkiserver.domain.store.model.Store; @@ -17,4 +19,8 @@ public class MenuFinder { public List findAllByStore(final Store store) { return menuRepository.findAllByStore(store); } + + protected Menu findById(final Long id) { + return menuRepository.findById(id).orElseThrow(() -> new NotFoundException(MenuErrorCode.MENU_NOT_FOUND)); + } } diff --git a/src/main/java/org/hankki/hankkiserver/common/code/MenuErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/MenuErrorCode.java new file mode 100644 index 00000000..5d60cbca --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/common/code/MenuErrorCode.java @@ -0,0 +1,15 @@ +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, "존재하지 않는 메뉴입니다."); + + private final HttpStatus httpStatus; + private final String message; +}