2023-02-16 09:53:02 +01:00
|
|
|
|
using Domain.Enums;
|
2023-02-13 19:47:08 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Application.Game.Card.Write;
|
|
|
|
|
|
|
|
|
|
public record WriteCardBDataCommand(long CardId, string Data) : IRequestWrapper<string>;
|
|
|
|
|
|
2023-02-16 17:38:01 +01:00
|
|
|
|
public class WriteBDataCommandHandler : RequestHandlerBase<WriteCardBDataCommand, string>
|
2023-02-13 19:47:08 +01:00
|
|
|
|
{
|
2023-02-16 17:38:01 +01:00
|
|
|
|
private readonly ILogger<WriteBDataCommandHandler> logger;
|
2023-02-13 19:47:08 +01:00
|
|
|
|
|
2023-02-16 17:38:01 +01:00
|
|
|
|
public WriteBDataCommandHandler(ICardDependencyAggregate aggregate, ILogger<WriteBDataCommandHandler> logger) : base(aggregate)
|
2023-02-13 19:47:08 +01:00
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<ServiceResult<string>> 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<string>(
|
|
|
|
|
new ServiceError($"Card id: {request.CardId} does not exist!", (int)CardReturnCode.CardNotRegistered));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dto = request.Data.DeserializeCardData<CardBDatumDto>();
|
|
|
|
|
var data = dto.DtoToCardBDatum();
|
|
|
|
|
data.CardId = request.CardId;
|
2023-02-14 19:14:19 +01:00
|
|
|
|
await CardDbContext.CardBdata.Upsert(data).RunAsync(cancellationToken);
|
2023-02-13 19:47:08 +01:00
|
|
|
|
|
|
|
|
|
await CardDbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
return new ServiceResult<string>(request.Data);
|
|
|
|
|
}
|
|
|
|
|
}
|