Files

85 lines
3.0 KiB
C#
Raw Permalink Normal View History

2020-06-02 14:02:57 +02:00
using System;
2020-06-07 14:39:03 +02:00
using System.Collections.Generic;
using System.Linq;
2020-06-02 14:02:57 +02:00
using System.Threading.Tasks;
using AutoMapper;
2020-06-07 14:39:03 +02:00
using WotBlitzStatisticsPro.Common.Dictionaries;
2020-05-29 23:03:32 +02:00
using WotBlitzStatisticsPro.Common.Model;
2020-06-02 14:02:57 +02:00
using WotBlitzStatisticsPro.DataAccess;
2020-06-24 12:19:03 +02:00
using WotBlitzStatisticsPro.DataAccess.Model;
2020-06-02 14:02:57 +02:00
using WotBlitzStatisticsPro.WgApiClient;
2020-06-07 14:39:03 +02:00
using WotBlitzStatisticsPro.WgApiClient.Model;
2020-05-29 23:03:32 +02:00
namespace WotBlitzStatisticsPro.Logic.Dictionaries
{
public class VehiclesDictionaryUpdater : IDictionaryUpdater
{
2020-06-02 14:02:57 +02:00
private readonly IWargamingDictionariesApiClient _wargamingDictionariesApiClient;
private readonly IDictionariesDataAccessor _dataAccessor;
private readonly IMapper _mapper;
[Obsolete("Parameter-less constructor only for unit tests")]
public VehiclesDictionaryUpdater()
{
2020-12-27 12:39:25 +01:00
_wargamingDictionariesApiClient = null!;
_dataAccessor = null!;
_mapper = null!;
2020-06-02 14:02:57 +02:00
}
2020-06-07 14:39:03 +02:00
public VehiclesDictionaryUpdater(IWargamingDictionariesApiClient wargamingDictionariesApiClient,
IDictionariesDataAccessor dataAccessor, IMapper mapper)
2020-06-02 14:02:57 +02:00
{
_wargamingDictionariesApiClient = wargamingDictionariesApiClient;
_dataAccessor = dataAccessor;
_mapper = mapper;
}
2020-06-07 14:39:03 +02:00
public virtual async Task<UpdateDictionariesResponseItem> Update()
2020-05-29 23:03:32 +02:00
{
2020-06-07 14:39:03 +02:00
var vehiclesDictionary = await GetAndMapVehiclesDictionary();
await _dataAccessor.UpdateVehicles(vehiclesDictionary);
return new UpdateDictionariesResponseItem
{
DictionaryType = DictionaryType.Vehicles,
Description = $"Got and saved {vehiclesDictionary.Count} vehicles dictionary items"
};
2020-05-29 23:03:32 +02:00
}
2020-06-07 14:39:03 +02:00
2020-06-24 12:19:03 +02:00
private async Task<List<IVehiclesDictionary>> GetAndMapVehiclesDictionary()
2020-06-07 14:39:03 +02:00
{
var defaultRealmType = RealmType.Eu;
2020-06-24 12:19:03 +02:00
var vehicles = new List<IVehiclesDictionary>();
2020-06-07 14:39:03 +02:00
foreach (var requestLanguage in (RequestLanguage[]) Enum.GetValues(typeof(RequestLanguage)))
{
var wgVehicles =
await _wargamingDictionariesApiClient.GetVehicles(defaultRealmType, requestLanguage);
2020-12-27 12:39:25 +01:00
if (wgVehicles == null)
{
continue;
}
2020-06-07 14:39:03 +02:00
foreach (var wgVehicle in wgVehicles)
{
var mappedVehicle = vehicles.FirstOrDefault(v => v.TankId == wgVehicle.TankId);
if (mappedVehicle == null)
{
mappedVehicle = _mapper.Map<WotEncyclopediaVehiclesResponse, VehiclesDictionary>(wgVehicle);
vehicles.Add(mappedVehicle);
}
mappedVehicle.Name.Add(new LocalizableString {Language = requestLanguage, Value = wgVehicle.Name});
mappedVehicle.Description.Add(new LocalizableString {Language = requestLanguage, Value = wgVehicle.Description});
}
}
return vehicles;
}
2020-05-29 23:03:32 +02:00
}
}