using Domain.Enums; using Microsoft.Extensions.Logging; namespace Application.Game.Card.Read; public record ReadCardDetailQuery(long CardId, string Data) : IRequestWrapper; public class ReadDetailQueryHandler : RequestHandlerBase { private const string CARD_DETAILS_XPATH = "/root/card_detail"; private readonly ILogger logger; public ReadDetailQueryHandler(ICardDependencyAggregate aggregate, ILogger logger) : base(aggregate) { this.logger = logger; } public override async Task> Handle(ReadCardDetailQuery 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 queryCondition = request.Data.DeserializeCardData(); var detail = await CardDbContext.CardDetails.FirstOrDefaultAsync(cardDetail => cardDetail.CardId == request.CardId && cardDetail.Pcol1 == queryCondition.Pcol1 && cardDetail.Pcol2 == queryCondition.Pcol2 && cardDetail.Pcol3 == queryCondition.Pcol3, cancellationToken: cancellationToken); var dto = detail?.CardDetailToDto(); var result = dto?.SerializeCardData(CARD_DETAILS_XPATH) ?? new object().SerializeCardData(CARD_DETAILS_XPATH); return new ServiceResult(result); } }