2023-02-12 18:46:08 +01:00
|
|
|
using Domain.Enums;
|
2023-02-13 17:38:19 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-02-12 18:46:08 +01:00
|
|
|
|
|
|
|
namespace Application.Game.Card.Read;
|
|
|
|
|
|
|
|
|
2023-02-12 19:12:26 +01:00
|
|
|
public record ReadCardBDataQuery(long CardId) : IRequestWrapper<string>;
|
2023-02-12 18:46:08 +01:00
|
|
|
|
|
|
|
public class ReadCardBDataQueryHandler : CardRequestHandlerBase<ReadCardBDataQuery, string>
|
|
|
|
{
|
2023-02-13 17:38:19 +01:00
|
|
|
private const string CARD_BDATA_XPATH = "/root/card_bdata";
|
|
|
|
|
|
|
|
private readonly ILogger<ReadCardBDataQueryHandler> logger;
|
|
|
|
|
|
|
|
public ReadCardBDataQueryHandler(ICardDependencyAggregate aggregate, ILogger<ReadCardBDataQueryHandler> logger) : base(aggregate)
|
2023-02-12 18:46:08 +01:00
|
|
|
{
|
2023-02-13 17:38:19 +01:00
|
|
|
this.logger = logger;
|
2023-02-12 18:46:08 +01:00
|
|
|
}
|
|
|
|
|
2023-02-13 17:38:19 +01:00
|
|
|
public override async Task<ServiceResult<string>> Handle(ReadCardBDataQuery request, CancellationToken cancellationToken)
|
2023-02-12 18:46:08 +01:00
|
|
|
{
|
2023-02-13 17:38:19 +01:00
|
|
|
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<string>(
|
|
|
|
new ServiceError($"Card id: {request.CardId} does not exist!", (int)CardReturnCode.CardNotRegistered));
|
|
|
|
}
|
|
|
|
|
|
|
|
var bdata = await CardDbContext.CardBdata.FirstOrDefaultAsync(
|
|
|
|
card => card.CardId == request.CardId, cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
var result = bdata?.CardBDatumToDto().SerializeCardData(CARD_BDATA_XPATH)
|
|
|
|
?? new object().SerializeCardData(CARD_BDATA_XPATH);
|
|
|
|
|
|
|
|
return new ServiceResult<string>(result);
|
2023-02-12 18:46:08 +01:00
|
|
|
}
|
|
|
|
}
|