async process on worker of AddRectangle
This commit is contained in:
@@ -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;
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<ShapeId>;
|
||||
int BorderThickness) : ICommand;
|
||||
@@ -13,7 +13,7 @@ using AipsCore.Domain.Models.Whiteboard.ValueObjects;
|
||||
|
||||
namespace AipsCore.Application.Models.Shape.Command.CreateRectangle;
|
||||
|
||||
public class CreateRectangleCommandHandler : ICommandHandler<CreateRectangleCommand, ShapeId>
|
||||
public class CreateRectangleCommandHandler : ICommandHandler<CreateRectangleCommand>
|
||||
{
|
||||
private readonly IShapeRepository _shapeRepository;
|
||||
private readonly IWhiteboardRepository _whiteboardRepository;
|
||||
@@ -28,11 +28,12 @@ public class CreateRectangleCommandHandler : ICommandHandler<CreateRectangleComm
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<ShapeId> 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<CreateRectangleComm
|
||||
|
||||
await _shapeRepository.SaveAsync(rectangle, cancellationToken);
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return rectangle.Id;
|
||||
}
|
||||
|
||||
private void Validate(CreateRectangleCommand command)
|
||||
|
||||
@@ -30,6 +30,7 @@ public class RabbitMqSubscriber : IMessageSubscriber
|
||||
|
||||
await channel.QueueDeclareAsync(
|
||||
queue: GetQueueName<T>(),
|
||||
autoDelete: false,
|
||||
durable: true);
|
||||
|
||||
await channel.QueueBindAsync(
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Services\" />
|
||||
<Folder Include="Hubs\Dtos\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
namespace AipsRT.Hubs.Dtos;
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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<GetWhiteboardService>();
|
||||
builder.Services.AddSingleton<WhiteboardManager>();
|
||||
builder.Services.AddSingleton<IMessagingService, MessagingService>();
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
|
||||
8
dotnet/AipsRT/Services/Interfaces/IMessagingService.cs
Normal file
8
dotnet/AipsRT/Services/Interfaces/IMessagingService.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using AipsRT.Model.Whiteboard.Shapes;
|
||||
|
||||
namespace AipsRT.Services.Interfaces;
|
||||
|
||||
public interface IMessagingService
|
||||
{
|
||||
Task CreatedRectangle(Guid whiteboardId, Rectangle rectangle);
|
||||
}
|
||||
36
dotnet/AipsRT/Services/MessagingService.cs
Normal file
36
dotnet/AipsRT/Services/MessagingService.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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>(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)
|
||||
|
||||
Reference in New Issue
Block a user