feat: clan query implemented

This commit is contained in:
Mikhail Shabanov committed 2021-03-22 14:58:13 +01:00
1 parent 8fb1b170df
commit af747cf3f4
7 files changed
+97 -8

No files matched your search

@@ -91,6 +91,6 @@ namespace WotBlitzStatisticsPro.Common.Model
///<summary> ///<summary>
/// Clan info updated at /// Clan info updated at
///</summary> ///</summary>
public int? UpdatedAt { get; set; } public DateTime? UpdatedAt { get; set; }
} }
} }
@@ -3,5 +3,5 @@
/// <summary> /// <summary>
/// Represents pair of realm type a nd requested language /// Represents pair of realm type a nd requested language
/// </summary> /// </summary>
public record RealmAndLanguage (RealmType realm, RequestLanguage language); public record RealmAndLanguage (RealmType Realm, RequestLanguage Language);
} }
@@ -139,5 +139,19 @@ namespace WotBlitzStatisticsPro.DataAccess
return collection.ToDictionary(n => n.typeId, n => n.typeValue); return collection.ToDictionary(n => n.typeId, n => n.typeValue);
} }
public async Task<Dictionary<string, string>> GetClanRoles(RequestLanguage language)
{
var collection = await _database.GetCollection<ClanRoleDictionary>(ClanRolesCollectionName)
.Find(Builders<ClanRoleDictionary>.Filter.Empty)
.Project(n => new
{
ClanRoleId = n.ClanRoleId,
ClanRoleValueValue = n.ClanRoleNames.First(v => v.Language == language).Value
})
.ToListAsync();
return collection.ToDictionary(n => n.ClanRoleId, n => n.ClanRoleValueValue);
}
} }
} }
@@ -30,5 +30,7 @@ namespace WotBlitzStatisticsPro.DataAccess
Task<Dictionary<string, string>> GetNations(RequestLanguage language); Task<Dictionary<string, string>> GetNations(RequestLanguage language);
Task<Dictionary<string, string>> GetTankTypes(RequestLanguage language); Task<Dictionary<string, string>> GetTankTypes(RequestLanguage language);
Task<Dictionary<string, string>> GetClanRoles(RequestLanguage language);
} }
} }
@@ -5,6 +5,6 @@ namespace WotBlitzStatisticsPro.Logic
{ {
public interface IWargamingClans public interface IWargamingClans
{ {
Task<ClanInfoResponse> GelClanInfoByAccount(long accountId, RealmAndLanguage queryParams); Task<ClanInfoResponse?> GelClanInfoByAccount(long accountId, RealmAndLanguage queryParams);
} }
} }
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Dictionaries;
using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.DataAccess.Model;
using WotBlitzStatisticsPro.WgApiClient.Model;
using WotBlitzStatisticsPro.Logic.Calculations;
namespace WotBlitzStatisticsPro.Logic.Mappers
{
public class ClansProfile: Profile
{
public ClansProfile()
{
CreateMap<ClanAccountInfo, ClanInfoResponse>()
.ForMember(d => d.JoinedAt,
o => o.MapFrom(s => s.JoinedAt.HasValue ? s.JoinedAt.Value.ToDateTime() : new DateTime(1970, 1, 1)))
.ForMember(d => d.Role,
o => o.MapFrom((src, dest, destMember, context) =>
{
var dictionary = context.Items["roles"] as Dictionary<string, string>;
if (string.IsNullOrEmpty(src.Role) || dictionary == null)
{
return string.Empty;
}
return dictionary[src.Role];
}))
;
CreateMap<ClanInfo, ClanInfoResponse>()
.ForMember(d => d.CreatedAt,
o => o.MapFrom(s => s.CreatedAt.HasValue ? s.CreatedAt.Value.ToDateTime() : new DateTime(1970, 1, 1)))
.ForMember(d => d.UpdatedAt,
o => o.MapFrom(s => s.UpdatedAt.HasValue ? (DateTime?)s.UpdatedAt.Value.ToDateTime() : null))
;
;
}
}
}
+35 -5
View File
@@ -1,18 +1,48 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Model; using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.DataAccess;
using WotBlitzStatisticsPro.WgApiClient;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.Logic namespace WotBlitzStatisticsPro.Logic
{ {
public class WargamingClans: IWargamingClans public class WargamingClans: IWargamingClans
{ {
private const RealmType DefaultRealm = RealmType.Ru; private readonly IDictionariesDataAccessor _dictionariesDataAccessor;
private const RequestLanguage DefaultRequestLanguage = RequestLanguage.En; private readonly IWargamingApiClient _wargamingApiClient;
private readonly IMapper _mapper;
public Task<ClanInfoResponse> GelClanInfoByAccount(long accountId, RealmAndLanguage queryParams) public WargamingClans(
IDictionariesDataAccessor dictionariesDataAccessor,
IWargamingApiClient wargamingApiClient,
IMapper mapper)
{ {
// ToDo: Make Wargaming request _dictionariesDataAccessor = dictionariesDataAccessor;
return Task.FromResult(new ClanInfoResponse {ClanId = 42, Name = "Fake clan"}); _wargamingApiClient = wargamingApiClient;
_mapper = mapper;
}
public async Task<ClanInfoResponse?> GelClanInfoByAccount(long accountId, RealmAndLanguage queryParams)
{
var clanRoles = await _dictionariesDataAccessor.GetClanRoles(queryParams.Language);
var accountClanInfo =
await _wargamingApiClient.GetPlayerClanInfo(accountId, queryParams.Realm, queryParams.Language);
if (accountClanInfo?.ClanId == null)
{
return null;
}
var result = _mapper.Map<ClanAccountInfo, ClanInfoResponse>(accountClanInfo,
opt => opt.Items["roles"] = clanRoles);
var clanInfo = await _wargamingApiClient.GetClanInfo(accountClanInfo.ClanId.Value, queryParams.Realm,
queryParams.Language);
_mapper.Map(clanInfo, result);
return result;
} }
} }
} }