diff --git a/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommandHandler.cs b/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommandHandler.cs index f5baf88..3d3323f 100644 --- a/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommandHandler.cs +++ b/dotnet/AipsCore/Application/Models/Whiteboard/Command/CreateWhiteboard/CreateWhiteboardCommandHandler.cs @@ -18,15 +18,7 @@ public class CreateWhiteboardCommandHandler : ICommandHandler Handle(CreateWhiteboardCommand command, CancellationToken cancellationToken = default) { - WhiteboardCode whiteboardCode; - bool codeExists; - - do - { - whiteboardCode = GenerateUniqueWhiteboardCode(); - - codeExists = await _whiteboardRepository.WhiteboardCodeExists(whiteboardCode); - } while (codeExists); + var whiteboardCode = await WhiteboardCode.GenerateUniqueAsync(_whiteboardRepository); var whiteboard = Domain.Models.Whiteboard.Whiteboard.Create(command.OwnerId, whiteboardCode.CodeValue, command.Title); @@ -35,18 +27,4 @@ public class CreateWhiteboardCommandHandler : ICommandHandler GenerateUniqueAsync(IWhiteboardRepository whiteboardRepository) + { + WhiteboardCode whiteboardCode; + bool codeExists; + + do + { + whiteboardCode = Generate(); + + codeExists = await whiteboardRepository.WhiteboardCodeExists(whiteboardCode); + } while (codeExists); + + return whiteboardCode; + } + + public static WhiteboardCode Generate() + { + 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