async process on worker of AddRectangle

This commit is contained in:
2026-02-16 16:56:00 +01:00
parent d9caeb2209
commit 1750f5adb1
12 changed files with 107 additions and 9 deletions

View File

@@ -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;

View File

@@ -0,0 +1,19 @@
using AipsCore.Application.Abstract;
using AipsCore.Application.Abstract.MessageBroking;
namespace AipsCore.Application.Common.Message.AddRectangle;
public class AddRectangleMessageHandler : IMessageHandler<AddRectangleMessage>
{
private readonly IDispatcher _dispatcher;
public AddRectangleMessageHandler(IDispatcher dispatcher)
{
_dispatcher = dispatcher;
}
public async Task Handle(AddRectangleMessage message, CancellationToken cancellationToken)
{
await _dispatcher.Execute(message.Command, cancellationToken);
}
}

View File

@@ -4,6 +4,7 @@ using AipsCore.Domain.Models.Shape.ValueObjects;
namespace AipsCore.Application.Models.Shape.Command.CreateRectangle; namespace AipsCore.Application.Models.Shape.Command.CreateRectangle;
public record CreateRectangleCommand( public record CreateRectangleCommand(
string Id,
string WhiteboardId, string WhiteboardId,
string AuthorId, string AuthorId,
int PositionX, int PositionX,
@@ -11,4 +12,4 @@ public record CreateRectangleCommand(
string Color, string Color,
int EndPositionX, int EndPositionX,
int EndPositionY, int EndPositionY,
int BorderThickness) : ICommand<ShapeId>; int BorderThickness) : ICommand;

View File

@@ -13,7 +13,7 @@ using AipsCore.Domain.Models.Whiteboard.ValueObjects;
namespace AipsCore.Application.Models.Shape.Command.CreateRectangle; namespace AipsCore.Application.Models.Shape.Command.CreateRectangle;
public class CreateRectangleCommandHandler : ICommandHandler<CreateRectangleCommand, ShapeId> public class CreateRectangleCommandHandler : ICommandHandler<CreateRectangleCommand>
{ {
private readonly IShapeRepository _shapeRepository; private readonly IShapeRepository _shapeRepository;
private readonly IWhiteboardRepository _whiteboardRepository; private readonly IWhiteboardRepository _whiteboardRepository;
@@ -28,11 +28,12 @@ public class CreateRectangleCommandHandler : ICommandHandler<CreateRectangleComm
_unitOfWork = unitOfWork; _unitOfWork = unitOfWork;
} }
public async Task<ShapeId> Handle(CreateRectangleCommand command, CancellationToken cancellationToken = default) public async Task Handle(CreateRectangleCommand command, CancellationToken cancellationToken = default)
{ {
Validate(command); Validate(command);
var rectangle = Rectangle.Create( var rectangle = Rectangle.Create(
command.Id,
command.WhiteboardId, command.WhiteboardId,
command.AuthorId, command.AuthorId,
command.PositionX, command.PositionY, command.PositionX, command.PositionY,
@@ -43,8 +44,6 @@ public class CreateRectangleCommandHandler : ICommandHandler<CreateRectangleComm
await _shapeRepository.SaveAsync(rectangle, cancellationToken); await _shapeRepository.SaveAsync(rectangle, cancellationToken);
await _unitOfWork.SaveChangesAsync(cancellationToken); await _unitOfWork.SaveChangesAsync(cancellationToken);
return rectangle.Id;
} }
private void Validate(CreateRectangleCommand command) private void Validate(CreateRectangleCommand command)

View File

@@ -30,6 +30,7 @@ public class RabbitMqSubscriber : IMessageSubscriber
await channel.QueueDeclareAsync( await channel.QueueDeclareAsync(
queue: GetQueueName<T>(), queue: GetQueueName<T>(),
autoDelete: false,
durable: true); durable: true);
await channel.QueueBindAsync( await channel.QueueBindAsync(

View File

@@ -15,7 +15,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Services\" /> <Folder Include="Hubs\Dtos\" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1 +0,0 @@
namespace AipsRT.Hubs.Dtos;

View File

@@ -1,5 +1,7 @@
using AipsCore.Application.Abstract.MessageBroking;
using AipsRT.Model.Whiteboard; using AipsRT.Model.Whiteboard;
using AipsRT.Model.Whiteboard.Shapes; using AipsRT.Model.Whiteboard.Shapes;
using AipsRT.Services.Interfaces;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
@@ -9,10 +11,12 @@ namespace AipsRT.Hubs;
public class WhiteboardHub : Hub public class WhiteboardHub : Hub
{ {
private readonly WhiteboardManager _whiteboardManager; private readonly WhiteboardManager _whiteboardManager;
private readonly IMessagingService _messagingService;
public WhiteboardHub(WhiteboardManager whiteboardManager) public WhiteboardHub(WhiteboardManager whiteboardManager, IMessagingService messagingService)
{ {
_whiteboardManager = whiteboardManager; _whiteboardManager = whiteboardManager;
_messagingService = messagingService;
} }
public async Task JoinWhiteboard(Guid whiteboardId) public async Task JoinWhiteboard(Guid whiteboardId)
@@ -41,6 +45,10 @@ public class WhiteboardHub : Hub
{ {
var whiteboard = _whiteboardManager.GetWhiteboardForUser(Guid.Parse(Context.UserIdentifier!))!; var whiteboard = _whiteboardManager.GetWhiteboardForUser(Guid.Parse(Context.UserIdentifier!))!;
rectangle.OwnerId = Guid.Parse(Context.UserIdentifier!);
await _messagingService.CreatedRectangle(whiteboard.WhiteboardId, rectangle);
whiteboard.AddRectangle(rectangle); whiteboard.AddRectangle(rectangle);
await Clients.GroupExcept(whiteboard.WhiteboardId.ToString(), Context.ConnectionId) await Clients.GroupExcept(whiteboard.WhiteboardId.ToString(), Context.ConnectionId)

View File

@@ -1,6 +1,8 @@
using AipsCore.Infrastructure.DI; using AipsCore.Infrastructure.DI;
using AipsRT.Hubs; using AipsRT.Hubs;
using AipsRT.Model.Whiteboard; using AipsRT.Model.Whiteboard;
using AipsRT.Services;
using AipsRT.Services.Interfaces;
using DotNetEnv; using DotNetEnv;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
@@ -16,6 +18,7 @@ builder.Services.AddAips(builder.Configuration);
builder.Services.AddScoped<GetWhiteboardService>(); builder.Services.AddScoped<GetWhiteboardService>();
builder.Services.AddSingleton<WhiteboardManager>(); builder.Services.AddSingleton<WhiteboardManager>();
builder.Services.AddSingleton<IMessagingService, MessagingService>();
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {

View File

@@ -0,0 +1,8 @@
using AipsRT.Model.Whiteboard.Shapes;
namespace AipsRT.Services.Interfaces;
public interface IMessagingService
{
Task CreatedRectangle(Guid whiteboardId, Rectangle rectangle);
}

View File

@@ -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);
}
}

View File

@@ -2,6 +2,7 @@ using System.Reflection;
using AipsCore.Application.Abstract; using AipsCore.Application.Abstract;
using AipsCore.Application.Abstract.MessageBroking; using AipsCore.Application.Abstract.MessageBroking;
using AipsCore.Application.Common.Message.TestMessage; using AipsCore.Application.Common.Message.TestMessage;
using AipsCore.Domain.Common.Validation;
using AipsWorker.Utilities; using AipsWorker.Utilities;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@@ -48,7 +49,24 @@ public class WorkerService : BackgroundService
private async Task HandleMessage<T>(T message, CancellationToken ct) where T : IMessage private async Task HandleMessage<T>(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) private MethodInfo GetMessageHandleMethod(Type messageType)