Skip to content

Commit

Permalink
add ability to list orders
Browse files Browse the repository at this point in the history
  • Loading branch information
tkeburia committed Dec 11, 2023
1 parent f23c7f0 commit 04e1a3d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.redbadger.orderservice.controller;

import com.redbadger.orderservice.dto.OrderDto;
import com.redbadger.orderservice.dto.OrderRequest;
import com.redbadger.orderservice.service.OrderService;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
Expand All @@ -9,6 +10,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
Expand All @@ -27,6 +29,13 @@ public CompletableFuture<String> placeOrder(@RequestBody OrderRequest orderReque
return CompletableFuture.supplyAsync(() -> orderService.placeOrder(orderRequest));
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public List<OrderDto> getOrders() {
return orderService.getOrders();
}


public CompletableFuture<String> fallbackMethod(OrderRequest orderRequest, RuntimeException runtimeException) {
return CompletableFuture.supplyAsync(() -> "Oops! Something went wrong, please order after some time!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderDto {
private String orderNumber;
private List<OrderLineItemsDto> items;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.redbadger.orderservice.service;

import com.redbadger.orderservice.dto.InventoryResponse;
import com.redbadger.orderservice.dto.OrderDto;
import com.redbadger.orderservice.dto.OrderLineItemsDto;
import com.redbadger.orderservice.dto.OrderRequest;
import com.redbadger.orderservice.event.OrderPlacedEvent;
Expand Down Expand Up @@ -28,6 +29,12 @@ public class OrderService {
private final WebClient.Builder webClientBuilder;
private final KafkaTemplate<String, OrderPlacedEvent> kafkaTemplate;

public List<OrderDto> getOrders() {
final List<Order> all = orderRepository.findAll();

return all.stream().map(this::mapToOrder).toList();
}

public String placeOrder(OrderRequest orderRequest) {
Order order = new Order();
order.setOrderNumber(UUID.randomUUID().toString());
Expand Down Expand Up @@ -76,4 +83,17 @@ private OrderLineItems mapToDto(OrderLineItemsDto orderLineItemsDto) {
orderLineItems.setSkuCode(orderLineItemsDto.getSkuCode());
return orderLineItems;
}

private OrderDto mapToOrder(Order order) {
final String orderNumber = order.getOrderNumber();

final List<OrderLineItemsDto> items = order.getOrderLineItemsList()
.stream()
.map(entry -> new OrderLineItemsDto(entry.getSkuCode(), entry.getPrice(), entry.getQuantity()))
.toList();

return new OrderDto(orderNumber, items);


}
}

0 comments on commit 04e1a3d

Please sign in to comment.