1
0
mirror of synced 2024-12-21 02:35:53 +01:00
GC-local-server-rewrite/Application/Game/Card/Read/ReadAllCardDetailsQuery.cs

57 lines
2.1 KiB
C#
Raw Normal View History

using System.Diagnostics.CodeAnalysis;
using Domain.Enums;
using Microsoft.Extensions.Logging;
namespace Application.Game.Card.Read;
public record ReadAllCardDetailsQuery(long CardId) : IRequestWrapper<string>;
2023-02-16 17:38:01 +01:00
public class ReadAllDetailsQueryHandler : RequestHandlerBase<ReadAllCardDetailsQuery, string>
{
2023-02-13 17:38:19 +01:00
private const string CARD_DETAILS_XPATH = "/root/card_detail";
private const string RECORD_XPATH = $"{CARD_DETAILS_XPATH}/record";
2023-02-16 17:38:01 +01:00
private readonly ILogger<ReadAllDetailsQueryHandler> logger;
2023-02-16 17:38:01 +01:00
public ReadAllDetailsQueryHandler(ICardDependencyAggregate aggregate, ILogger<ReadAllDetailsQueryHandler> logger) : base(aggregate)
{
this.logger = logger;
}
[SuppressMessage("ReSharper.DPA", "DPA0007: Large number of DB records",
Justification = "Card details will return all records by design, which results in a large number of DB records")]
public override async Task<ServiceResult<string>> Handle(ReadAllCardDetailsQuery 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 cardDetails = await CardDbContext.CardDetails
.Where(detail => detail.CardId == request.CardId)
.ToListAsync(cancellationToken: cancellationToken);
string result;
if (cardDetails.Count == 0)
{
2023-02-13 17:38:19 +01:00
result = new object().SerializeCardData(CARD_DETAILS_XPATH);
}
else
{
var dtoList = cardDetails.Select((detail, i) =>
{
var dto = detail.CardDetailToDto();
dto.Id = i;
return dto;
});
2023-02-13 17:38:19 +01:00
result = dtoList.SerializeCardDataList(RECORD_XPATH);
}
return new ServiceResult<string>(result);
}
}