Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kitchenpos.global.exception;

import kitchenpos.global.exception.dto.ErrorCode;
import kitchenpos.global.exception.dto.ExceptionResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.NoSuchElementException;

@RestControllerAdvice
public class GlobalRestControllerAdvice {

@ResponseBody
@ExceptionHandler(NoSuchElementException.class)
public ExceptionResponse noSuchElementExceptionHandler(NoSuchElementException exception) {
return new ExceptionResponse(
ErrorCode.NO_SUCH_ELEMENT.getCode(),
ErrorCode.NO_SUCH_ELEMENT.getMessage()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kitchenpos.global.exception.custom;

import java.util.NoSuchElementException;

public class NotFoundProductException extends NoSuchElementException {
public NotFoundProductException() {
}
}
22 changes: 22 additions & 0 deletions src/main/java/kitchenpos/global/exception/dto/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kitchenpos.global.exception.dto;

public enum ErrorCode {

NO_SUCH_ELEMENT("E001", "๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.");

private final String code;
private final String message;

ErrorCode(String code, String message) {
this.code = code;
this.message = message;
}

public String getCode() {
return code;
}

public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kitchenpos.global.exception.dto;

public class ExceptionResponse {

private final String code;
private final String message;

public ExceptionResponse(String code, String message) {
this.code = code;
this.message = message;
}

public String getCode() {
return code;
}

public String getMessage() {
return message;
}
}
20 changes: 20 additions & 0 deletions src/main/java/kitchenpos/menus/application/MenuEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kitchenpos.menus.application;

import kitchenpos.menus.application.dto.MenuEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MenuEventListener {

private final MenuService menuService;

public MenuEventListener(MenuService menuService) {
this.menuService = menuService;
}

@EventListener
public void changeMenuDisplayStatus(final MenuEvent menuEvent) {
menuService.changeMenuDisplayStatus(menuEvent.getProductId());
}
}
25 changes: 19 additions & 6 deletions src/main/java/kitchenpos/menus/application/MenuService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package kitchenpos.menus.application;

import kitchenpos.menus.domain.*;
import kitchenpos.products.domain.Product;
import kitchenpos.products.domain.ProductRepository;
import kitchenpos.products.tobe.domain.Product;
import kitchenpos.products.tobe.domain.ProductRepository;
import kitchenpos.products.infra.PurgomalumClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -12,6 +12,7 @@
import java.util.stream.Collectors;

@Service
@Transactional
public class MenuService {
private final MenuRepository menuRepository;
private final MenuGroupRepository menuGroupRepository;
Expand All @@ -30,7 +31,6 @@ public MenuService(
this.purgomalumClient = purgomalumClient;
}

@Transactional
public Menu create(final Menu request) {
final BigDecimal price = request.getPrice();
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) {
Expand Down Expand Up @@ -61,6 +61,7 @@ public Menu create(final Menu request) {
.orElseThrow(NoSuchElementException::new);
sum = sum.add(
product.getPrice()
.getProductPrice()
.multiply(BigDecimal.valueOf(quantity))
);
final MenuProduct menuProduct = new MenuProduct();
Expand All @@ -85,7 +86,6 @@ public Menu create(final Menu request) {
return menuRepository.save(menu);
}

@Transactional
public Menu changePrice(final UUID menuId, final Menu request) {
final BigDecimal price = request.getPrice();
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) {
Expand All @@ -98,6 +98,7 @@ public Menu changePrice(final UUID menuId, final Menu request) {
sum = sum.add(
menuProduct.getProduct()
.getPrice()
.getProductPrice()
.multiply(BigDecimal.valueOf(menuProduct.getQuantity()))
);
}
Expand All @@ -108,7 +109,6 @@ public Menu changePrice(final UUID menuId, final Menu request) {
return menu;
}

@Transactional
public Menu display(final UUID menuId) {
final Menu menu = menuRepository.findById(menuId)
.orElseThrow(NoSuchElementException::new);
Expand All @@ -117,6 +117,7 @@ public Menu display(final UUID menuId) {
sum = sum.add(
menuProduct.getProduct()
.getPrice()
.getProductPrice()
.multiply(BigDecimal.valueOf(menuProduct.getQuantity()))
);
}
Expand All @@ -127,14 +128,26 @@ public Menu display(final UUID menuId) {
return menu;
}

@Transactional
public Menu hide(final UUID menuId) {
final Menu menu = menuRepository.findById(menuId)
.orElseThrow(NoSuchElementException::new);
menu.setDisplayed(false);
return menu;
}

public void changeMenuDisplayStatus(final UUID productId) {
menuRepository.findAllByProductId(productId)
.forEach(menu -> {
BigDecimal sum = menu.getMenuProducts().stream()
.map(menuProduct -> menuProduct.getProduct().getPrice().getProductPrice().multiply(BigDecimal.valueOf(menuProduct.getQuantity())))
.reduce(BigDecimal.ZERO, BigDecimal::add);

if (menu.getPrice().compareTo(sum) > 0) {
menu.setDisplayed(false);
}
});
}

@Transactional(readOnly = true)
public List<Menu> findAll() {
return menuRepository.findAll();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/kitchenpos/menus/application/dto/MenuEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kitchenpos.menus.application.dto;

import java.util.UUID;

public abstract class MenuEvent {

public abstract UUID getProductId();

}
2 changes: 1 addition & 1 deletion src/main/java/kitchenpos/menus/domain/MenuProduct.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kitchenpos.menus.domain;

import kitchenpos.products.domain.Product;
import kitchenpos.products.tobe.domain.Product;

import javax.persistence.*;
import java.util.UUID;
Expand Down
81 changes: 0 additions & 81 deletions src/main/java/kitchenpos/products/application/ProductService.java

This file was deleted.

49 changes: 0 additions & 49 deletions src/main/java/kitchenpos/products/domain/Product.java

This file was deleted.

Loading