Skip to content

Commit

Permalink
[feat] 메뉴 편집 기능 (#198)
Browse files Browse the repository at this point in the history
* [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
kgy1008 authored Oct 1, 2024
1 parent aef7a4e commit 7d63cab
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Temporary Items

application-local.yml
application-dev.yml
application-prod.yml

# Qclass
/src/main/generated
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ tasks.register('copyYml', Copy) {
include "*.yml"
into 'src/main/resources'
}
}
}
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())));
}
}
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);
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,4 +19,12 @@ public class MenuFinder {
public List<Menu> findAllByStore(final Store store) {
return menuRepository.findAllByStore(store);
}

protected Menu findByStoreIdAndId(final Long storeId, final Long id) {
return menuRepository.findByStoreIdAndId(storeId,id).orElseThrow(() -> new NotFoundException(MenuErrorCode.MENU_NOT_FOUND));
}

protected boolean existsByStoreAndName(final Store store, final String name) {
return menuRepository.existsByStoreAndName(store, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public class MenuUpdater {
public void saveAll(final List<Menu> menus) {
menuRepository.saveAll(menus);
}

protected void save(final Menu menu) {
menuRepository.save(menu);
}
}
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);
}
}
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);
}
}
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);
}
}
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());
}
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public static Menu create(final Store store, final String name, final int price)
.price(price)
.build();
}

public void update(final String name, final int price) {
this.name = name;
this.price = price;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface MenuRepository extends JpaRepository<Menu, Long> {
List<Menu> findAllByStore(Store store);
Optional<Menu> findByStoreIdAndId(Long storeId, Long id);
boolean existsByStoreAndName(Store store, String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ public void softDelete() {
public String getImageUrlOrElseNull() {
return images.isEmpty() ? null : images.get(0).getImageUrl();
}

public void updateLowestPrice(int lowestPrice) {
this.lowestPrice = lowestPrice;
}
}

0 comments on commit 7d63cab

Please sign in to comment.