using Application.Common.Helpers; using Microsoft.Extensions.Logging; namespace Application.Api; public record SetPlayerNameCommand(ClientCardDto Card) : IRequestWrapper; public class SetPlayerNameCommandHandler : RequestHandlerBase { private readonly ILogger logger; public SetPlayerNameCommandHandler(ICardDependencyAggregate aggregate, ILogger logger) : base(aggregate) { this.logger = logger; } public override async Task> Handle(SetPlayerNameCommand request, CancellationToken cancellationToken) { var card = await CardDbContext.CardMains.FirstOrDefaultAsync(card => card.CardId == request.Card.CardId, cancellationToken: cancellationToken); if (card is null) { logger.LogWarning("Attempt to set name for a non existing card {CardId}", request.Card.CardId); return ServiceResult.Failed(ServiceError.UserNotFound); } card.PlayerName = request.Card.PlayerName; card.Modified = TimeHelper.CurrentTimeToString(); CardDbContext.CardMains.Update(card); var count = await CardDbContext.SaveChangesAsync(cancellationToken); return count == 1 ? new ServiceResult(true) : ServiceResult.Failed(ServiceError.DatabaseSaveFailed); } }