Saving accounts To DB

This commit is contained in:
Mikhail Shabanov committed 2020-11-06 19:23:20 +01:00
1 parent 31b067e087
commit ddbf42e7bb
14 files changed
+372 -54

No files matched your search

@@ -17,7 +17,7 @@ namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
public Task Invoke(AccountInformationPipelineContext context, Func<AccountInformationPipelineContext, Task> next)
{
if (context.DbAccountInfo == null ||
context.DbAccountInfo.LastBattleTime >= context.AccountInfo.LastBattleTime)
context.DbAccountInfo.LastBattleTime < context.AccountInfo.LastBattleTime)
{
return next.Invoke(context);
}
@@ -30,10 +30,8 @@ namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
context.AccountInfo = _mapper.Map<WotAccountInfo, AccountInfo>(accountInfo);
context.AccountInfo.Realm = context.RealmType;
context.AccountInfoHistory =
_mapper.Map<WotAccountFullStatistics, AccountInfoHistory>(accountInfo.Statistics.All);
context.AccountInfoHistory.AccountId = context.AccountId;
context.AccountInfoHistory.LastBattleTime = context.AccountInfo.LastBattleTime;
context.AccountInfoHistory = new AccountInfoHistory(context.AccountId, context.AccountInfo.LastBattleTime);
_mapper.Map(accountInfo.Statistics.All, context.AccountInfoHistory);
await next.Invoke(context);
}
@@ -32,11 +32,11 @@ namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
tanksInfo.ForEach(tank =>
{
context.Tanks.Add(_mapper.Map<WotAccountTanksStatistics, TankInfo>(tank));
var stat = _mapper.Map<WotAccountTanksFullStatistics, TankInfoHistory>(tank.All);
stat.AccountId = tank.AccountId;
stat.TankId = tank.TankId;
stat.LastBattleTime = tank.LastBattleTime;
var tankInfo = new TankInfo(tank.AccountId, tank.TankId);
_mapper.Map(tank, tankInfo);
context.Tanks.Add(tankInfo);
var stat = new TankInfoHistory(tank.AccountId, tank.TankId, tank.LastBattleTime);
_mapper.Map(tank.All, stat);
context.TanksHistory[stat.TankId] = stat;
});
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using WotBlitzStatisticsPro.DataAccess;
using WotBlitzStatisticsPro.Logic.Pipeline;
namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
{
public class SaveAccountAndTanksOperation : IOperation<AccountInformationPipelineContext>
{
private readonly IWargamingAccountDataAccessor _accountDataAccessor;
private readonly ILogger<SaveAccountAndTanksOperation> _logger;
public SaveAccountAndTanksOperation(
IWargamingAccountDataAccessor accountDataAccessor,
ILogger<SaveAccountAndTanksOperation> logger)
{
_accountDataAccessor = accountDataAccessor;
_logger = logger;
}
public async Task Invoke(AccountInformationPipelineContext context, Func<AccountInformationPipelineContext, Task> next)
{
await _accountDataAccessor.AddOurUpdateAccountInfo(context.AccountInfo);
await _accountDataAccessor.AddAccountInfoHistory(context.AccountInfoHistory);
var tanksCount = 0;
var tankIdsAsString = new StringBuilder();
foreach (var tankInfo in context.Tanks)
{
var tankFromDb = await _accountDataAccessor.ReadTankInfo(tankInfo.AccountId, tankInfo.TankId);
if (tankFromDb == null || tankInfo.LastBattleTime > tankFromDb.LastBattleTime)
{
await _accountDataAccessor.AddOrUpdateTankInfo(tankInfo);
await _accountDataAccessor.AddTankInfoHistory(context.TanksHistory[tankInfo.TankId]);
tanksCount++;
tankIdsAsString.Append($" {tankInfo.TankId}; ");
}
}
_logger.LogInformation($"Updated account {context.AccountInfo.AccountId} and {tanksCount} tanks: {tankIdsAsString}");
await next.Invoke(context);
}
}
}
@@ -43,7 +43,7 @@ namespace WotBlitzStatisticsPro.Logic
.AddOperation<CheckLastBattleDateOperation>()
.AddOperation<GetTanksInfoOperation>()
.AddOperation<CalculateStatisticsOperation>()
// ToDo: add operation for save info into DB
.AddOperation<SaveAccountAndTanksOperation>()
.AddOperation<BuildAccountInfoResponseOperation>()
;
@@ -65,6 +65,7 @@ namespace WotBlitzStatisticsPro.Logic
services.AddTransient<GetTanksInfoOperation>();
services.AddTransient<CalculateStatisticsOperation>();
services.AddTransient<BuildAccountInfoResponseOperation>();
services.AddTransient<SaveAccountAndTanksOperation>();
services.AddTransient<IOperationFactory>(serviceProvider =>
new ServiceProviderOperationFactory(serviceProvider));