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 #37 from 7SOATSquad30/feat/add-list-orders-specifi…
Browse files Browse the repository at this point in the history
…c-statuses

feat: add list orders specific statuses
  • Loading branch information
jonasmzsouza authored Jul 27, 2024
2 parents 60e9132 + 84d76f8 commit 6de07a2
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 12 deletions.
10 changes: 10 additions & 0 deletions init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ INSERT INTO tb_order_item (order_id, product_id, quantity, total_price) VALUES (
INSERT INTO tb_order_item (order_id, product_id, quantity, total_price) VALUES (9, 22, 2, 17.8);
INSERT INTO tb_order_item (order_id, product_id, quantity, total_price) VALUES (10, 12, 1, 8.9);
INSERT INTO tb_order_item (order_id, product_id, quantity, total_price) VALUES (10, 15, 1, 13.9);
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (1, 0.0, 'NOT_SUBMITTED', NOW());
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (2, 14.9, 'COLLECTED', NOW());
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (3, 0.0, 'NOT_SUBMITTED', NOW());
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (4, 29.8, 'COLLECTED', NOW());
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (5, 21.7, 'COLLECTED', NOW());
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (6, 17.7, 'COLLECTED', NOW());
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (7, 49.6, 'COLLECTED', NOW());
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (8, 7.9, 'COLLECTED', NOW());
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (9, 17.8, 'COLLECTED', NOW());
INSERT INTO tb_payment (order_id, amount, status, created_at) VALUES (10, 22.8, 'COLLECTED', NOW());
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.util.List;

public interface OrderRepository {

List<Order> findOrdersWithSpecificStatuses();

List<Order> findOrdersByStatus(OrderStatus status);

Order findById(Long orderId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import java.util.List;
import java.util.Locale;

public class ListOrdersUseCase {
public class ListOrdersByStatusUseCase {

private final OrderGateway orderGateway;

public ListOrdersUseCase(OrderGateway orderGateway) {
public ListOrdersByStatusUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.com.fiap.grupo30.fastfood.domain.usecases.order;

import br.com.fiap.grupo30.fastfood.domain.entities.Order;
import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway;
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO;
import java.util.List;

public class ListOrdersWithSpecificStatusesUseCase {

private final OrderGateway orderGateway;

public ListOrdersWithSpecificStatusesUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
}

public List<OrderDTO> execute() {
return orderGateway.findOrdersWithSpecificStatuses().stream().map(Order::toDTO).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ public OrderRepository orderRepository(JpaOrderRepository jpaOrderRepository) {
}

@Bean
public ListOrdersUseCase listOrdersUseCase(OrderGateway orderGateway) {
return new ListOrdersUseCase(orderGateway);
public ListOrdersWithSpecificStatusesUseCase listOrdersBySpecificStatusesUseCase(
OrderGateway orderGateway) {
return new ListOrdersWithSpecificStatusesUseCase(orderGateway);
}

@Bean
public ListOrdersByStatusUseCase listOrdersByStatusUseCase(OrderGateway orderGateway) {
return new ListOrdersByStatusUseCase(orderGateway);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public OrderGateway(JpaOrderRepository jpaOrderRepository) {
this.jpaOrderRepository = jpaOrderRepository;
}

@Override
@Transactional(readOnly = true)
public List<Order> findOrdersWithSpecificStatuses() {
return jpaOrderRepository.findOrdersWithSpecificStatuses().stream()
.map(OrderEntity::toDomainEntity)
.toList();
}

@Override
public List<Order> findOrdersByStatus(OrderStatus orderStatus) {
return jpaOrderRepository.findOrdersByStatus(orderStatus).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

@Repository
public interface JpaOrderRepository extends JpaRepository<OrderEntity, Long> {

@Query(
"SELECT obj FROM OrderEntity obj "
+ "WHERE obj.status IN ('READY', 'PREPARING', 'SUBMITTED') "
+ "ORDER BY CASE obj.status "
+ "WHEN 'READY' THEN 1 "
+ "WHEN 'PREPARING' THEN 2 "
+ "WHEN 'SUBMITTED' THEN 3 "
+ "END ASC, obj.createdAt ASC")
List<OrderEntity> findOrdersWithSpecificStatuses();

@Query(
"SELECT obj FROM OrderEntity obj "
+ "WHERE (:status IS NULL OR obj.status = :status) "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class OrderController {
private final RemoveProductFromOrderUseCase removeProductFromOrderUseCase;
private final GetOrderUseCase getOrderUseCase;
private final SubmitOrderUseCase submitOrderUseCase;
private final ListOrdersUseCase listAllOrdersUseCase;
private final ListOrdersWithSpecificStatusesUseCase listOrdersWithSpecificStatusesUseCase;
private final ListOrdersByStatusUseCase listAllOrdersByStatus;
private final StartPreparingOrderUseCase startPreparingOrderUseCase;
private final FinishPreparingOrderUseCase finishPreparingOrderUseCase;
private final DeliverOrderUseCase deliverOrderUseCase;
Expand All @@ -35,7 +36,8 @@ public OrderController(
RemoveProductFromOrderUseCase removeProductFromOrderUseCase,
GetOrderUseCase getOrderUseCase,
SubmitOrderUseCase submitOrderUseCase,
ListOrdersUseCase listAllOrdersUseCase,
ListOrdersWithSpecificStatusesUseCase listOrdersWithSpecificStatusesUseCase,
ListOrdersByStatusUseCase listAllOrdersByStatus,
StartPreparingOrderUseCase startPreparingOrderUseCase,
FinishPreparingOrderUseCase finishPreparingOrderUseCase,
DeliverOrderUseCase deliverOrderUseCase) {
Expand All @@ -44,21 +46,33 @@ public OrderController(
this.removeProductFromOrderUseCase = removeProductFromOrderUseCase;
this.getOrderUseCase = getOrderUseCase;
this.submitOrderUseCase = submitOrderUseCase;
this.listAllOrdersUseCase = listAllOrdersUseCase;
this.listOrdersWithSpecificStatusesUseCase = listOrdersWithSpecificStatusesUseCase;
this.listAllOrdersByStatus = listAllOrdersByStatus;
this.startPreparingOrderUseCase = startPreparingOrderUseCase;
this.finishPreparingOrderUseCase = finishPreparingOrderUseCase;
this.deliverOrderUseCase = deliverOrderUseCase;
}

@GetMapping
@GetMapping()
@Operation(
summary = "Get all orders",
summary = "Get orders with specific status",
description =
"Get a list of all registered orders sorted by date and status or by status"
"Get a list of orders with statuses READY, PREPARING, and SUBMITTED, sorted by"
+ " status and date")
public ResponseEntity<List<OrderDTO>> findOrdersWithSpecificStatuses() {
List<OrderDTO> orders = listOrdersWithSpecificStatusesUseCase.execute();
return ResponseEntity.ok().body(orders);
}

@GetMapping(value = "/by-status")
@Operation(
summary = "Get orders of any status or by status",
description =
"Get a list of all registered orders of any status or by status"
+ " sorted by date via RequestParam. i.e., ?status=")
public ResponseEntity<List<OrderDTO>> findOrdersByStatus(
@RequestParam(value = "status", required = false) String status) {
List<OrderDTO> orders = listAllOrdersUseCase.execute(status);
List<OrderDTO> orders = listAllOrdersByStatus.execute(status);
return ResponseEntity.ok().body(orders);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ spring.profiles.active=dev
spring.jpa.open-in-view=false

# SWAGGER-UI CUSTOM PATH
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.swagger-ui.tagsSorter=alpha
springdoc.swagger-ui.operationsSorter=alpha

0 comments on commit 6de07a2

Please sign in to comment.