diff --git a/src/main/java/com/booleanuk/OrderService/controllers/OrderController.java b/src/main/java/com/booleanuk/OrderService/controllers/OrderController.java index 39cdc87..2d7e40c 100644 --- a/src/main/java/com/booleanuk/OrderService/controllers/OrderController.java +++ b/src/main/java/com/booleanuk/OrderService/controllers/OrderController.java @@ -2,8 +2,10 @@ import com.booleanuk.OrderService.models.Order; +import com.booleanuk.OrderService.repositories.OrderRepository; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; @@ -19,12 +21,19 @@ import software.amazon.awssdk.services.sqs.model.DeleteMessageRequest; import software.amazon.awssdk.services.sqs.model.Message; import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.sns.SnsClient; +import software.amazon.awssdk.services.sqs.SqsClient; +import software.amazon.awssdk.services.eventbridge.EventBridgeClient; import java.util.List; @RestController @RequestMapping("orders") public class OrderController { + @Autowired + private OrderRepository orderRepository; + private SqsClient sqsClient; private SnsClient snsClient; private EventBridgeClient eventBridgeClient; @@ -32,15 +41,26 @@ public class OrderController { private String queueUrl; private String topicArn; private String eventBusName; + private String RuleArn; + private String QueueArn; + private String subscriptionArn; public OrderController() { - this.sqsClient = SqsClient.builder().build(); - this.snsClient = SnsClient.builder().build(); - this.eventBridgeClient = EventBridgeClient.builder().build(); + Region region = Region.EU_WEST_1; // Specify your region here + + - this.queueUrl = ""; - this.topicArn = ""; - this.eventBusName = ""; + + this.sqsClient = SqsClient.builder().region(region).build(); + this.snsClient = SnsClient.builder().region(region).build(); + this.eventBridgeClient = EventBridgeClient.builder().region(region).build(); + + this.queueUrl = "https://sqs.eu-west-1.amazonaws.com/637423341661/josteinruenOrderQueue"; + this.topicArn = "arn:aws:sns:eu-west-1:637423341661:josteinruenOrderCreatedTopic"; + this.eventBusName = "arn:aws:events:eu-west-1:637423341661:event-bus/josteinruenCustomEventBus"; + this.RuleArn ="arn:aws:events:eu-west-1:637423341661:rule/josteinruenCustomEventBus/josteinruenOrderProcessedRule"; + this.QueueArn = "arn:aws:sqs:eu-west-1:637423341661:josteinruenOrderQueue"; + this.subscriptionArn = "\"arn:aws:sns:eu-west-1:637423341661:josteinruenOrderCreatedTopic:4587c0f2-2a33-4c27-9b78-b0bb20dd8ac2\""; this.objectMapper = new ObjectMapper(); } @@ -77,8 +97,10 @@ public ResponseEntity GetAllOrders() { @PostMapping public ResponseEntity createOrder(@RequestBody Order order) { try { + order.setTotal(order.getQuantity() * order.getAmount()); + orderRepository.save(order); + String orderJson = objectMapper.writeValueAsString(order); - System.out.println(orderJson); PublishRequest publishRequest = PublishRequest.builder() .topicArn(topicArn) .message(orderJson) @@ -101,10 +123,26 @@ public ResponseEntity createOrder(@RequestBody Order order) { String status = "Order created, Message Published to SNS and Event Emitted to EventBridge"; return ResponseEntity.ok(status); } catch (JsonProcessingException e) { -// e.printStackTrace(); return ResponseEntity.status(500).body("Failed to create order"); } } + + + @PutMapping("/{id}") + public ResponseEntity updateOrder(@PathVariable Integer id, @RequestBody Order updatedOrder) { + return orderRepository.findById(id) + .map(order -> { + order.setProcessed(updatedOrder.isProcessed()); + order.setQuantity(updatedOrder.getQuantity()); + order.setAmount(updatedOrder.getAmount()); + order.setTotal(updatedOrder.getQuantity() * updatedOrder.getAmount()); + orderRepository.save(order); + return ResponseEntity.ok("Order updated"); + }) + .orElseGet(() -> ResponseEntity.status(404).body("Order not found")); + } + + private void processOrder(Order order) { System.out.println(order.toString()); diff --git a/src/main/java/com/booleanuk/OrderService/models/Order.java b/src/main/java/com/booleanuk/OrderService/models/Order.java index a7669de..33f4d47 100644 --- a/src/main/java/com/booleanuk/OrderService/models/Order.java +++ b/src/main/java/com/booleanuk/OrderService/models/Order.java @@ -6,10 +6,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor + @Entity @Table(name = "orders") public class Order { @@ -39,4 +36,55 @@ public Order(String product, int quantity, int amount, boolean processed, int to this.processed = processed; this.total = total; } + + public Order() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getProduct() { + return product; + } + + public void setProduct(String product) { + this.product = product; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public int getAmount() { + return amount; + } + + public void setAmount(int amount) { + this.amount = amount; + } + + public boolean isProcessed() { + return processed; + } + + public void setProcessed(boolean processed) { + this.processed = processed; + } + + public int getTotal() { + return total; + } + + public void setTotal(int total) { + this.total = total; + } }