Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from 7SOATSquad30/refactor/add-use-cases-for-o…
Browse files Browse the repository at this point in the history
…rder

refactor: implement order actions as use cases
  • Loading branch information
MuriloKakazu authored May 20, 2024
2 parents 4793aa4 + 150e910 commit 9b24d05
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 210 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.PHONY: test build run debug lint/fix

test:
./gradlew test

Expand All @@ -8,4 +10,7 @@ run:
./gradlew bootRun

debug:
./gradlew bootRun --debug-jvm
./gradlew bootRun --debug-jvm

lint/fix:
./gradlew spotlessApply
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

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 br.com.fiap.grupo30.fastfood.domain.usecases.order.AddProductToOrderUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.order.GetOrderUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.order.RemoveProductFromOrderUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.order.StartNewOrderUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.order.SubmitOrderUseCase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URI;
Expand All @@ -19,19 +20,32 @@
@Tag(name = "Orders Resource", description = "RESTful API for managing orders.")
public class OrderResource {

private final OrderService orderService;
private final StartNewOrderUseCase startNewOrderUseCase;
private final AddProductToOrderUseCase addProductToOrderUseCase;
private final RemoveProductFromOrderUseCase removeProductFromOrderUseCase;
private final GetOrderUseCase getOrderUseCase;
private final SubmitOrderUseCase submitOrderUseCase;

@Autowired
public OrderResource(OrderService orderService) {
this.orderService = orderService;
public OrderResource(
StartNewOrderUseCase startNewOrderUseCase,
AddProductToOrderUseCase addProductToOrderUseCase,
RemoveProductFromOrderUseCase removeProductFromOrderUseCase,
GetOrderUseCase getOrderUseCase,
SubmitOrderUseCase submitOrderUseCase) {
this.startNewOrderUseCase = startNewOrderUseCase;
this.addProductToOrderUseCase = addProductToOrderUseCase;
this.removeProductFromOrderUseCase = removeProductFromOrderUseCase;
this.getOrderUseCase = getOrderUseCase;
this.submitOrderUseCase = submitOrderUseCase;
}

@GetMapping(value = "/{orderId}")
@Operation(
summary = "Get an order by ID",
description = "Retrieve a specific order based on its ID")
public ResponseEntity<OrderDTO> findById(@PathVariable Long orderId) {
OrderDTO order = orderService.getOrder(orderId);
OrderDTO order = this.getOrderUseCase.execute(orderId);
return ResponseEntity.ok().body(order);
}

Expand All @@ -40,7 +54,7 @@ public ResponseEntity<OrderDTO> findById(@PathVariable Long orderId) {
summary = "Create a new order",
description = "Create a new order and return the new order's data")
public ResponseEntity<OrderDTO> startNewOrder() {
OrderDTO order = orderService.startNewOrder();
OrderDTO order = this.startNewOrderUseCase.execute();
URI uri =
ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{orderId}")
Expand All @@ -53,12 +67,9 @@ public ResponseEntity<OrderDTO> startNewOrder() {
@Operation(summary = "Add a product to an order", description = "Adds a product to an order")
public ResponseEntity<OrderDTO> 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);
OrderDTO order =
this.addProductToOrderUseCase.execute(
orderId, request.getProductId(), request.getQuantity());
return ResponseEntity.ok().body(order);
}

Expand All @@ -68,11 +79,7 @@ public ResponseEntity<OrderDTO> addProduct(
description = "Removes a product from an order")
public ResponseEntity<OrderDTO> removeProduct(
@PathVariable Long orderId, @PathVariable Long productId) {
var command = new RemoveProductFromOrderCommand();
command.setOrderId(orderId);
command.setProductId(productId);

OrderDTO order = orderService.removeProductFromOrder(command);
OrderDTO order = this.removeProductFromOrderUseCase.execute(orderId, productId);
return ResponseEntity.ok().body(order);
}

Expand All @@ -81,10 +88,7 @@ public ResponseEntity<OrderDTO> removeProduct(
summary = "Submit an order for preparation",
description = "Submits an order for preparation and return the order's data")
public ResponseEntity<OrderDTO> submitOrder(@PathVariable Long orderId) {
var command = new SubmitOrderCommand();
command.setOrderId(orderId);

OrderDTO order = orderService.submitOrder(command);
OrderDTO order = this.submitOrderUseCase.execute(orderId);
return ResponseEntity.ok().body(order);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceBadRequestException;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceConflictException;
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;
Expand Down Expand Up @@ -65,17 +64,4 @@ public ResponseEntity<StandardError> database(DatabaseException e, HttpServletRe
err.setPath(request.getRequestURI());
return ResponseEntity.status(status).body(err);
}

@ExceptionHandler(UserCantChangeOrderAfterSubmitException.class)
public ResponseEntity<StandardError> 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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package br.com.fiap.grupo30.fastfood.application.services.exceptions;

import java.io.Serial;

public class CantChangeOrderProductsAfterSubmitException extends DomainValidationException {

@Serial private static final long serialVersionUID = 1L;
private static final String MESSAGE = "Can not change products of a submitted order";

public CantChangeOrderProductsAfterSubmitException() {
super(MESSAGE);
}

public CantChangeOrderProductsAfterSubmitException(Throwable exception) {
super(MESSAGE, exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.com.fiap.grupo30.fastfood.application.services.exceptions;

import java.io.Serial;

public class CantSubmitOrderWithoutProductsException extends DomainValidationException {
@Serial private static final long serialVersionUID = 1L;
private static final String MESSAGE = "Can not submit order without products";

public CantSubmitOrderWithoutProductsException() {
super(MESSAGE);
}

public CantSubmitOrderWithoutProductsException(Throwable exception) {
super(MESSAGE, exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package br.com.fiap.grupo30.fastfood.application.services.exceptions;

import java.io.Serial;

public abstract class DomainValidationException extends ResourceBadRequestException {
@Serial private static final long serialVersionUID = 1L;

public DomainValidationException(String msg) {
super(msg);
}

public DomainValidationException(String msg, Throwable exception) {
super(msg, exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.com.fiap.grupo30.fastfood.application.services.exceptions;

import java.io.Serial;

public class InvalidCpfException extends DomainValidationException {
@Serial private static final long serialVersionUID = 1L;
private static final String MESSAGE_TEMPLATE = "Invalid CPF: %s";

public InvalidCpfException(String cpf) {
super(String.format(MESSAGE_TEMPLATE, cpf));
}

public InvalidCpfException(String cpf, Throwable exception) {
super(String.format(MESSAGE_TEMPLATE, cpf), exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import java.io.Serial;

public class ResourceBadRequestException extends RuntimeException {
public abstract class ResourceBadRequestException extends RuntimeException {
@Serial private static final long serialVersionUID = 1L;

public ResourceBadRequestException(String msg) {
super(msg);
}

public ResourceBadRequestException(String msg, Throwable exception) {
super(msg, exception);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import br.com.fiap.grupo30.fastfood.application.mapper.impl.CustomerDTOMapper;
import br.com.fiap.grupo30.fastfood.application.mapper.impl.CustomerMapper;
import br.com.fiap.grupo30.fastfood.application.services.CustomerService;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceBadRequestException;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.InvalidCpfException;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceConflictException;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceNotFoundException;
import br.com.fiap.grupo30.fastfood.domain.vo.CPF;
Expand Down Expand Up @@ -45,7 +45,7 @@ public CustomerDTO findCustomerByCpf(String cpf) {
public CustomerDTO insert(CustomerDTO dto) {
String cpfDTO = dto.getCpf();
if (!CPF.isValid(cpfDTO)) {
throw new ResourceBadRequestException("Invalid CPF: " + cpfDTO);
throw new InvalidCpfException(cpfDTO);
}
try {
dto.setCpf(CPF.removeNonDigits(cpfDTO));
Expand Down

This file was deleted.

Loading

0 comments on commit 9b24d05

Please sign in to comment.