Static dictionaries getter and mapper

This commit is contained in:
Mikhail Shabanov committed 2020-05-30 17:15:43 +02:00
1 parent 32f7245d85
commit 36be21e61c
7 files changed
+267 -9

No files matched your search

@@ -0,0 +1,27 @@
using System.Collections.Generic;
using WotBlitzStatisticsPro.Common.Model;
namespace WotBlitzStatisticsPro.Common.Dictionaries
{
/// <summary>
/// Achievement section dictionary item.
/// </summary>
public class AchievementSectionDictionary
{
/// <summary>
/// Achievement section id
/// </summary>
public string AchievementSectionId { get; set; }
/// <summary>
/// Section order
/// </summary>
public int Order { get; set; }
/// <summary>
/// Localized section names
/// </summary>
public Dictionary<RequestLanguage, string> AchievementSectionNames { get; set; }
}
}
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using WotBlitzStatisticsPro.Common.Model;
namespace WotBlitzStatisticsPro.Common.Dictionaries
{
/// <summary>
/// Clan role dictionary item
/// </summary>
public class ClanRoleDictionary
{
/// <summary>
/// Clan role id
/// </summary>
public string ClanRoleId { get; set; }
/// <summary>
/// Localized clan role names
/// </summary>
public Dictionary<RequestLanguage, string> ClanRoleNames { get; set; }
}
}
@@ -0,0 +1,19 @@
namespace WotBlitzStatisticsPro.Common.Dictionaries
{
/// <summary>
/// Languages available
/// </summary>
public class LanguageDictionary
{
/// <summary>
/// Language id
/// </summary>
public string LanguageId { get; set; }
/// <summary>
/// Language
/// </summary>
public string LanguageName { get; set; }
}
}
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using WotBlitzStatisticsPro.Common.Model;
namespace WotBlitzStatisticsPro.Common.Dictionaries
{
/// <summary>
/// Tanks nations dictionary item
/// </summary>
public class NationDictionary
{
/// <summary>
/// Nation id
/// </summary>
public string NationId { get; set; }
/// <summary>
/// Nation
/// </summary>
public Dictionary<RequestLanguage, string> NationNames { get; set; }
}
}
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using WotBlitzStatisticsPro.Common.Model;
namespace WotBlitzStatisticsPro.Common.Dictionaries
{
/// <summary>
/// Vehicle type dictionary item
/// </summary>
public class VehicleTypeDictionary
{
/// <summary>
/// Vehicle type id
/// </summary>
public string VehicleTypeId { get; set; }
/// <summary>
/// Vehicle type localized name
/// </summary>
public Dictionary<RequestLanguage, string> VehicleTypeNames { get; set; }
}
}
@@ -1,32 +1,164 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Dictionaries;
using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.WgApiClient;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.Logic.Dictionaries
{
public class StaticDictionariesUpdater : IDictionaryUpdater
{
private readonly IWargamingDictionariesApiClient _wargamingDictionariesApiClient;
private readonly IMapper _mapper;
public StaticDictionariesUpdater(IWargamingDictionariesApiClient wargamingDictionariesApiClient)
public StaticDictionariesUpdater(
IWargamingDictionariesApiClient wargamingDictionariesApiClient,
IMapper mapper)
{
_wargamingDictionariesApiClient = wargamingDictionariesApiClient;
_mapper = mapper;
}
public async Task<UpdateDictionariesResponseItem> Update()
{
var defaultRealmType = RealmType.Eu;
foreach (var requestLanguage in (RequestLanguage[]) Enum.GetValues(typeof(RequestLanguage)))
{
var (WotEncyclopediaInfoResponse, WotClanMembersDictionaryResponse) =
await _wargamingDictionariesApiClient.GetStaticDictionariesAsync(defaultRealmType, requestLanguage);
// ToDo: map
// ToDo: add to dictionary
}
var (
languages,
nations,
vehicleTypes,
clanRoles,
achievementsSections) = await GetAndMapDictionaries();
// ToDo: Save dictionary to Mongo
// ToDo: create unit tests
throw new System.NotImplementedException();
}
private async Task<(
List<LanguageDictionary>,
List<NationDictionary>,
List<VehicleTypeDictionary>,
List<ClanRoleDictionary>,
List<AchievementSectionDictionary>)> GetAndMapDictionaries()
{
var defaultRealmType = RealmType.Eu;
List<LanguageDictionary> languages = null;
var nations = new List<NationDictionary>();
var vehicleTypes = new List<VehicleTypeDictionary>();
var clanRoles = new List<ClanRoleDictionary>();
var achievementsSections = new List<AchievementSectionDictionary>();
foreach (var requestLanguage in (RequestLanguage[])Enum.GetValues(typeof(RequestLanguage)))
{
var (wotEncyclopediaInfoResponse, wotClanMembersDictionaryResponse) =
await _wargamingDictionariesApiClient.GetStaticDictionariesAsync(defaultRealmType, requestLanguage);
if (languages == null)
{
languages =
_mapper.Map<Dictionary<string, string>, List<LanguageDictionary>>(wotEncyclopediaInfoResponse
.Languages);
}
MapVehicleNations(requestLanguage, wotEncyclopediaInfoResponse.VehicleNations, nations);
MapVehicleTypes(requestLanguage, wotEncyclopediaInfoResponse.VehicleTypes, vehicleTypes);
MapClanRoles(requestLanguage, wotClanMembersDictionaryResponse.ClanRoles, clanRoles);
MapAchievementsSections(requestLanguage, wotEncyclopediaInfoResponse.AchievementSections, achievementsSections);
}
return (languages, nations, vehicleTypes, clanRoles, achievementsSections);
}
private void MapVehicleNations(
RequestLanguage requestLanguage,
Dictionary<string, string> sourceDictionary,
List<NationDictionary> destinationDictionary)
{
foreach (var source in sourceDictionary)
{
var destinationItem = destinationDictionary.FirstOrDefault(d => d.NationId == source.Key);
if (destinationItem == null)
{
destinationItem = new NationDictionary
{
NationId = source.Key, NationNames = new Dictionary<RequestLanguage, string>()
};
destinationDictionary.Add(destinationItem);
}
destinationItem.NationNames[requestLanguage] = source.Value;
}
}
private void MapVehicleTypes(
RequestLanguage requestLanguage,
Dictionary<string, string> sourceDictionary,
List<VehicleTypeDictionary> destinationDictionary)
{
foreach (var source in sourceDictionary)
{
var destinationItem = destinationDictionary.FirstOrDefault(d => d.VehicleTypeId == source.Key);
if (destinationItem == null)
{
destinationItem = new VehicleTypeDictionary
{
VehicleTypeId = source.Key, VehicleTypeNames = new Dictionary<RequestLanguage, string>()
};
destinationDictionary.Add(destinationItem);
}
destinationItem.VehicleTypeNames[requestLanguage] = source.Value;
}
}
private void MapClanRoles(
RequestLanguage requestLanguage,
Dictionary<string, string> sourceDictionary,
List<ClanRoleDictionary> destinationDictionary)
{
foreach (var source in sourceDictionary)
{
var destinationItem = destinationDictionary.FirstOrDefault(d => d.ClanRoleId == source.Key);
if (destinationItem == null)
{
destinationItem = new ClanRoleDictionary
{
ClanRoleId = source.Key, ClanRoleNames = new Dictionary<RequestLanguage, string>()
};
destinationDictionary.Add(destinationItem);
}
destinationItem.ClanRoleNames[requestLanguage] = source.Value;
}
}
private void MapAchievementsSections(
RequestLanguage requestLanguage,
Dictionary<string, WotEncyclopediaInfoAchievementSection> sourceSections,
List<AchievementSectionDictionary> destinationDictionary)
{
foreach (var section in sourceSections)
{
var destinationItem =
destinationDictionary.FirstOrDefault(d => d.AchievementSectionId == section.Key);
if (destinationItem == null)
{
destinationItem = new AchievementSectionDictionary
{
AchievementSectionId = section.Key,
Order = Convert.ToInt32(section.Value.Order),
AchievementSectionNames = new Dictionary<RequestLanguage, string>()
};
destinationDictionary.Add(destinationItem);
}
destinationItem.AchievementSectionNames[requestLanguage] = section.Value.Name;
}
}
}
}
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Dictionaries;
namespace WotBlitzStatisticsPro.Logic.Mappers
{
public class DictionaryLanguageMapper : Profile
{
public DictionaryLanguageMapper()
{
CreateMap<KeyValuePair<string, string>, LanguageDictionary>()
.ForMember(dest => dest.LanguageId, op => op.MapFrom(s => s.Key))
.ForMember(dest => dest.LanguageName, op => op.MapFrom(svm => svm.Value));
}
}
}