using Application.Common.Extensions; using Application.Common.Models; using Application.Dto; using Application.Interfaces; using Application.Mappers; using Domain.Enums; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace Application.Game.Card.Write; public record WriteCardBDataCommand(long CardId, string Data) : IRequestWrapper; public class WriteCardBDataCommandHandler : CardRequestHandlerBase { private readonly ILogger logger; public WriteCardBDataCommandHandler(ICardDependencyAggregate aggregate, ILogger logger) : base(aggregate) { this.logger = logger; } public override async Task> Handle(WriteCardBDataCommand request, CancellationToken cancellationToken) { var exists = await CardDbContext.CardMains.AnyAsync(card => card.CardId == request.CardId, cancellationToken: cancellationToken); if (!exists) { logger.LogWarning("Card id: {CardId} does not exist!", request.CardId); return ServiceResult.Failed( new ServiceError($"Card id: {request.CardId} does not exist!", (int)CardReturnCode.CardNotRegistered)); } var dto = request.Data.DeserializeCardData(); var data = dto.DtoToCardBDatum(); data.CardId = request.CardId; await CardDbContext.CardBdata.Upsert(data).RunAsync(cancellationToken); await CardDbContext.SaveChangesAsync(cancellationToken); return new ServiceResult(request.Data); } }