Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thomas Wiik #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ out/
.vscode/


application.yml
src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


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.Value;
Expand All @@ -13,6 +14,7 @@
import software.amazon.awssdk.services.eventbridge.EventBridgeClient;
import software.amazon.awssdk.services.eventbridge.model.PutEventsRequest;
import software.amazon.awssdk.services.eventbridge.model.PutEventsRequestEntry;
import software.amazon.awssdk.services.sns.model.NotFoundException;
import software.amazon.awssdk.services.sns.model.PublishRequest;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sns.SnsClient;
Expand All @@ -33,14 +35,17 @@ public class OrderController {
private String topicArn;
private String eventBusName;

public OrderController() {
private final OrderRepository orderRepository;

public OrderController(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
this.sqsClient = SqsClient.builder().build();
this.snsClient = SnsClient.builder().build();
this.eventBridgeClient = EventBridgeClient.builder().build();

this.queueUrl = "";
this.topicArn = "";
this.eventBusName = "";
this.queueUrl = "https://sqs.eu-west-1.amazonaws.com/637423341661/ic4rus90OrderQueue";
this.topicArn = "arn:aws:sns:eu-west-1:637423341661:ic4rus90OrderCreatedTopic";
this.eventBusName = "ic4rus90CustomEventBus";

this.objectMapper = new ObjectMapper();
}
Expand All @@ -56,8 +61,11 @@ public ResponseEntity<String> GetAllOrders() {
List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages();

for (Message message : messages) {
System.out.println(message);
try {
Order order = this.objectMapper.readValue(message.body(), Order.class);
String messageJson = this.objectMapper.readTree(message.body()).get("Message").asText();
Order order = this.objectMapper.readValue(messageJson, Order.class);

this.processOrder(order);

DeleteMessageRequest deleteRequest = DeleteMessageRequest.builder()
Expand All @@ -67,7 +75,7 @@ public ResponseEntity<String> GetAllOrders() {

sqsClient.deleteMessage(deleteRequest);
} catch (JsonProcessingException e) {
// e.printStackTrace();
System.out.println(e.getMessage());
}
}
String status = String.format("%d Orders have been processed", messages.size());
Expand All @@ -77,8 +85,12 @@ public ResponseEntity<String> GetAllOrders() {
@PostMapping
public ResponseEntity<String> createOrder(@RequestBody Order order) {
try {
Order savedOrder = orderRepository.save(order);
int savedOrderId = savedOrder.getId();
order.setId(savedOrderId);
String orderJson = objectMapper.writeValueAsString(order);
System.out.println(orderJson);

PublishRequest publishRequest = PublishRequest.builder()
.topicArn(topicArn)
.message(orderJson)
Expand Down Expand Up @@ -106,7 +118,41 @@ public ResponseEntity<String> createOrder(@RequestBody Order order) {
}
}

/*
@PutMapping("/{id}")
public ResponseEntity<String> updateOrder(@PathVariable (name="id") String id, @RequestBody Order order) {
try {
Order existingOrder = orderRepository
.findById(Integer.valueOf(id))
.orElseThrow();

existingOrder.setProduct(order.getProduct());
existingOrder.setQuantity(order.getQuantity());
existingOrder.setAmount(order.getAmount());
existingOrder.setTotal(order.getTotal());
order.setProcessed(order.isProcessed());

orderRepository.save(order);

String status = "Order updated, Message Published to SNS and Event Emitted to EventBridge";
return ResponseEntity.ok(status);
} catch (Exception e) {
// e.printStackTrace();
return ResponseEntity.status(500).body("Failed to create order");
}
}
*/


private void processOrder(Order order) {
System.out.println(order.toString());
int updatedTotal = order.getAmount() * order.getQuantity();
order.setTotal(updatedTotal);

orderRepository.findById(order.getId()).ifPresent(existingOrder -> {
existingOrder.setTotal(updatedTotal);
existingOrder.setProcessed(true);
orderRepository.save(existingOrder);
});
System.out.println("Processed order: " + order.getId());
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/booleanuk/OrderService/models/Order.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.booleanuk.OrderService.models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -10,6 +11,7 @@
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name = "orders")
public class Order {
Expand Down