diff --git a/Images/Commands executed success.png b/Images/Commands executed success.png new file mode 100644 index 0000000..535c842 Binary files /dev/null and b/Images/Commands executed success.png differ diff --git a/Images/First command success.png b/Images/First command success.png new file mode 100644 index 0000000..941f680 Binary files /dev/null and b/Images/First command success.png differ diff --git a/WorkshopBackend/OrderService/Controllers/OrderController.cs b/WorkshopBackend/OrderService/Controllers/OrderController.cs index e49adda..6a016af 100644 --- a/WorkshopBackend/OrderService/Controllers/OrderController.cs +++ b/WorkshopBackend/OrderService/Controllers/OrderController.cs @@ -7,6 +7,8 @@ using Amazon.EventBridge.Model; using System.Text.Json; using InventoryService.Models; +using Npgsql; +using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database; [ApiController] [Route("[controller]")] @@ -15,8 +17,9 @@ public class OrderController : ControllerBase private readonly IAmazonSQS _sqs; private readonly IAmazonSimpleNotificationService _sns; private readonly IAmazonEventBridge _eventBridge; - private readonly string _queueUrl = ""; // Format of https://.* - private readonly string _topicArn = ""; // Format of arn:aws.* + private readonly string _queueUrl = "https://sqs.eu-north-1.amazonaws.com/637423341661/MalteStengardOrderQueue"; // Format of https://.* + private readonly string _topicArn = "arn:aws:sns:eu-north-1:637423341661:MalteStengardOrderCreatedTopic"; // Format of arn:aws.* + private readonly string _connectionString = "your database info"; public OrderController() { @@ -24,38 +27,71 @@ public OrderController() _sqs = new AmazonSQSClient(); _sns = new AmazonSimpleNotificationServiceClient(); _eventBridge = new AmazonEventBridgeClient(); + } [HttpGet] public async Task GetOrdersAndProcess() { - /* - * AmazonSQSClient - * - */ var request = new ReceiveMessageRequest { QueueUrl = _queueUrl, MaxNumberOfMessages = 10, WaitTimeSeconds = 20 }; - var response = await _sqs.ReceiveMessageAsync(request); - foreach (var message in response.Messages) + using (var connection = new NpgsqlConnection(_connectionString)) { - var order = JsonSerializer.Deserialize(message.Body); - // Process order (e.g., update inventory) + await connection.OpenAsync(); - // Delete message after processing - await _sqs.DeleteMessageAsync(_queueUrl, message.ReceiptHandle); + foreach (var message in response.Messages) + { + try + { + var order = JsonSerializer.Deserialize(message.Body); + + // Update order in the database + var query = $@"UPDATE orders SET processed = @Processed, total = @Total WHERE orderid = @OrderId"; + await using (var command = new NpgsqlCommand(query, connection)) + { + command.Parameters.AddWithValue("@Processed", true); + command.Parameters.AddWithValue("@Total", order.Quantity * order.Amount); + command.Parameters.AddWithValue("@OrderId", order.OrderId); + await command.ExecuteNonQueryAsync(); + } + + // Delete message from SQS after processing + await _sqs.DeleteMessageAsync(_queueUrl, message.ReceiptHandle); + } + catch (Exception ex) + { + // Log the exception (consider using a logging framework) + Console.WriteLine($"Error processing order {message.Body}: {ex.Message}"); + } + } } - return Ok(new { Status = $"{response.Messages.Count()}Order have been processed" }); + return Ok(new { Status = $"{response.Messages.Count()} Order(s) have been processed" }); } + [HttpPost] public async Task CreateOrder([FromBody] Order order) { + using (var connection = new NpgsqlConnection(_connectionString)) + { + await connection.OpenAsync(); + var query = "INSERT INTO Orders (product, quantity, amount, processed, total) VALUES (@Product, @Quantity, @Amount, @Processed, @Total)"; + using (var command = new NpgsqlCommand(query, connection)) + { + command.Parameters.AddWithValue("@Product", order.Product); + command.Parameters.AddWithValue("@Quantity", order.Quantity); + command.Parameters.AddWithValue("@Amount", order.Amount); + command.Parameters.AddWithValue("@Processed", order.Processed); + command.Parameters.AddWithValue("@Total", order.Total); + await command.ExecuteNonQueryAsync(); + } + } /* * AmazonSimpleNotificationServiceClient * diff --git a/WorkshopBackend/OrderService/OrderService.csproj b/WorkshopBackend/OrderService/OrderService.csproj index 385ff06..fb92fb4 100644 --- a/WorkshopBackend/OrderService/OrderService.csproj +++ b/WorkshopBackend/OrderService/OrderService.csproj @@ -12,6 +12,7 @@ + diff --git a/WorkshopBackend/OrderService/appsettings.json b/WorkshopBackend/OrderService/appsettings.json index 1c233fe..5b3e784 100644 --- a/WorkshopBackend/OrderService/appsettings.json +++ b/WorkshopBackend/OrderService/appsettings.json @@ -1,7 +1,7 @@ { "AWS": { "Profile": "default", - "Region": "us-west-2" + "Region": "eu-north-1" }, "Logging": { "LogLevel": { diff --git a/WorkshopBackend/policy.json b/WorkshopBackend/policy.json new file mode 100644 index 0000000..75324bb --- /dev/null +++ b/WorkshopBackend/policy.json @@ -0,0 +1,3 @@ +{ + "Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"SQS:SendMessage\",\"Resource\":\"arn:aws:sqs:eu-north-1:637423341661:MalteStengardOrderQueue\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"arn:aws:sns:eu-north-1:637423341661:MalteStengardOrderCreatedTopic\"}}}]}" +} \ No newline at end of file