Files

70 lines
2.9 KiB
C#
Raw Permalink Normal View History

2020-07-19 13:04:31 +02:00
using System;
using System.Threading.Tasks;
2020-08-20 13:40:08 +02:00
using AutoMapper;
2020-12-27 12:39:25 +01:00
using Microsoft.Extensions.Logging;
2020-08-20 13:40:08 +02:00
using WotBlitzStatisticsPro.DataAccess.Model.Accounts;
2020-11-17 12:36:02 +01:00
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.OperationContext;
using WotBlitzStatisticsPro.Logic.Model;
2020-07-19 13:04:31 +02:00
using WotBlitzStatisticsPro.Logic.Pipeline;
2020-08-20 13:40:08 +02:00
using WotBlitzStatisticsPro.WgApiClient;
using WotBlitzStatisticsPro.WgApiClient.Model;
2020-07-19 13:04:31 +02:00
namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
{
2020-11-17 12:36:02 +01:00
public class GetAccountInfoOperation: IOperation<IOperationContext>
2020-07-19 13:04:31 +02:00
{
2020-08-20 13:40:08 +02:00
private readonly IWargamingApiClient _wargamingApi;
private readonly IMapper _mapper;
2020-12-27 12:39:25 +01:00
private readonly ILogger<GetAccountInfoOperation> _logger;
private readonly StatisticsCache _cache;
2020-08-20 13:40:08 +02:00
public GetAccountInfoOperation(
IWargamingApiClient wargamingApi,
2020-12-27 12:39:25 +01:00
IMapper mapper,
ILogger<GetAccountInfoOperation> logger,
StatisticsCache cache)
2020-07-19 13:04:31 +02:00
{
2020-08-20 13:40:08 +02:00
_wargamingApi = wargamingApi;
_mapper = mapper;
2020-12-27 12:39:25 +01:00
_logger = logger;
_cache = cache;
2020-08-20 13:40:08 +02:00
}
2020-12-27 12:39:25 +01:00
public async Task Invoke(IOperationContext context, Func<IOperationContext, Task>? next)
2020-08-20 13:40:08 +02:00
{
// Store info to AccountInfo and in AccountInfoHistory
var contextData = context.Get<AccountInformationPipelineContextData>();
var cache = _cache.GetAccountData(context.Request.AccountId);
if (cache != null)
{
contextData.AccountInfo = cache.AccountInfo;
contextData.AccountInfoHistory = cache.AccountInfoHistory;
_logger.LogInformation($"Account data for account {context.Request.AccountId} got from cache");
if (next != null) await next.Invoke(context);
return;
}
2020-08-20 13:40:08 +02:00
var accountInfo = await
_wargamingApi.GetPlayerAccountInfo(context.Request.AccountId, context.Request.RealmType, context.Request.RequestLanguage, context.Request.AuthenticationToken);
2020-08-20 13:40:08 +02:00
2020-12-27 12:39:25 +01:00
if (accountInfo == null)
{
_logger.LogInformation(
$"Account not found for AccountId {context.Request.AccountId} and {context.Request.RealmType} realm");
return;
}
2020-11-17 12:36:02 +01:00
contextData.AccountInfo = _mapper.Map<WotAccountInfo, AccountInfo>(accountInfo);
contextData.AccountInfo.Realm = context.Request.RealmType;
2020-08-20 13:40:08 +02:00
2020-11-17 12:36:02 +01:00
contextData.AccountInfoHistory = new AccountInfoHistory(context.Request.AccountId, contextData.AccountInfo.LastBattleTime);
2020-12-27 12:39:25 +01:00
_mapper.Map(accountInfo.Statistics?.All, contextData.AccountInfoHistory);
2020-08-20 13:40:08 +02:00
_cache.SetAccountData(context.Request.AccountId, new AccountDataCache(contextData.AccountInfo, contextData.AccountInfoHistory));
2020-12-27 12:39:25 +01:00
if (next != null) await next.Invoke(context);
2020-07-19 13:04:31 +02:00
}
}
}