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);

View File

@@ -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<WhiteboardMembershipId>;

View File

@@ -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<CreateWhiteboardMembershipCommand, WhiteboardMembershipId>
{
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<WhiteboardMembershipId> 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;
}
}

View File

@@ -1,10 +1,10 @@
using AipsCore.Application.Abstract; using AipsCore.Application.Abstract;
using AipsCore.Application.Models.Whiteboard.Command.CreateWhiteboard; using AipsCore.Application.Models.Whiteboard.Command.CreateWhiteboard;
using AipsCore.Application.Models.Whiteboard.Command.DeleteWhiteboard; 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.GetRecentWhiteboards;
using AipsCore.Application.Models.Whiteboard.Query.GetWhiteboard; using AipsCore.Application.Models.Whiteboard.Query.GetWhiteboard;
using AipsCore.Application.Models.Whiteboard.Query.GetWhiteboardHistory; using AipsCore.Application.Models.Whiteboard.Query.GetWhiteboardHistory;
using AipsCore.Application.Models.WhiteboardMembership.Command.CreateWhiteboardMembership;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Whiteboard = AipsCore.Infrastructure.Persistence.Whiteboard.Whiteboard; using Whiteboard = AipsCore.Infrastructure.Persistence.Whiteboard.Whiteboard;
@@ -69,7 +69,7 @@ public class WhiteboardController : ControllerBase
[Authorize] [Authorize]
[HttpPost("join")] [HttpPost("join")]
public async Task<ActionResult> JoinWhiteboard(CreateWhiteboardMembershipCommand command, CancellationToken cancellationToken) public async Task<ActionResult<JoinWithCodeDto>> JoinWhiteboardWithCode(JoinWithCodeCommand command, CancellationToken cancellationToken)
{ {
var result = await _dispatcher.Execute(command, cancellationToken); var result = await _dispatcher.Execute(command, cancellationToken);
return Ok(result); return Ok(result);