Files
WotBlitzStatisticsBlazor/WotBlitzStatisticsPro.Logic/AccountInformationPipeline/Operations/BuildAccountInfoResponseOperation.cs
T

103 lines
4.4 KiB
C#
Raw Normal View History

2020-09-30 19:30:36 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
2020-12-27 12:39:25 +01:00
using Microsoft.Extensions.Logging;
2020-09-30 19:30:36 +02:00
using WotBlitzStatisticsPro.Common.Dictionaries;
using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.DataAccess;
using WotBlitzStatisticsPro.DataAccess.Model;
using WotBlitzStatisticsPro.DataAccess.Model.Accounts;
2020-11-17 12:36:02 +01:00
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.OperationContext;
2020-09-30 19:30:36 +02:00
using WotBlitzStatisticsPro.Logic.Pipeline;
namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
{
2020-11-17 12:36:02 +01:00
public class BuildAccountInfoResponseOperation : IOperation<IOperationContext>
2020-09-30 19:30:36 +02:00
{
private readonly IMapper _mapper;
private readonly IDictionariesDataAccessor _dictionariesDataAccessor;
2020-12-27 12:39:25 +01:00
private readonly ILogger<BuildAccountInfoResponseOperation> _logger;
2020-09-30 19:30:36 +02:00
2020-12-27 12:39:25 +01:00
public BuildAccountInfoResponseOperation(
IMapper mapper,
IDictionariesDataAccessor dictionariesDataAccessor,
ILogger<BuildAccountInfoResponseOperation> logger)
2020-09-30 19:30:36 +02:00
{
_mapper = mapper;
_dictionariesDataAccessor = dictionariesDataAccessor;
2020-12-27 12:39:25 +01:00
_logger = logger;
2020-09-30 19:30:36 +02:00
}
2020-12-27 12:39:25 +01:00
public async Task Invoke(IOperationContext context, Func<IOperationContext, Task>? next)
2020-09-30 19:30:36 +02:00
{
2020-11-17 12:36:02 +01:00
var contextData = context.Get<AccountInformationPipelineContextData>();
2020-09-30 19:30:36 +02:00
2020-12-27 12:39:25 +01:00
if (contextData?.AccountInfo == null
|| contextData?.Tanks == null
|| contextData?.TanksHistory == null)
{
_logger.LogInformation(
$"contextData or AccountInfo or Tanks or TanksHistory is null");
return;
}
2020-11-17 12:36:02 +01:00
contextData.Response = _mapper.Map<AccountInfo, AccountInfoResponse>(contextData.AccountInfo);
contextData.Response = _mapper.Map(contextData.AccountInfoHistory, contextData.Response);
contextData.Response.RegionAndLanguage =
new RealmAndLanguage(context.Request.RealmType, context.Request.RequestLanguage);
2020-11-17 12:36:02 +01:00
var tankIds = contextData.Tanks.Select(t => t.TankId).ToArray();
2020-09-30 19:30:36 +02:00
var vehicles = await _dictionariesDataAccessor.GetVehicles(tankIds);
2020-11-17 12:36:02 +01:00
var nations = await _dictionariesDataAccessor.GetNations(context.Request.RequestLanguage);
var tankTypes = await _dictionariesDataAccessor.GetTankTypes(context.Request.RequestLanguage);
2020-09-30 19:30:36 +02:00
2020-11-17 12:36:02 +01:00
contextData.Response.Tanks = _mapper.Map<List<TankInfo>, List<TankInfoResponse>>(contextData.Tanks);
contextData.Response.Tanks.ForEach(t =>
2020-09-30 19:30:36 +02:00
{
2020-11-17 12:36:02 +01:00
t = _mapper.Map(contextData.TanksHistory[t.TankId], t);
2020-09-30 19:30:36 +02:00
var vehicle = vehicles.ContainsKey(t.TankId) ? vehicles[t.TankId] : CreateStubVehicle();
t = _mapper.Map(vehicle, t,
opt =>
{
2020-11-17 12:36:02 +01:00
opt.Items["language"] = context.Request.RequestLanguage;
2020-09-30 19:30:36 +02:00
opt.AfterMap((src, dest) =>
{
2020-12-27 12:39:25 +01:00
if(dest?.TankTypeId != null)
{
dest.TankType = tankTypes.ContainsKey(dest.TankTypeId)
? tankTypes[dest.TankTypeId]
: "-";
}
if(dest?.TankNationId != null)
{
dest.TankNation = nations.ContainsKey(dest.TankNationId)
? nations[dest.TankNationId]
: "-";
}
2020-09-30 19:30:36 +02:00
});
});
});
2020-12-27 12:39:25 +01:00
if (next != null) await next.Invoke(context);
2020-09-30 19:30:36 +02:00
}
private static IVehiclesDictionary CreateStubVehicle()
{
return new VehiclesDictionary
{
Name = new List<LocalizableString>()
{
new LocalizableString
{Language = RequestLanguage.En, Value = "[Vehicle is not provided in the dictionary]"},
new LocalizableString {Language = RequestLanguage.Ru, Value = "[Танк отсутствует в танкопедии]"},
new LocalizableString
{Language = RequestLanguage.De, Value = "[Tank ist nicht im Wörterbuch enthalten]"},
}
};
}
}
}