implement

This commit is contained in:
2026-02-09 19:15:34 +01:00
parent e3d229a61d
commit df53d86f4c
2 changed files with 30 additions and 23 deletions

View File

@@ -18,15 +18,7 @@ public class CreateWhiteboardCommandHandler : ICommandHandler<CreateWhiteboardCo
public async Task<WhiteboardId> Handle(CreateWhiteboardCommand command, CancellationToken cancellationToken = default) public async Task<WhiteboardId> Handle(CreateWhiteboardCommand command, CancellationToken cancellationToken = default)
{ {
WhiteboardCode whiteboardCode; var whiteboardCode = await WhiteboardCode.GenerateUniqueAsync(_whiteboardRepository);
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); var whiteboard = Domain.Models.Whiteboard.Whiteboard.Create(command.OwnerId, whiteboardCode.CodeValue, command.Title);
@@ -35,18 +27,4 @@ public class CreateWhiteboardCommandHandler : ICommandHandler<CreateWhiteboardCo
return whiteboard.Id; 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));
}
} }

View File

@@ -1,6 +1,7 @@
using AipsCore.Domain.Abstract.Rule; using AipsCore.Domain.Abstract.Rule;
using AipsCore.Domain.Abstract.ValueObject; using AipsCore.Domain.Abstract.ValueObject;
using AipsCore.Domain.Common.Validation.Rules; using AipsCore.Domain.Common.Validation.Rules;
using AipsCore.Domain.Models.Whiteboard.External;
using AipsCore.Domain.Models.Whiteboard.Validation; using AipsCore.Domain.Models.Whiteboard.Validation;
namespace AipsCore.Domain.Models.Whiteboard.ValueObjects; namespace AipsCore.Domain.Models.Whiteboard.ValueObjects;
@@ -25,4 +26,32 @@ public record WhiteboardCode : AbstractValueObject
new WhiteboardCodeCharsetRule(CodeValue) new WhiteboardCodeCharsetRule(CodeValue)
]; ];
} }
public async static Task<WhiteboardCode> 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));
}
} }