From 1750f5adb109c165b35ac5b51c0a4135d8f4e546 Mon Sep 17 00:00:00 2001 From: Andrija Stevanovic Date: Mon, 16 Feb 2026 16:56:00 +0100 Subject: [PATCH] async process on worker of AddRectangle --- .../AddRectangle/AddRectangleMessage.cs | 6 ++++ .../AddRectangleMessageHandler.cs | 19 ++++++++++ .../CreateRectangle/CreateRectangleCommand.cs | 3 +- .../CreateRectangleCommandHandler.cs | 7 ++-- .../RabbitMQ/RabbitMqSubscriber.cs | 1 + dotnet/AipsRT/AipsRT.csproj | 2 +- dotnet/AipsRT/Hubs/Dtos/AddRectangleDto.cs | 1 - dotnet/AipsRT/Hubs/WhiteboardHub.cs | 10 +++++- dotnet/AipsRT/Program.cs | 3 ++ .../Services/Interfaces/IMessagingService.cs | 8 +++++ dotnet/AipsRT/Services/MessagingService.cs | 36 +++++++++++++++++++ dotnet/AipsWorker/WorkerService.cs | 20 ++++++++++- 12 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 dotnet/AipsCore/Application/Common/Message/AddRectangle/AddRectangleMessage.cs create mode 100644 dotnet/AipsCore/Application/Common/Message/AddRectangle/AddRectangleMessageHandler.cs delete mode 100644 dotnet/AipsRT/Hubs/Dtos/AddRectangleDto.cs create mode 100644 dotnet/AipsRT/Services/Interfaces/IMessagingService.cs create mode 100644 dotnet/AipsRT/Services/MessagingService.cs diff --git a/dotnet/AipsCore/Application/Common/Message/AddRectangle/AddRectangleMessage.cs b/dotnet/AipsCore/Application/Common/Message/AddRectangle/AddRectangleMessage.cs new file mode 100644 index 0000000..0277ee1 --- /dev/null +++ b/dotnet/AipsCore/Application/Common/Message/AddRectangle/AddRectangleMessage.cs @@ -0,0 +1,6 @@ +using AipsCore.Application.Abstract.MessageBroking; +using AipsCore.Application.Models.Shape.Command.CreateRectangle; + +namespace AipsCore.Application.Common.Message.AddRectangle; + +public record AddRectangleMessage(CreateRectangleCommand Command) : IMessage; \ No newline at end of file diff --git a/dotnet/AipsCore/Application/Common/Message/AddRectangle/AddRectangleMessageHandler.cs b/dotnet/AipsCore/Application/Common/Message/AddRectangle/AddRectangleMessageHandler.cs new file mode 100644 index 0000000..44875c5 --- /dev/null +++ b/dotnet/AipsCore/Application/Common/Message/AddRectangle/AddRectangleMessageHandler.cs @@ -0,0 +1,19 @@ +using AipsCore.Application.Abstract; +using AipsCore.Application.Abstract.MessageBroking; + +namespace AipsCore.Application.Common.Message.AddRectangle; + +public class AddRectangleMessageHandler : IMessageHandler +{ + private readonly IDispatcher _dispatcher; + + public AddRectangleMessageHandler(IDispatcher dispatcher) + { + _dispatcher = dispatcher; + } + + public async Task Handle(AddRectangleMessage message, CancellationToken cancellationToken) + { + await _dispatcher.Execute(message.Command, cancellationToken); + } +} \ No newline at end of file diff --git a/dotnet/AipsCore/Application/Models/Shape/Command/CreateRectangle/CreateRectangleCommand.cs b/dotnet/AipsCore/Application/Models/Shape/Command/CreateRectangle/CreateRectangleCommand.cs index 84602d6..288d735 100644 --- a/dotnet/AipsCore/Application/Models/Shape/Command/CreateRectangle/CreateRectangleCommand.cs +++ b/dotnet/AipsCore/Application/Models/Shape/Command/CreateRectangle/CreateRectangleCommand.cs @@ -4,6 +4,7 @@ using AipsCore.Domain.Models.Shape.ValueObjects; namespace AipsCore.Application.Models.Shape.Command.CreateRectangle; public record CreateRectangleCommand( + string Id, string WhiteboardId, string AuthorId, int PositionX, @@ -11,4 +12,4 @@ public record CreateRectangleCommand( string Color, int EndPositionX, int EndPositionY, - int BorderThickness) : ICommand; \ No newline at end of file + int BorderThickness) : ICommand; \ No newline at end of file diff --git a/dotnet/AipsCore/Application/Models/Shape/Command/CreateRectangle/CreateRectangleCommandHandler.cs b/dotnet/AipsCore/Application/Models/Shape/Command/CreateRectangle/CreateRectangleCommandHandler.cs index d0352f8..543a2a1 100644 --- a/dotnet/AipsCore/Application/Models/Shape/Command/CreateRectangle/CreateRectangleCommandHandler.cs +++ b/dotnet/AipsCore/Application/Models/Shape/Command/CreateRectangle/CreateRectangleCommandHandler.cs @@ -13,7 +13,7 @@ using AipsCore.Domain.Models.Whiteboard.ValueObjects; namespace AipsCore.Application.Models.Shape.Command.CreateRectangle; -public class CreateRectangleCommandHandler : ICommandHandler +public class CreateRectangleCommandHandler : ICommandHandler { private readonly IShapeRepository _shapeRepository; private readonly IWhiteboardRepository _whiteboardRepository; @@ -28,11 +28,12 @@ public class CreateRectangleCommandHandler : ICommandHandler Handle(CreateRectangleCommand command, CancellationToken cancellationToken = default) + public async Task Handle(CreateRectangleCommand command, CancellationToken cancellationToken = default) { Validate(command); var rectangle = Rectangle.Create( + command.Id, command.WhiteboardId, command.AuthorId, command.PositionX, command.PositionY, @@ -43,8 +44,6 @@ public class CreateRectangleCommandHandler : ICommandHandler(), + autoDelete: false, durable: true); await channel.QueueBindAsync( diff --git a/dotnet/AipsRT/AipsRT.csproj b/dotnet/AipsRT/AipsRT.csproj index f311397..d4c6e59 100644 --- a/dotnet/AipsRT/AipsRT.csproj +++ b/dotnet/AipsRT/AipsRT.csproj @@ -15,7 +15,7 @@ - + diff --git a/dotnet/AipsRT/Hubs/Dtos/AddRectangleDto.cs b/dotnet/AipsRT/Hubs/Dtos/AddRectangleDto.cs deleted file mode 100644 index 3ba488c..0000000 --- a/dotnet/AipsRT/Hubs/Dtos/AddRectangleDto.cs +++ /dev/null @@ -1 +0,0 @@ -namespace AipsRT.Hubs.Dtos; \ No newline at end of file diff --git a/dotnet/AipsRT/Hubs/WhiteboardHub.cs b/dotnet/AipsRT/Hubs/WhiteboardHub.cs index f70af4f..6322be0 100644 --- a/dotnet/AipsRT/Hubs/WhiteboardHub.cs +++ b/dotnet/AipsRT/Hubs/WhiteboardHub.cs @@ -1,5 +1,7 @@ +using AipsCore.Application.Abstract.MessageBroking; using AipsRT.Model.Whiteboard; using AipsRT.Model.Whiteboard.Shapes; +using AipsRT.Services.Interfaces; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; @@ -9,10 +11,12 @@ namespace AipsRT.Hubs; public class WhiteboardHub : Hub { private readonly WhiteboardManager _whiteboardManager; + private readonly IMessagingService _messagingService; - public WhiteboardHub(WhiteboardManager whiteboardManager) + public WhiteboardHub(WhiteboardManager whiteboardManager, IMessagingService messagingService) { _whiteboardManager = whiteboardManager; + _messagingService = messagingService; } public async Task JoinWhiteboard(Guid whiteboardId) @@ -40,6 +44,10 @@ public class WhiteboardHub : Hub public async Task AddRectangle(Rectangle rectangle) { var whiteboard = _whiteboardManager.GetWhiteboardForUser(Guid.Parse(Context.UserIdentifier!))!; + + rectangle.OwnerId = Guid.Parse(Context.UserIdentifier!); + + await _messagingService.CreatedRectangle(whiteboard.WhiteboardId, rectangle); whiteboard.AddRectangle(rectangle); diff --git a/dotnet/AipsRT/Program.cs b/dotnet/AipsRT/Program.cs index c980533..dd0ef15 100644 --- a/dotnet/AipsRT/Program.cs +++ b/dotnet/AipsRT/Program.cs @@ -1,6 +1,8 @@ using AipsCore.Infrastructure.DI; using AipsRT.Hubs; using AipsRT.Model.Whiteboard; +using AipsRT.Services; +using AipsRT.Services.Interfaces; using DotNetEnv; using Microsoft.AspNetCore.SignalR; @@ -16,6 +18,7 @@ builder.Services.AddAips(builder.Configuration); builder.Services.AddScoped(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddCors(options => { diff --git a/dotnet/AipsRT/Services/Interfaces/IMessagingService.cs b/dotnet/AipsRT/Services/Interfaces/IMessagingService.cs new file mode 100644 index 0000000..de1f413 --- /dev/null +++ b/dotnet/AipsRT/Services/Interfaces/IMessagingService.cs @@ -0,0 +1,8 @@ +using AipsRT.Model.Whiteboard.Shapes; + +namespace AipsRT.Services.Interfaces; + +public interface IMessagingService +{ + Task CreatedRectangle(Guid whiteboardId, Rectangle rectangle); +} \ No newline at end of file diff --git a/dotnet/AipsRT/Services/MessagingService.cs b/dotnet/AipsRT/Services/MessagingService.cs new file mode 100644 index 0000000..980fae3 --- /dev/null +++ b/dotnet/AipsRT/Services/MessagingService.cs @@ -0,0 +1,36 @@ +using AipsCore.Application.Abstract.MessageBroking; +using AipsCore.Application.Common.Message.AddRectangle; +using AipsCore.Application.Models.Shape.Command.CreateRectangle; +using AipsRT.Model.Whiteboard.Shapes; +using AipsRT.Services.Interfaces; + +namespace AipsRT.Services; + +public class MessagingService : IMessagingService +{ + private readonly IMessagePublisher _messagePublisher; + + public MessagingService(IMessagePublisher messagePublisher) + { + _messagePublisher = messagePublisher; + } + + public async Task CreatedRectangle(Guid whiteboardId, Rectangle rectangle) + { + var command = new CreateRectangleCommand( + rectangle.Id.ToString(), + whiteboardId.ToString(), + rectangle.OwnerId.ToString(), + rectangle.Position.X, + rectangle.Position.Y, + rectangle.Color, + rectangle.EndPosition.X, + rectangle.EndPosition.Y, + rectangle.BorderThickness + ); + + var message = new AddRectangleMessage(command); + + await _messagePublisher.PublishAsync(message); + } +} \ No newline at end of file diff --git a/dotnet/AipsWorker/WorkerService.cs b/dotnet/AipsWorker/WorkerService.cs index dea0790..41a082c 100644 --- a/dotnet/AipsWorker/WorkerService.cs +++ b/dotnet/AipsWorker/WorkerService.cs @@ -2,6 +2,7 @@ using System.Reflection; using AipsCore.Application.Abstract; using AipsCore.Application.Abstract.MessageBroking; using AipsCore.Application.Common.Message.TestMessage; +using AipsCore.Domain.Common.Validation; using AipsWorker.Utilities; using Microsoft.Extensions.Hosting; @@ -48,7 +49,24 @@ public class WorkerService : BackgroundService private async Task HandleMessage(T message, CancellationToken ct) where T : IMessage { - await _dispatcher.Execute(message, ct); + try + { + await _dispatcher.Execute(message, ct); + } + catch (ValidationException validationException) + { + Console.WriteLine("===Validation Exception: "); + foreach (var error in validationException.ValidationErrors) + { + Console.WriteLine(" * Code: " + error.Code); + Console.WriteLine(" * Message: " + error.Message); + Console.WriteLine("==================="); + } + } + catch (Exception ex) + { + Console.WriteLine("Unhandled Exception: " + ex.Message); + } } private MethodInfo GetMessageHandleMethod(Type messageType)