Join with code endpoint and command

This commit is contained in:
2026-03-04 23:10:48 +01:00
parent 409f44476f
commit 88442f22dd
6 changed files with 77 additions and 55 deletions

View File

@@ -0,0 +1,5 @@
using AipsCore.Application.Abstract.Command;
namespace AipsCore.Application.Models.Whiteboard.Command.JoinWithCode;
public record JoinWithCodeCommand(string Code): ICommand<JoinWithCodeDto>;

View File

@@ -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<JoinWithCodeCommand, JoinWithCodeDto>
{
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<JoinWithCodeDto> 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);
}
}

View File

@@ -0,0 +1,5 @@
using AipsCore.Domain.Models.WhiteboardMembership.Enums;
namespace AipsCore.Application.Models.Whiteboard.Command.JoinWithCode;
public record JoinWithCodeDto(string WhiteboardId, WhiteboardMembershipStatus Status);