diff --git a/dotnet/AipsCore/Application/Models/Whiteboard/Command/JoinWithCode/JoinWithCodeCommand.cs b/dotnet/AipsCore/Application/Models/Whiteboard/Command/JoinWithCode/JoinWithCodeCommand.cs new file mode 100644 index 0000000..21e5ae2 --- /dev/null +++ b/dotnet/AipsCore/Application/Models/Whiteboard/Command/JoinWithCode/JoinWithCodeCommand.cs @@ -0,0 +1,5 @@ +using AipsCore.Application.Abstract.Command; + +namespace AipsCore.Application.Models.Whiteboard.Command.JoinWithCode; + +public record JoinWithCodeCommand(string Code): ICommand; \ No newline at end of file diff --git a/dotnet/AipsCore/Application/Models/Whiteboard/Command/JoinWithCode/JoinWithCodeCommandHandler.cs b/dotnet/AipsCore/Application/Models/Whiteboard/Command/JoinWithCode/JoinWithCodeCommandHandler.cs new file mode 100644 index 0000000..c9139a2 --- /dev/null +++ b/dotnet/AipsCore/Application/Models/Whiteboard/Command/JoinWithCode/JoinWithCodeCommandHandler.cs @@ -0,0 +1,65 @@ +using AipsCore.Application.Abstract.Command; +using AipsCore.Application.Abstract.UserContext; +using AipsCore.Domain.Abstract; +using AipsCore.Domain.Common.Validation; +using AipsCore.Domain.Models.Whiteboard.External; +using AipsCore.Domain.Models.Whiteboard.Validation; +using AipsCore.Domain.Models.Whiteboard.ValueObjects; +using AipsCore.Domain.Models.WhiteboardMembership.Enums; +using AipsCore.Domain.Models.WhiteboardMembership.External; + +namespace AipsCore.Application.Models.Whiteboard.Command.JoinWithCode; + +public class JoinWithCodeCommandHandler : ICommandHandler +{ + private readonly IWhiteboardRepository _whiteboardRepository; + private readonly IWhiteboardMembershipRepository _whiteboardMembershipRepository; + private readonly IUnitOfWork _unitOfWork; + private readonly IUserContext _userContext; + + public JoinWithCodeCommandHandler( + IWhiteboardRepository whiteboardRepository, + IWhiteboardMembershipRepository whiteboardMembershipRepository, + IUnitOfWork unitOfWork, + IUserContext userContext) + { + _whiteboardRepository = whiteboardRepository; + _whiteboardMembershipRepository = whiteboardMembershipRepository; + _unitOfWork = unitOfWork; + _userContext = userContext; + } + + public async Task Handle(JoinWithCodeCommand command, CancellationToken cancellationToken = default) + { + var userId = _userContext.GetCurrentUserId(); + + var code = new WhiteboardCode(command.Code); + var whiteboard = await _whiteboardRepository.GetByCodeAsync(code, cancellationToken); + + if (whiteboard is null) + { + throw new ValidationException(WhiteboardErrors.NotFound(code)); + } + + if (!whiteboard.ShouldRequestToJoin(userId)) + { + return new JoinWithCodeDto(whiteboard.Id.IdValue, WhiteboardMembershipStatus.Accepted); + } + + var membership = await _whiteboardMembershipRepository.GetByWhiteboardAndUserAsync(whiteboard.Id, userId, cancellationToken); + + if (membership is null) + { + membership = whiteboard.RequestJoin(userId); + } + else + { + whiteboard.RequestReJoin(membership); + } + + await _whiteboardMembershipRepository.SaveAsync(membership, cancellationToken); + await _unitOfWork.SaveChangesAsync(cancellationToken); + + return new JoinWithCodeDto(whiteboard.Id.IdValue, membership.Status); + } +} \ No newline at end of file diff --git a/dotnet/AipsCore/Application/Models/Whiteboard/Command/JoinWithCode/JoinWithCodeDto.cs b/dotnet/AipsCore/Application/Models/Whiteboard/Command/JoinWithCode/JoinWithCodeDto.cs new file mode 100644 index 0000000..c629e28 --- /dev/null +++ b/dotnet/AipsCore/Application/Models/Whiteboard/Command/JoinWithCode/JoinWithCodeDto.cs @@ -0,0 +1,5 @@ +using AipsCore.Domain.Models.WhiteboardMembership.Enums; + +namespace AipsCore.Application.Models.Whiteboard.Command.JoinWithCode; + +public record JoinWithCodeDto(string WhiteboardId, WhiteboardMembershipStatus Status); \ No newline at end of file diff --git a/dotnet/AipsCore/Application/Models/WhiteboardMembership/Command/CreateWhiteboardMembership/CreateWhiteboardMembershipCommand.cs b/dotnet/AipsCore/Application/Models/WhiteboardMembership/Command/CreateWhiteboardMembership/CreateWhiteboardMembershipCommand.cs deleted file mode 100644 index ebe3367..0000000 --- a/dotnet/AipsCore/Application/Models/WhiteboardMembership/Command/CreateWhiteboardMembership/CreateWhiteboardMembershipCommand.cs +++ /dev/null @@ -1,11 +0,0 @@ -using AipsCore.Application.Abstract.Command; -using AipsCore.Domain.Models.WhiteboardMembership.ValueObjects; - -namespace AipsCore.Application.Models.WhiteboardMembership.Command.CreateWhiteboardMembership; - -public record CreateWhiteboardMembershipCommand( - string WhiteboardId, - bool IsBanned, - bool EditingEnabled, - bool CanJoin) - : ICommand; \ No newline at end of file diff --git a/dotnet/AipsCore/Application/Models/WhiteboardMembership/Command/CreateWhiteboardMembership/CreateWhiteboardMembershipCommandHandler.cs b/dotnet/AipsCore/Application/Models/WhiteboardMembership/Command/CreateWhiteboardMembership/CreateWhiteboardMembershipCommandHandler.cs deleted file mode 100644 index a336e36..0000000 --- a/dotnet/AipsCore/Application/Models/WhiteboardMembership/Command/CreateWhiteboardMembership/CreateWhiteboardMembershipCommandHandler.cs +++ /dev/null @@ -1,42 +0,0 @@ -using AipsCore.Application.Abstract.Command; -using AipsCore.Application.Abstract.UserContext; -using AipsCore.Domain.Abstract; -using AipsCore.Domain.Models.WhiteboardMembership.External; -using AipsCore.Domain.Models.WhiteboardMembership.ValueObjects; - -namespace AipsCore.Application.Models.WhiteboardMembership.Command.CreateWhiteboardMembership; - -public class CreateWhiteboardMembershipCommandHandler : ICommandHandler -{ - private readonly IWhiteboardMembershipRepository _whiteboardMembershipRepository; - private readonly IUserContext _userContext; - private readonly IUnitOfWork _unitOfWork; - - public CreateWhiteboardMembershipCommandHandler( - IWhiteboardMembershipRepository whiteboardMembershipRepository, - IUserContext userContext, - IUnitOfWork unitOfWork) - { - _whiteboardMembershipRepository = whiteboardMembershipRepository; - _userContext = userContext; - _unitOfWork = unitOfWork; - } - - public async Task Handle(CreateWhiteboardMembershipCommand command, CancellationToken cancellationToken = default) - { - var userId = _userContext.GetCurrentUserId(); - - var whiteboardMembership = Domain.Models.WhiteboardMembership.WhiteboardMembership.Create( - command.WhiteboardId, - userId.IdValue, - command.IsBanned, - command.EditingEnabled, - command.CanJoin, - DateTime.UtcNow); - - await _whiteboardMembershipRepository.SaveAsync(whiteboardMembership, cancellationToken); - await _unitOfWork.SaveChangesAsync(cancellationToken); - - return whiteboardMembership.Id; - } -} \ No newline at end of file diff --git a/dotnet/AipsWebApi/Controllers/WhiteboardController.cs b/dotnet/AipsWebApi/Controllers/WhiteboardController.cs index fd418ae..109cc5c 100644 --- a/dotnet/AipsWebApi/Controllers/WhiteboardController.cs +++ b/dotnet/AipsWebApi/Controllers/WhiteboardController.cs @@ -1,10 +1,10 @@ using AipsCore.Application.Abstract; using AipsCore.Application.Models.Whiteboard.Command.CreateWhiteboard; using AipsCore.Application.Models.Whiteboard.Command.DeleteWhiteboard; +using AipsCore.Application.Models.Whiteboard.Command.JoinWithCode; using AipsCore.Application.Models.Whiteboard.Query.GetRecentWhiteboards; using AipsCore.Application.Models.Whiteboard.Query.GetWhiteboard; using AipsCore.Application.Models.Whiteboard.Query.GetWhiteboardHistory; -using AipsCore.Application.Models.WhiteboardMembership.Command.CreateWhiteboardMembership; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Whiteboard = AipsCore.Infrastructure.Persistence.Whiteboard.Whiteboard; @@ -69,7 +69,7 @@ public class WhiteboardController : ControllerBase [Authorize] [HttpPost("join")] - public async Task JoinWhiteboard(CreateWhiteboardMembershipCommand command, CancellationToken cancellationToken) + public async Task> JoinWhiteboardWithCode(JoinWithCodeCommand command, CancellationToken cancellationToken) { var result = await _dispatcher.Execute(command, cancellationToken); return Ok(result);