Beginning ApiManager
continuous-integration/drone/push Build is passing Details

ApiManager
Emre KARTAL 2 years ago
parent 8c5a9d7b49
commit dded54568b

@ -41,7 +41,7 @@ namespace ApiLol.Controllers.v2
return BadRequest($"Champion limit exceed, max {nbTotal}");
}
IEnumerable<ChampionDto> dtos = (await _manager.ChampionsMgr.GetItems(pageRequest.index, pageRequest.count))
IEnumerable<ChampionDto> dtos = (await _manager.ChampionsMgr.GetItems(pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending))
.Select(x => x.ToDto());
return Ok(dtos);
}
@ -81,7 +81,7 @@ namespace ApiLol.Controllers.v2
dtos = (await _manager.ChampionsMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending))
.Select(x => x.ToDto());
}
return Ok(new PageResponse<ChampionDto>{ Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal });
return Ok(new PageResponse<ChampionDto> { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal });
}
catch (Exception error)
{

@ -0,0 +1,184 @@
using ApiLol.Mapper;
using DTO;
using Model;
using System.Net.Http.Json;
namespace ApiManager
{
public partial class ApiManagerData
{
public class ChampionsManager : HttpClientManager, IChampionsManager
{
private const string UrlApiChampions = "/api/v2/champions";
public ChampionsManager(HttpClient httpClient) : base(httpClient) { }
public async Task<Champion?> AddItem(Champion? item)
{
try
{
var resp = await _httpClient.PostAsJsonAsync($"{UrlApiChampions}", item.ToDto());
if (resp.IsSuccessStatusCode)
{
var createdItem = await resp.Content.ReadFromJsonAsync<ChampionDto>();
return createdItem?.ToModel();
}
else
{
return null;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error adding champion: {ex.Message}");
return null;
}
}
public async Task<bool> DeleteItem(Champion? item)
{
try
{
var resp = await _httpClient.DeleteAsync($"{UrlApiChampions}/{item?.Name}");
return resp.IsSuccessStatusCode;
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting champion: {ex.Message}");
return false;
}
}
private Func<Champion, string, bool> filterByNameContains = (champ, substring) => champ.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase);
private Func<Champion, string, bool> filterByName = (champ, substring) => champ.Name.Equals(substring, StringComparison.InvariantCultureIgnoreCase);
public async Task<IEnumerable<Champion?>> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return response.Select(c => c.ToModel()).GetItemsWithFilterAndOrdering(champ => filterByName(champ, substring), index, count, orderingPropertyName, descending);
}
public async Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
var url = $"{UrlApiChampions}?index={index}&count={count}&orderingPropertyName={orderingPropertyName}&descending={descending}";
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(url);
return response.Select(c => c.ToModel());
}
private Func<Champion, string, bool> filterByCharacteristic = (champ, charName) => champ.Characteristics.Keys.Any(k => k.Contains(charName, StringComparison.InvariantCultureIgnoreCase));
public async Task<IEnumerable<Champion?>> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return response.Select(c => c.ToModel()).GetItemsWithFilterAndOrdering(
champ => filterByCharacteristic(champ, charName),
index, count, orderingPropertyName, descending);
}
private Func<Champion, ChampionClass, bool> filterByClass = (champ, championClass) => champ.Class == championClass;
public async Task<IEnumerable<Champion?>> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return response.Select(c => c.ToModel()).GetItemsWithFilterAndOrdering(
champ => filterByClass(champ, championClass),
index, count, orderingPropertyName, descending);
}
public async Task<IEnumerable<Champion?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return response.Select(c => c.ToModel()).GetItemsWithFilterAndOrdering(champ => filterByNameContains(champ, substring), index, count, orderingPropertyName, descending);
}
public async Task<IEnumerable<Champion?>> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
private Func<Champion, Skill?, bool> filterBySkill = (champ, skill) => skill != null && champ.Skills.Contains(skill!);
private static Func<Champion, string, bool> filterBySkillSubstring = (champ, skill) => champ.Skills.Any(s => s.Name.Contains(skill, StringComparison.InvariantCultureIgnoreCase));
public async Task<IEnumerable<Champion?>> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return response.Select(c => c.ToModel()).GetItemsWithFilterAndOrdering(champ => filterBySkill(champ, skill), index, count, orderingPropertyName, descending);
}
public async Task<IEnumerable<Champion?>> GetItemsBySkill(string skillSubstring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return response.Select(c => c.ToModel()).GetItemsWithFilterAndOrdering(champ => filterBySkillSubstring(champ, skillSubstring), index, count, orderingPropertyName, descending);
}
public async Task<int> GetNbItems()
{
var response = await _httpClient.GetAsync("/countChampions");
var content = await response.Content.ReadAsStringAsync();
return int.Parse(content);
}
public async Task<int> GetNbItemsByCharacteristic(string charName)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return await response.GetNbItemsWithFilter(champ => filterByCharacteristic(champ.ToModel(), charName));
}
public async Task<int> GetNbItemsByClass(ChampionClass championClass)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return response.Where(c => c.Class.Equals(championClass))
.Count();
}
public async Task<int> GetNbItemsByName(string substring)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return response.Where(c => c.Name.Equals(substring))
.Count();
}
public Task<int> GetNbItemsByRunePage(RunePage? runePage)
{
throw new NotImplementedException();
}
public async Task<int> GetNbItemsBySkill(Skill? skill)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return await response.GetNbItemsWithFilter(champ => filterBySkill(champ.ToModel(), skill));
}
public async Task<int> GetNbItemsBySkill(string skill)
{
var response = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(UrlApiChampions);
return await response.GetNbItemsWithFilter(champ => filterBySkillSubstring(champ.ToModel(), skill));
}
public async Task<Champion?> UpdateItem(Champion? oldItem, Champion? newItem)
{
try
{
var resp = await _httpClient.PutAsJsonAsync($"{UrlApiChampions}/{oldItem?.Name}", newItem.ToDto());
if (resp.IsSuccessStatusCode)
{
var updatedItem = await resp.Content.ReadFromJsonAsync<ChampionDto>();
return updatedItem?.ToModel();
}
else
{
return null;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error updating champion: {ex.Message}");
return null;
}
}
}
}
}

@ -0,0 +1,73 @@
using Model;
namespace ApiManager
{
public partial class ApiManagerData
{
public class RunePagesManager : HttpClientManager, IRunePagesManager
{
private const string UrlApiRunePages = "/api/RunePages";
public RunePagesManager(HttpClient httpClient) : base(httpClient) { }
public Task<RunePage?> AddItem(RunePage? item)
{
throw new NotImplementedException();
}
public Task<bool> DeleteItem(RunePage? item)
{
throw new NotImplementedException();
}
public Task<IEnumerable<RunePage?>> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<RunePage?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<RunePage?>> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<RunePage?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<RunePage?>> GetItemsByRune(Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItems()
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByChampion(Champion? champion)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByName(string substring)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByRune(Rune? rune)
{
throw new NotImplementedException();
}
public Task<RunePage?> UpdateItem(RunePage? oldItem, RunePage? newItem)
{
throw new NotImplementedException();
}
}
}
}

@ -0,0 +1,63 @@
using Model;
namespace ApiManager
{
public partial class ApiManagerData
{
public class RunesManager : HttpClientManager, IRunesManager
{
private const string UrlApiRunes = "/api/runes";
public RunesManager(HttpClient httpClient) : base(httpClient) { }
public Task<Rune?> AddItem(Rune? item)
{
throw new NotImplementedException();
}
public Task<bool> DeleteItem(Rune? item)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Rune?>> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Rune?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Rune?>> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Rune?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItems()
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByFamily(RuneFamily family)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByName(string substring)
{
throw new NotImplementedException();
}
public Task<Rune?> UpdateItem(Rune? oldItem, Rune? newItem)
{
throw new NotImplementedException();
}
}
}
}

@ -0,0 +1,63 @@
using Model;
namespace ApiManager
{
public partial class ApiManagerData
{
public class SkinsManager : HttpClientManager, ISkinsManager
{
private const string UrlApiSkins = "/api/Skins";
public SkinsManager(HttpClient httpClient) : base(httpClient) { }
public Task<Skin?> AddItem(Skin? item)
{
throw new NotImplementedException();
}
public Task<bool> DeleteItem(Skin? item)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Skin?>> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Skin?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Skin?>> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Skin?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItems()
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByChampion(Champion? champion)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByName(string substring)
{
throw new NotImplementedException();
}
public Task<Skin?> UpdateItem(Skin? oldItem, Skin? newItem)
{
throw new NotImplementedException();
}
}
}
}

@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

@ -0,0 +1,24 @@
using Model;
namespace ApiManager
{
public partial class ApiManagerData : IDataManager
{
public ApiManagerData(HttpClient httpClient)
{
ChampionsMgr = new ChampionsManager(httpClient);
SkinsMgr = new SkinsManager(httpClient);
RunesMgr = new RunesManager(httpClient);
RunePagesMgr = new RunePagesManager(httpClient);
}
public IChampionsManager ChampionsMgr { get; set; }
public ISkinsManager SkinsMgr { get; set; }
public IRunesManager RunesMgr { get; set; }
public IRunePagesManager RunePagesMgr { get; set; }
}
}

@ -1,43 +0,0 @@
using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
namespace Client
{
public class ChampionHttpClient
{
private const string UrlApiChampions = "/api/v3/champions";
private readonly HttpClient _httpClient;
public ChampionHttpClient(HttpClient httpClient)
{
_httpClient = httpClient;
httpClient.BaseAddress = new Uri("https://localhost:7252");
}
public async Task<IEnumerable<ChampionDto>> GetChampion(int index, int count)
{
var url = $"{UrlApiChampions}?index={index}&count={count}";
var Response = await _httpClient.GetFromJsonAsync<PageResponse<ChampionDto>>(url);
return Response.Data;
}
/* public async void Add(ChampionDto champion)
{
await _httpClient.PostAsJsonAsync<ChampionDto>(ApiChampions, champion);
}*/
/* public async void Delete(ChampionDto champion)
{
await _httpClient.DeleteAsync(champion.Name);
}
public async void Update(ChampionDto champion)
{
await _httpClient.PutAsJsonAsync<ChampionDto>(ApiChampions, champion);
}*/
}
}

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApiManager
{
static class Extensions
{
internal static IEnumerable<T?> GetItemsWithFilterAndOrdering<T>(this IEnumerable<T> collection,
Func<T, bool> filter, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
IEnumerable<T> temp = collection;
temp = temp.Where(item => filter(item));
if (orderingPropertyName != null)
{
var prop = typeof(T).GetProperty(orderingPropertyName!);
if (prop != null)
{
temp = descending ? temp.OrderByDescending(item => prop.GetValue(item))
: temp.OrderBy(item => prop.GetValue(item));
}
}
return temp.Skip(index * count).Take(count);
}
internal static Task<int> GetNbItemsWithFilter<T>(this IEnumerable<T> collection, Func<T, bool> filter)
{
return Task.FromResult(collection.Count(item => filter(item)));
}
}
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApiManager
{
public class HttpClientManager
{
protected readonly HttpClient _httpClient;
public HttpClientManager(HttpClient httpClient)
{
_httpClient = httpClient;
_httpClient.BaseAddress = new Uri("https://localhost:7252");
}
}
}

@ -1,17 +1,16 @@
// See https://aka.ms/new-console-template for more information
using Client;
using DTO;
/*// See https://aka.ms/new-console-template for more information
*//*using static ApiManager.ApiManagerData;
Console.WriteLine("Hello, World!");
var championClient = new ChampionHttpClient(new HttpClient());
var championClient = new ChampionsManager(new HttpClient());
// Get all champions
var champions = await championClient.GetChampion(0,6);
var champions = await championClient.GetItems(0,6);
Console.WriteLine("All champions:");
foreach (var champion in champions)
{
Console.WriteLine($"{champion.Name} ({champion.Bio})");
}
}*/
/*// Add a new champion
var newChampion = new ChampionDto { Name = "Akali", Role = "Assassin" };
@ -34,6 +33,6 @@ if (championToUpdate != null)
Console.WriteLine($"{championToUpdate.Name} updated.");
}
*/
*//*
Console.ReadLine();
Console.ReadLine();*/

@ -47,9 +47,12 @@ namespace DbLib
index, count, orderingPropertyName, descending).Select(c => c.ToModel(parent.DbContext));
public async Task<IEnumerable<RunePage?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
=> parent.DbContext.RunePages.Include(rp => rp.Champions).Include(rp => rp.DictionaryCategoryRunes).GetItemsWithFilterAndOrdering(
{
parent.DbContext.Runes.Include(r => r.Image);
return parent.DbContext.RunePages.Include(rp => rp.Champions).Include(rp => rp.DictionaryCategoryRunes).Include(rp => rp.DictionaryCategoryRunes).GetItemsWithFilterAndOrdering(
rp => true,
index, count, orderingPropertyName, descending).Select(c => c.ToModel(parent.DbContext));
}
public async Task<IEnumerable<RunePage?>> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false)
=> parent.DbContext.RunePages.Include(rp => rp.Champions).Include(rp => rp.DictionaryCategoryRunes).GetItemsWithFilterAndOrdering(

@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Model;
using Model;
using MyFlib;
namespace DbLib

@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

@ -15,7 +15,7 @@ namespace DbManager.Mapper
}
foreach (var skin in championEntity.Skins)
{
champion.AddSkin(new Skin(skin.Name, champion, skin.Price, skin.Icon, skin.Image.Base64, skin.Description));
champion.AddSkin(new Skin(skin.Name, champion, skin.Price, skin.Icon, "", skin.Description));
}
if (championEntity.Characteristics != null)
{

@ -6,7 +6,7 @@ namespace DbManager.Mapper
{
public static class RuneMapper
{
public static Rune ToModel(this RuneEntity rune) => new(rune.Name, rune.Family.ToModel(), rune.Icon, rune.Image.Base64, rune.Description);
public static Rune ToModel(this RuneEntity rune) => new(rune.Name, rune.Family.ToModel(), rune.Icon, "", rune.Description);
public static RuneEntity ToEntity(this Rune rune)
=> new()
{

@ -1,4 +1,5 @@
using DbManager.Mapper.enums;
using Microsoft.EntityFrameworkCore;
using Model;
using MyFlib;
using MyFlib.Entities;

@ -1,2 +0,0 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

@ -82,6 +82,7 @@
<MauiImage Include="Resources\Images\rp.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Client\ApiManager.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\StubLib\StubLib.csproj" />

Loading…
Cancel
Save