From 777d18bd41a2e49568a27cbb041ef73da64e57b0 Mon Sep 17 00:00:00 2001 From: Murilo Kakazu Date: Sun, 12 May 2024 20:21:55 -0300 Subject: [PATCH 1/4] feat: add order domain logic and entities --- Makefile | 8 ++ .../fastfood/application/dto/OrderDTO.java | 16 +++ .../application/dto/OrderItemDTO.java | 10 ++ .../application/services/OrderService.java | 15 +++ .../CantAddProductToOrderException.java | 11 ++ .../services/impl/OrderServiceImpl.java | 68 +++++++++++ .../commands/AddProductToOrderCommand.java | 10 ++ .../domain/commands/SubmitOrderCommand.java | 8 ++ .../jpa/entities/CategoryEntity.java | 7 ++ .../persistence/jpa/entities/OrderEntity.java | 110 ++++++++++++++++++ .../jpa/entities/OrderItemEntity.java | 72 ++++++++++++ .../persistence/jpa/entities/OrderStatus.java | 9 ++ .../jpa/entities/ProductEntity.java | 14 +++ .../jpa/repositories/OrderRepository.java | 8 ++ 14 files changed, 366 insertions(+) create mode 100644 Makefile create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderDTO.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderItemDTO.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/application/services/OrderService.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/CantAddProductToOrderException.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/AddProductToOrderCommand.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/SubmitOrderCommand.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderItemEntity.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderStatus.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/repositories/OrderRepository.java diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..50ecc9c --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +test: + ./gradlew test + +build: + ./gradlew build + +run: + ./gradlew bootRun \ No newline at end of file diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderDTO.java new file mode 100644 index 0000000..65d539e --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderDTO.java @@ -0,0 +1,16 @@ +package br.com.fiap.grupo30.fastfood.application.dto; + +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderStatus; +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class OrderDTO { + + private Long id; + private OrderStatus status; + private OrderItemDTO[] items; + private Double totalPrice; + +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderItemDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderItemDTO.java new file mode 100644 index 0000000..6878d71 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderItemDTO.java @@ -0,0 +1,10 @@ +package br.com.fiap.grupo30.fastfood.application.dto; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class OrderItemDTO { + private Long quantity; + private Double totalPrice; + private ProductDTO product; +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/OrderService.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/OrderService.java new file mode 100644 index 0000000..1927369 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/OrderService.java @@ -0,0 +1,15 @@ +package br.com.fiap.grupo30.fastfood.application.services; + +import br.com.fiap.grupo30.fastfood.domain.commands.AddProductToOrderCommand; +import br.com.fiap.grupo30.fastfood.domain.commands.SubmitOrderCommand; +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity; + +public interface OrderService { + + public OrderEntity startNewOrder(); + + public void addProductToOrder(AddProductToOrderCommand addProductCommand); + + public void submitOrder(SubmitOrderCommand command); + +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/CantAddProductToOrderException.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/CantAddProductToOrderException.java new file mode 100644 index 0000000..4d83cc4 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/CantAddProductToOrderException.java @@ -0,0 +1,11 @@ +package br.com.fiap.grupo30.fastfood.application.services.exceptions; + +public class CantAddProductToOrderException extends RuntimeException { + public CantAddProductToOrderException(String msg) { + super(msg); + } + + public CantAddProductToOrderException(String msg, Throwable exception) { + super(msg, exception); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java new file mode 100644 index 0000000..3a93f0e --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java @@ -0,0 +1,68 @@ +package br.com.fiap.grupo30.fastfood.application.services.impl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import br.com.fiap.grupo30.fastfood.application.services.OrderService; +import br.com.fiap.grupo30.fastfood.application.services.exceptions.CantAddProductToOrderException; +import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceNotFoundException; +import br.com.fiap.grupo30.fastfood.domain.commands.AddProductToOrderCommand; +import br.com.fiap.grupo30.fastfood.domain.commands.SubmitOrderCommand; +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity; +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderStatus; +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.ProductEntity; +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.repositories.OrderRepository; +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.repositories.ProductRepository; +import jakarta.transaction.Transactional; + +@Service +public class OrderServiceImpl implements OrderService { + + private final OrderRepository orderRepository; + private final ProductRepository productRepository; + + @Autowired + public OrderServiceImpl( + OrderRepository orderRepository, + ProductRepository productRepository) { + this.orderRepository = orderRepository; + this.productRepository = productRepository; + } + + @Override + @Transactional + public OrderEntity startNewOrder() { + OrderEntity newOrder = OrderEntity.create(); + return this.orderRepository.save(newOrder); + } + + @Override + @Transactional + public void addProductToOrder(AddProductToOrderCommand command) { + OrderEntity order = + this.orderRepository.findById(command.getOrderId()) + .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + + if (!order.isDraft()) { + throw new CantAddProductToOrderException("Can only add products to an order in draft"); + } + + ProductEntity product = this.productRepository.findById(command.getProductId()) + .orElseThrow(() -> new ResourceNotFoundException("Product not found")); + + order.addProduct(product, command.getProductQuantity()); + this.orderRepository.save(order); + } + + @Override + @Transactional + public void submitOrder(SubmitOrderCommand command) { + OrderEntity order = + this.orderRepository.findById(command.getOrderId()) + .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + + order.setStatus(OrderStatus.SUBMITTED); + this.orderRepository.save(order); + } + +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/AddProductToOrderCommand.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/AddProductToOrderCommand.java new file mode 100644 index 0000000..0288bb3 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/AddProductToOrderCommand.java @@ -0,0 +1,10 @@ +package br.com.fiap.grupo30.fastfood.domain.commands; + +import lombok.Data; + +@Data +public class AddProductToOrderCommand { + private Long orderId; + private Long productId; + private Long productQuantity; +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/SubmitOrderCommand.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/SubmitOrderCommand.java new file mode 100644 index 0000000..c1a3765 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/SubmitOrderCommand.java @@ -0,0 +1,8 @@ +package br.com.fiap.grupo30.fastfood.domain.commands; + +import lombok.Data; + +@Data +public class SubmitOrderCommand { + private Long orderId; +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/CategoryEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/CategoryEntity.java index 375a0b8..68a60e9 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/CategoryEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/CategoryEntity.java @@ -2,6 +2,8 @@ import jakarta.persistence.*; import java.time.Instant; + +import br.com.fiap.grupo30.fastfood.application.dto.CategoryDTO; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -53,4 +55,9 @@ public void preUpdate() { public void preRemove() { deletedAt = Instant.now(); } + + public CategoryDTO toDTO() { + CategoryDTO categoryDto = new CategoryDTO(this.id, this.name); + return categoryDto; + } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java new file mode 100644 index 0000000..c8e79db --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java @@ -0,0 +1,110 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities; + +import jakarta.persistence.*; +import java.time.Instant; +import java.util.List; + +import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.application.dto.OrderItemDTO; +import lombok.AccessLevel; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@EqualsAndHashCode +@Entity +@Table(name = "tb_order") +public class OrderEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Enumerated(EnumType.STRING) + private OrderStatus status; + + @OneToMany(mappedBy = "order", cascade = CascadeType.PERSIST, orphanRemoval = true) + private List items; + + @Transient private Double totalPrice; + + @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") + private Instant createdAt; + + @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") + private Instant updatedAt; + + @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") + private Instant deletedAt; + + public void addProduct(ProductEntity product, Long quantity) { + this.items.stream() + .filter(orderItem -> orderItem.getProduct().equals(product)) + .findFirst() + .ifPresentOrElse( + existingItem -> existingItem.setQuantity(existingItem.getQuantity() + quantity), + () -> this.items.add(OrderItemEntity.create(product, quantity))); + + this.recalculateTotalPrice(); + } + + public void removeProduct(ProductEntity product) { + this.items.stream() + .filter(orderItem -> orderItem.getProduct().equals(product)) + .forEach(existingItem -> this.items.remove(existingItem)); + + this.recalculateTotalPrice(); + } + + public Double getTotalPrice() { + return this.totalPrice; + } + + private void recalculateTotalPrice() { + this.totalPrice = this.items.stream().mapToDouble(item -> item.getTotalPrice()).sum(); + } + + public Boolean isDraft() { + return OrderStatus.DRAFT.equals(this.status); + } + + public void setStatus(OrderStatus status) { + this.status = status; + } + + @PostLoad + private void postLoad() { + recalculateTotalPrice(); + } + + @PrePersist + private void prePersist() { + createdAt = Instant.now(); + } + + @PreUpdate + private void preUpdate() { + updatedAt = Instant.now(); + } + + @PreRemove + private void preRemove() { + deletedAt = Instant.now(); + } + + public static OrderEntity create() { + OrderEntity order = new OrderEntity(); + order.status = OrderStatus.DRAFT; + return order; + } + + public OrderDTO toDTO() { + OrderDTO orderDto = new OrderDTO( + this.id, + this.status, + this.items.stream().map(item -> item.toDTO()).toArray(OrderItemDTO[]::new), + this.getTotalPrice() + ); + return orderDto; + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderItemEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderItemEntity.java new file mode 100644 index 0000000..2530776 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderItemEntity.java @@ -0,0 +1,72 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities; + +import br.com.fiap.grupo30.fastfood.application.dto.OrderItemDTO; +import jakarta.persistence.*; +import jakarta.validation.constraints.Min; +import lombok.AccessLevel; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@EqualsAndHashCode +@Entity +@Table(name = "tb_order_item") +public class OrderItemEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "order_id", nullable = false, insertable = false, updatable = false) + private OrderEntity order; + + @ManyToOne + @JoinColumn(name = "product_id", nullable = false) + private ProductEntity product; + + @Column(nullable = false) + @Min(0) + private Long quantity; + + @Column(nullable = false) + @Min(0) + private Double totalPrice; + + public Long getQuantity() { + return this.quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity; + this.recalculateTotalPrice(); + } + + public Double getTotalPrice() { + return this.totalPrice; + } + + public ProductEntity getProduct() { + return this.product; + } + + private void recalculateTotalPrice() { + this.totalPrice = this.product.getPrice() * this.quantity; + } + + public static OrderItemEntity create(ProductEntity product, Long quantity) { + OrderItemEntity orderItem = new OrderItemEntity(); + orderItem.product = product; + orderItem.setQuantity(quantity); + return orderItem; + } + + public OrderItemDTO toDTO() { + OrderItemDTO orderItemDto = new OrderItemDTO( + this.quantity, + this.totalPrice, + this.product.toDTO() + ); + return orderItemDto; + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderStatus.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderStatus.java new file mode 100644 index 0000000..a46ea96 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderStatus.java @@ -0,0 +1,9 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities; + +public enum OrderStatus { + DRAFT, + SUBMITTED, + PREPARING, + DELIVERED, + CANCELED, +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/ProductEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/ProductEntity.java index c569b92..d65e0ec 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/ProductEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/ProductEntity.java @@ -2,6 +2,8 @@ import jakarta.persistence.*; import java.time.Instant; + +import br.com.fiap.grupo30.fastfood.application.dto.ProductDTO; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -82,4 +84,16 @@ public void preUpdate() { public void preRemove() { deletedAt = Instant.now(); } + + public ProductDTO toDTO() { + ProductDTO productDto = new ProductDTO( + this.id, + this.name, + this.description, + this.price, + this.imgUrl, + this.category.toDTO() + ); + return productDto; + } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/repositories/OrderRepository.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/repositories/OrderRepository.java new file mode 100644 index 0000000..0536082 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/repositories/OrderRepository.java @@ -0,0 +1,8 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.repositories; + +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface OrderRepository extends JpaRepository {} From b573318ff84ac024dea42d5ff68fbca43fa99ba3 Mon Sep 17 00:00:00 2001 From: Murilo Kakazu Date: Sun, 12 May 2024 22:25:53 -0300 Subject: [PATCH 2/4] feat: add order resource and domain commands --- Makefile | 5 +- .../adapters/in/rest/OrderResource.java | 90 +++++++++++++++++++ .../exceptions/ResourceExceptionHandler.java | 14 +++ .../dto/AddOrderProductRequest.java | 9 ++ .../fastfood/application/dto/OrderDTO.java | 4 +- .../application/dto/OrderItemDTO.java | 2 + .../application/services/OrderService.java | 12 ++- .../CantAddProductToOrderException.java | 11 --- ...erCantChangeOrderAfterSubmitException.java | 11 +++ .../services/impl/OrderServiceImpl.java | 78 +++++++++++----- .../RemoveProductFromOrderCommand.java | 9 ++ .../jpa/entities/CategoryEntity.java | 3 +- .../persistence/jpa/entities/OrderEntity.java | 45 +++++----- .../jpa/entities/OrderItemEntity.java | 24 +++-- .../jpa/entities/ProductEntity.java | 19 ++-- 15 files changed, 250 insertions(+), 86 deletions(-) create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/application/dto/AddOrderProductRequest.java delete mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/CantAddProductToOrderException.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/UserCantChangeOrderAfterSubmitException.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/RemoveProductFromOrderCommand.java diff --git a/Makefile b/Makefile index 50ecc9c..d36ba89 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,7 @@ build: ./gradlew build run: - ./gradlew bootRun \ No newline at end of file + ./gradlew bootRun + +debug: + ./gradlew bootRun --debug-jvm \ No newline at end of file diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java new file mode 100644 index 0000000..24f3632 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java @@ -0,0 +1,90 @@ +package br.com.fiap.grupo30.fastfood.adapters.in.rest; + +import br.com.fiap.grupo30.fastfood.application.dto.AddOrderProductRequest; +import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.application.services.OrderService; +import br.com.fiap.grupo30.fastfood.domain.commands.AddProductToOrderCommand; +import br.com.fiap.grupo30.fastfood.domain.commands.RemoveProductFromOrderCommand; +import br.com.fiap.grupo30.fastfood.domain.commands.SubmitOrderCommand; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import java.net.URI; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +@RestController +@RequestMapping(value = "/orders") +@Tag(name = "Orders Resource", description = "RESTful API for managing orders.") +public class OrderResource { + + private final OrderService orderService; + + @Autowired + public OrderResource(OrderService orderService) { + this.orderService = orderService; + } + + @GetMapping(value = "/{orderId}") + @Operation( + summary = "Get an order by ID", + description = "Retrieve a specific order based on its ID") + public ResponseEntity findById(@PathVariable Long orderId) { + OrderDTO order = orderService.getOrder(orderId); + return ResponseEntity.ok().body(order); + } + + @PostMapping + @Operation( + summary = "Create a new order", + description = "Create a new order and return the new order's data") + public ResponseEntity startNewOrder() { + OrderDTO order = orderService.startNewOrder(); + URI uri = + ServletUriComponentsBuilder.fromCurrentRequest() + .path("/{orderId}") + .buildAndExpand(order.getOrderId()) + .toUri(); + return ResponseEntity.created(uri).body(order); + } + + @PostMapping(value = "/{orderId}/products") + @Operation(summary = "Add a product to an order", description = "Adds a product to an order") + public ResponseEntity addProduct( + @PathVariable Long orderId, @RequestBody AddOrderProductRequest request) { + var command = new AddProductToOrderCommand(); + command.setOrderId(orderId); + command.setProductId(request.getProductId()); + command.setProductQuantity(request.getQuantity()); + + OrderDTO order = orderService.addProductToOrder(command); + return ResponseEntity.ok().body(order); + } + + @DeleteMapping(value = "/{orderId}/products/{productId}") + @Operation( + summary = "Remove a product from an order", + description = "Removes a product from an order") + public ResponseEntity removeProduct( + @PathVariable Long orderId, @PathVariable Long productId) { + var command = new RemoveProductFromOrderCommand(); + command.setOrderId(orderId); + command.setProductId(productId); + + OrderDTO order = orderService.removeProductFromOrder(command); + return ResponseEntity.ok().body(order); + } + + @PostMapping(value = "/{orderId}/submit") + @Operation( + summary = "Submit an order for preparation", + description = "Submits an order for preparation and return the order's data") + public ResponseEntity submitOrder(@PathVariable Long orderId) { + var command = new SubmitOrderCommand(); + command.setOrderId(orderId); + + OrderDTO order = orderService.submitOrder(command); + return ResponseEntity.ok().body(order); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java index 9095d26..a5ceff8 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java @@ -2,6 +2,7 @@ import br.com.fiap.grupo30.fastfood.application.services.exceptions.DatabaseException; import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceNotFoundException; +import br.com.fiap.grupo30.fastfood.application.services.exceptions.UserCantChangeOrderAfterSubmitException; import jakarta.servlet.http.HttpServletRequest; import java.time.Instant; import org.springframework.http.HttpStatus; @@ -36,4 +37,17 @@ public ResponseEntity database(DatabaseException e, HttpServletRe err.setPath(request.getRequestURI()); return ResponseEntity.status(status).body(err); } + + @ExceptionHandler(UserCantChangeOrderAfterSubmitException.class) + public ResponseEntity userCantChangeOrderAfterSubmit( + UserCantChangeOrderAfterSubmitException e, HttpServletRequest request) { + HttpStatus status = HttpStatus.BAD_REQUEST; + StandardError err = new StandardError(); + err.setTimestamp(Instant.now()); + err.setStatus(status.value()); + err.setError("UserCantChangeOrderAfterSubmit exception"); + err.setMessage(e.getMessage()); + err.setPath(request.getRequestURI()); + return ResponseEntity.status(status).body(err); + } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/AddOrderProductRequest.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/AddOrderProductRequest.java new file mode 100644 index 0000000..01f4fa9 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/AddOrderProductRequest.java @@ -0,0 +1,9 @@ +package br.com.fiap.grupo30.fastfood.application.dto; + +import lombok.Data; + +@Data +public class AddOrderProductRequest { + private final Long productId; + private final Long quantity; +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderDTO.java index 65d539e..092c6e6 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderDTO.java @@ -7,10 +7,8 @@ @Data @AllArgsConstructor public class OrderDTO { - - private Long id; + private Long orderId; private OrderStatus status; private OrderItemDTO[] items; private Double totalPrice; - } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderItemDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderItemDTO.java index 6878d71..da51a0a 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderItemDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/OrderItemDTO.java @@ -1,7 +1,9 @@ package br.com.fiap.grupo30.fastfood.application.dto; import lombok.AllArgsConstructor; +import lombok.Data; +@Data @AllArgsConstructor public class OrderItemDTO { private Long quantity; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/OrderService.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/OrderService.java index 1927369..d037495 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/OrderService.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/OrderService.java @@ -1,15 +1,19 @@ package br.com.fiap.grupo30.fastfood.application.services; +import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.domain.commands.AddProductToOrderCommand; +import br.com.fiap.grupo30.fastfood.domain.commands.RemoveProductFromOrderCommand; import br.com.fiap.grupo30.fastfood.domain.commands.SubmitOrderCommand; -import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity; public interface OrderService { - public OrderEntity startNewOrder(); + public OrderDTO startNewOrder(); - public void addProductToOrder(AddProductToOrderCommand addProductCommand); + public OrderDTO addProductToOrder(AddProductToOrderCommand command); - public void submitOrder(SubmitOrderCommand command); + public OrderDTO removeProductFromOrder(RemoveProductFromOrderCommand command); + public OrderDTO submitOrder(SubmitOrderCommand command); + + public OrderDTO getOrder(Long orderId); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/CantAddProductToOrderException.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/CantAddProductToOrderException.java deleted file mode 100644 index 4d83cc4..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/CantAddProductToOrderException.java +++ /dev/null @@ -1,11 +0,0 @@ -package br.com.fiap.grupo30.fastfood.application.services.exceptions; - -public class CantAddProductToOrderException extends RuntimeException { - public CantAddProductToOrderException(String msg) { - super(msg); - } - - public CantAddProductToOrderException(String msg, Throwable exception) { - super(msg, exception); - } -} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/UserCantChangeOrderAfterSubmitException.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/UserCantChangeOrderAfterSubmitException.java new file mode 100644 index 0000000..7efd67f --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/UserCantChangeOrderAfterSubmitException.java @@ -0,0 +1,11 @@ +package br.com.fiap.grupo30.fastfood.application.services.exceptions; + +public class UserCantChangeOrderAfterSubmitException extends RuntimeException { + public UserCantChangeOrderAfterSubmitException(String msg) { + super(msg); + } + + public UserCantChangeOrderAfterSubmitException(String msg, Throwable exception) { + super(msg, exception); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java index 3a93f0e..bbf0044 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java @@ -1,12 +1,11 @@ package br.com.fiap.grupo30.fastfood.application.services.impl; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - +import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.application.services.OrderService; -import br.com.fiap.grupo30.fastfood.application.services.exceptions.CantAddProductToOrderException; import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceNotFoundException; +import br.com.fiap.grupo30.fastfood.application.services.exceptions.UserCantChangeOrderAfterSubmitException; import br.com.fiap.grupo30.fastfood.domain.commands.AddProductToOrderCommand; +import br.com.fiap.grupo30.fastfood.domain.commands.RemoveProductFromOrderCommand; import br.com.fiap.grupo30.fastfood.domain.commands.SubmitOrderCommand; import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity; import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderStatus; @@ -14,6 +13,8 @@ import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.repositories.OrderRepository; import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.repositories.ProductRepository; import jakarta.transaction.Transactional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; @Service public class OrderServiceImpl implements OrderService { @@ -22,47 +23,82 @@ public class OrderServiceImpl implements OrderService { private final ProductRepository productRepository; @Autowired - public OrderServiceImpl( - OrderRepository orderRepository, - ProductRepository productRepository) { + public OrderServiceImpl(OrderRepository orderRepository, ProductRepository productRepository) { this.orderRepository = orderRepository; this.productRepository = productRepository; } @Override @Transactional - public OrderEntity startNewOrder() { + public OrderDTO startNewOrder() { OrderEntity newOrder = OrderEntity.create(); - return this.orderRepository.save(newOrder); + newOrder = this.orderRepository.save(newOrder); + return newOrder.toDTO(); } @Override @Transactional - public void addProductToOrder(AddProductToOrderCommand command) { + public OrderDTO addProductToOrder(AddProductToOrderCommand command) { OrderEntity order = - this.orderRepository.findById(command.getOrderId()) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + this.orderRepository + .findById(command.getOrderId()) + .orElseThrow(() -> new ResourceNotFoundException("Order not found")); if (!order.isDraft()) { - throw new CantAddProductToOrderException("Can only add products to an order in draft"); + throw new UserCantChangeOrderAfterSubmitException( + "Can only add products to an order in draft"); } - ProductEntity product = this.productRepository.findById(command.getProductId()) - .orElseThrow(() -> new ResourceNotFoundException("Product not found")); + ProductEntity product = + this.productRepository + .findById(command.getProductId()) + .orElseThrow(() -> new ResourceNotFoundException("Product not found")); order.addProduct(product, command.getProductQuantity()); - this.orderRepository.save(order); + return this.orderRepository.save(order).toDTO(); + } + + @Override + @Transactional + public OrderDTO removeProductFromOrder(RemoveProductFromOrderCommand command) { + OrderEntity order = + this.orderRepository + .findById(command.getOrderId()) + .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + + if (!order.isDraft()) { + throw new UserCantChangeOrderAfterSubmitException( + "Can only remove products from an order in draft"); + } + + ProductEntity product = + this.productRepository + .findById(command.getProductId()) + .orElseThrow(() -> new ResourceNotFoundException("Product not found")); + + order.removeProduct(product); + return this.orderRepository.save(order).toDTO(); } @Override @Transactional - public void submitOrder(SubmitOrderCommand command) { + public OrderDTO submitOrder(SubmitOrderCommand command) { OrderEntity order = - this.orderRepository.findById(command.getOrderId()) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + this.orderRepository + .findById(command.getOrderId()) + .orElseThrow(() -> new ResourceNotFoundException("Order not found")); order.setStatus(OrderStatus.SUBMITTED); - this.orderRepository.save(order); + return this.orderRepository.save(order).toDTO(); + } + + @Override + public OrderDTO getOrder(Long orderId) { + OrderEntity order = + this.orderRepository + .findById(orderId) + .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + + return order.toDTO(); } - } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/RemoveProductFromOrderCommand.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/RemoveProductFromOrderCommand.java new file mode 100644 index 0000000..a3ff108 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/commands/RemoveProductFromOrderCommand.java @@ -0,0 +1,9 @@ +package br.com.fiap.grupo30.fastfood.domain.commands; + +import lombok.Data; + +@Data +public class RemoveProductFromOrderCommand { + private Long orderId; + private Long productId; +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/CategoryEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/CategoryEntity.java index 68a60e9..34e33ac 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/CategoryEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/CategoryEntity.java @@ -1,9 +1,8 @@ package br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities; +import br.com.fiap.grupo30.fastfood.application.dto.CategoryDTO; import jakarta.persistence.*; import java.time.Instant; - -import br.com.fiap.grupo30.fastfood.application.dto.CategoryDTO; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java index c8e79db..e3042bf 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java @@ -1,11 +1,11 @@ package br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities; -import jakarta.persistence.*; -import java.time.Instant; -import java.util.List; - import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.application.dto.OrderItemDTO; +import jakarta.persistence.*; +import java.time.Instant; +import java.util.Collection; +import java.util.LinkedList; import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @@ -23,10 +23,14 @@ public class OrderEntity { @Enumerated(EnumType.STRING) private OrderStatus status; - @OneToMany(mappedBy = "order", cascade = CascadeType.PERSIST, orphanRemoval = true) - private List items; + @OneToMany( + mappedBy = "order", + fetch = FetchType.EAGER, + cascade = CascadeType.ALL, + orphanRemoval = true) + private Collection items = new LinkedList(); - @Transient private Double totalPrice; + @Transient private Double totalPrice = 0.0; @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") private Instant createdAt; @@ -39,19 +43,18 @@ public class OrderEntity { public void addProduct(ProductEntity product, Long quantity) { this.items.stream() - .filter(orderItem -> orderItem.getProduct().equals(product)) - .findFirst() - .ifPresentOrElse( - existingItem -> existingItem.setQuantity(existingItem.getQuantity() + quantity), - () -> this.items.add(OrderItemEntity.create(product, quantity))); + .filter(orderItem -> orderItem.getProduct().equals(product)) + .findFirst() + .ifPresentOrElse( + existingItem -> + existingItem.setQuantity(existingItem.getQuantity() + quantity), + () -> this.items.add(OrderItemEntity.create(this, product, quantity))); this.recalculateTotalPrice(); } public void removeProduct(ProductEntity product) { - this.items.stream() - .filter(orderItem -> orderItem.getProduct().equals(product)) - .forEach(existingItem -> this.items.remove(existingItem)); + this.items.removeIf(orderItem -> orderItem.getProduct().equals(product)); this.recalculateTotalPrice(); } @@ -99,12 +102,12 @@ public static OrderEntity create() { } public OrderDTO toDTO() { - OrderDTO orderDto = new OrderDTO( - this.id, - this.status, - this.items.stream().map(item -> item.toDTO()).toArray(OrderItemDTO[]::new), - this.getTotalPrice() - ); + OrderDTO orderDto = + new OrderDTO( + this.id, + this.status, + this.items.stream().map(item -> item.toDTO()).toArray(OrderItemDTO[]::new), + this.getTotalPrice()); return orderDto; } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderItemEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderItemEntity.java index 2530776..1f13081 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderItemEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderItemEntity.java @@ -17,21 +17,21 @@ public class OrderItemEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "order_id", nullable = false, insertable = false, updatable = false) + @ManyToOne() + @JoinColumn(name = "order_id", nullable = false, updatable = false) private OrderEntity order; - @ManyToOne - @JoinColumn(name = "product_id", nullable = false) + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "product_id", nullable = false, updatable = false) private ProductEntity product; @Column(nullable = false) - @Min(0) - private Long quantity; + @Min(1L) + private Long quantity = 1L; @Column(nullable = false) @Min(0) - private Double totalPrice; + private Double totalPrice = 0.0; public Long getQuantity() { return this.quantity; @@ -54,19 +54,17 @@ private void recalculateTotalPrice() { this.totalPrice = this.product.getPrice() * this.quantity; } - public static OrderItemEntity create(ProductEntity product, Long quantity) { + public static OrderItemEntity create(OrderEntity order, ProductEntity product, Long quantity) { OrderItemEntity orderItem = new OrderItemEntity(); + orderItem.order = order; orderItem.product = product; orderItem.setQuantity(quantity); return orderItem; } public OrderItemDTO toDTO() { - OrderItemDTO orderItemDto = new OrderItemDTO( - this.quantity, - this.totalPrice, - this.product.toDTO() - ); + OrderItemDTO orderItemDto = + new OrderItemDTO(this.quantity, this.totalPrice, this.product.toDTO()); return orderItemDto; } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/ProductEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/ProductEntity.java index d65e0ec..d32d666 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/ProductEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/ProductEntity.java @@ -1,9 +1,8 @@ package br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities; +import br.com.fiap.grupo30.fastfood.application.dto.ProductDTO; import jakarta.persistence.*; import java.time.Instant; - -import br.com.fiap.grupo30.fastfood.application.dto.ProductDTO; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -86,14 +85,14 @@ public void preRemove() { } public ProductDTO toDTO() { - ProductDTO productDto = new ProductDTO( - this.id, - this.name, - this.description, - this.price, - this.imgUrl, - this.category.toDTO() - ); + ProductDTO productDto = + new ProductDTO( + this.id, + this.name, + this.description, + this.price, + this.imgUrl, + this.category.toDTO()); return productDto; } } From 08d2acc726cee10dfd6807d750c6484bd0d53006 Mon Sep 17 00:00:00 2001 From: Murilo Kakazu Date: Sun, 12 May 2024 22:28:25 -0300 Subject: [PATCH 3/4] fix: pmd --- .../UserCantChangeOrderAfterSubmitException.java | 5 +++++ .../application/services/impl/OrderServiceImpl.java | 10 ++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/UserCantChangeOrderAfterSubmitException.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/UserCantChangeOrderAfterSubmitException.java index 7efd67f..9d874f4 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/UserCantChangeOrderAfterSubmitException.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/exceptions/UserCantChangeOrderAfterSubmitException.java @@ -1,6 +1,11 @@ package br.com.fiap.grupo30.fastfood.application.services.exceptions; +import java.io.Serial; + public class UserCantChangeOrderAfterSubmitException extends RuntimeException { + + @Serial private static final long serialVersionUID = 1L; + public UserCantChangeOrderAfterSubmitException(String msg) { super(msg); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java index bbf0044..ced289b 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/services/impl/OrderServiceImpl.java @@ -19,6 +19,8 @@ @Service public class OrderServiceImpl implements OrderService { + private final String ORDER_NOT_FOUND_MESSAGE = "Order not found"; + private final OrderRepository orderRepository; private final ProductRepository productRepository; @@ -42,7 +44,7 @@ public OrderDTO addProductToOrder(AddProductToOrderCommand command) { OrderEntity order = this.orderRepository .findById(command.getOrderId()) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + .orElseThrow(() -> new ResourceNotFoundException(ORDER_NOT_FOUND_MESSAGE)); if (!order.isDraft()) { throw new UserCantChangeOrderAfterSubmitException( @@ -64,7 +66,7 @@ public OrderDTO removeProductFromOrder(RemoveProductFromOrderCommand command) { OrderEntity order = this.orderRepository .findById(command.getOrderId()) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + .orElseThrow(() -> new ResourceNotFoundException(ORDER_NOT_FOUND_MESSAGE)); if (!order.isDraft()) { throw new UserCantChangeOrderAfterSubmitException( @@ -86,7 +88,7 @@ public OrderDTO submitOrder(SubmitOrderCommand command) { OrderEntity order = this.orderRepository .findById(command.getOrderId()) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + .orElseThrow(() -> new ResourceNotFoundException(ORDER_NOT_FOUND_MESSAGE)); order.setStatus(OrderStatus.SUBMITTED); return this.orderRepository.save(order).toDTO(); @@ -97,7 +99,7 @@ public OrderDTO getOrder(Long orderId) { OrderEntity order = this.orderRepository .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + .orElseThrow(() -> new ResourceNotFoundException(ORDER_NOT_FOUND_MESSAGE)); return order.toDTO(); } From 8e3485ef4649bacc47eb4a2a5da193eb7bfe8092 Mon Sep 17 00:00:00 2001 From: Murilo Kakazu Date: Sun, 12 May 2024 22:30:58 -0300 Subject: [PATCH 4/4] fix: pmd --- .../out/persistence/jpa/entities/OrderEntity.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java index e3042bf..a66b4d7 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/out/persistence/jpa/entities/OrderEntity.java @@ -76,22 +76,22 @@ public void setStatus(OrderStatus status) { } @PostLoad - private void postLoad() { + protected void postLoad() { recalculateTotalPrice(); } @PrePersist - private void prePersist() { + protected void prePersist() { createdAt = Instant.now(); } @PreUpdate - private void preUpdate() { + protected void preUpdate() { updatedAt = Instant.now(); } @PreRemove - private void preRemove() { + protected void preRemove() { deletedAt = Instant.now(); }