Skip to content

Commit

Permalink
added order-status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
abidknashtech committed Dec 5, 2023
1 parent fe02cb8 commit c429652
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
@Data
public class FindOrderQuery {
String orderId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.nashtech.order.query;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class FindOrdersByUserQuery {
String userId;
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package com.nashtech.order.query;

import com.nashtech.common.utils.OrderStatus;
import com.nashtech.order.repository.OrderRepository;
import com.nashtech.order.restapi.response.OrderSummary;
import com.nashtech.order.repository.entity.OrderEntity;
import org.axonframework.queryhandling.QueryHandler;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
public class OrderQueriesHandler {

OrderRepository ordersRepository;
private final OrderRepository ordersRepository;

public OrderQueriesHandler(OrderRepository ordersRepository) {
this.ordersRepository = ordersRepository;
}

@QueryHandler
public OrderSummary findOrder(FindOrderQuery findOrderQuery) {
return OrderSummary.builder()
.orderId(findOrderQuery.getOrderId())
.orderStatus(OrderStatus.ORDER_PLACED.toString())
.build();
public List<OrderEntity> findOrders(FindOrdersByUserQuery findOrdersByUserQuery) {
return ordersRepository.findByUserId(findOrdersByUserQuery.getUserId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.nashtech.order.repository.entity.OrderEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface OrderRepository extends JpaRepository<OrderEntity, String> {
List<OrderEntity> findByUserId(String userId);
OrderEntity findByOrderId(String orderId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@

import com.nashtech.common.utils.OrderStatus;
import com.nashtech.order.commands.CreateOrderCommand;
import com.nashtech.order.query.FindOrderQuery;
import com.nashtech.order.query.FindOrdersByUserQuery;
import com.nashtech.order.repository.entity.OrderEntity;
import com.nashtech.order.restapi.request.OrderCreateRequest;
import com.nashtech.order.restapi.response.OrderSummary;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.messaging.responsetypes.ResponseTypes;
import org.axonframework.queryhandling.QueryGateway;
import org.axonframework.queryhandling.SubscriptionQueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/orders")
@Slf4j
public class OrdersCommandController {

private final CommandGateway commandGateway;
Expand All @@ -35,7 +40,7 @@ public OrdersCommandController(CommandGateway commandGateway, QueryGateway query
@PostMapping("/create")
public OrderSummary createOrder(@Valid @RequestBody OrderCreateRequest orderRequest) {
String orderId = UUID.randomUUID().toString();

log.info("Order {} created request ", orderId);
CreateOrderCommand createOrderCommand = CreateOrderCommand.builder()
.productId(orderRequest.getProductId()).userId(orderRequest.getUserId())
.quantity(orderRequest.getQuantity()).orderId(orderId)
Expand All @@ -57,4 +62,9 @@ public OrderSummary createOrder(@Valid @RequestBody OrderCreateRequest orderRequ
.build();
}

@GetMapping("/{userId}")
public List<OrderEntity> getOrdersByUser(@PathVariable String userId) throws ExecutionException, InterruptedException {
log.info("Get orders status by userId: {}", userId);
return queryGateway.query(new FindOrdersByUserQuery(userId), ResponseTypes.multipleInstancesOf(OrderEntity.class)).get();
}
}

0 comments on commit c429652

Please sign in to comment.