diff --git a/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommand.cs b/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommand.cs new file mode 100644 index 0000000..616d7c2 --- /dev/null +++ b/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommand.cs @@ -0,0 +1,7 @@ +using System.Windows.Input; +using AipsCore.Application.Abstract.Command; +using AipsCore.Domain.Models.Whiteboard.ValueObjects; + +namespace AipsCore.Application.Models.Whiteboard.Command.CreateWhiteboard; + +public record CreateWhiteboardCommand(string OwnerId, string Title) : ICommand; \ No newline at end of file diff --git a/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommandHandler.cs b/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommandHandler.cs new file mode 100644 index 0000000..f5baf88 --- /dev/null +++ b/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommandHandler.cs @@ -0,0 +1,52 @@ +using AipsCore.Application.Abstract.Command; +using AipsCore.Domain.Abstract; +using AipsCore.Domain.Models.Whiteboard.External; +using AipsCore.Domain.Models.Whiteboard.ValueObjects; + +namespace AipsCore.Application.Models.Whiteboard.Command.CreateWhiteboard; + +public class CreateWhiteboardCommandHandler : ICommandHandler +{ + private readonly IWhiteboardRepository _whiteboardRepository; + private readonly IUnitOfWork _unitOfWork; + + public CreateWhiteboardCommandHandler(IWhiteboardRepository whiteboardRepository, IUnitOfWork unitOfWork) + { + _whiteboardRepository = whiteboardRepository; + _unitOfWork = unitOfWork; + } + + public async Task Handle(CreateWhiteboardCommand command, CancellationToken cancellationToken = default) + { + WhiteboardCode whiteboardCode; + bool codeExists; + + do + { + whiteboardCode = GenerateUniqueWhiteboardCode(); + + codeExists = await _whiteboardRepository.WhiteboardCodeExists(whiteboardCode); + } while (codeExists); + + var whiteboard = Domain.Models.Whiteboard.Whiteboard.Create(command.OwnerId, whiteboardCode.CodeValue, command.Title); + + await _whiteboardRepository.Save(whiteboard, cancellationToken); + await _unitOfWork.SaveChangesAsync(cancellationToken); + + return whiteboard.Id; + } + + // TRENUTNO SAMO, CE SE NAPRAVI KO SERVIS IL KAKO VEC KASNIJE + private static WhiteboardCode GenerateUniqueWhiteboardCode() + { + var rng = new Random(); + char[] result = new char[8]; + + for (int i = 0; i < result.Length; i++) + { + result[i] = (char)('0' + rng.Next(0, 10)); + } + + return new WhiteboardCode(new string(result)); + } +} \ No newline at end of file