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

Jostein Ruen #12

Open
wants to merge 3 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,28 +21,46 @@
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;
private ObjectMapper objectMapper;
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();
}
Expand Down Expand Up @@ -77,8 +97,10 @@ public ResponseEntity<String> GetAllOrders() {
@PostMapping
public ResponseEntity<String> 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)
Expand All @@ -101,10 +123,26 @@ public ResponseEntity<String> 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<String> 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());
Expand Down
56 changes: 52 additions & 4 deletions src/main/java/com/booleanuk/OrderService/models/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor

@Entity
@Table(name = "orders")
public class Order {
Expand Down Expand Up @@ -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;
}
}