From f66fc1a48db303106959768df1a7c54b5ef31750 Mon Sep 17 00:00:00 2001 From: Emre Date: Sun, 26 Mar 2023 18:31:30 +0200 Subject: [PATCH] completion of the ApiManager :white_check_mark: --- .../Sources/ApiLol/ApiLol.csproj | 1 + .../ApiLol/Controllers/RunePagesController.cs | 2 +- .../ApiLol/Controllers/RunesController.cs | 2 +- .../ApiLol/Controllers/SkinsController.cs | 2 +- .../Controllers/v1/ChampionsController.cs | 2 +- .../Controllers/v2/ChampionsController.cs | 3 +- .../Sources/ApiMapping/ApiMapping.csproj | 14 ++ .../Mapper => ApiMapping}/ChampionMapper.cs | 82 ++++----- .../Mapper => ApiMapping}/LargeImageMapper.cs | 2 +- .../Mapper => ApiMapping}/RuneMapper.cs | 45 ++--- .../Mapper => ApiMapping}/RunePageMapper.cs | 5 +- .../Mapper => ApiMapping}/SkillMapper.cs | 40 ++--- .../Mapper => ApiMapping}/SkinMapper.cs | 5 +- .../enums/CategoryMapper.cs | 2 +- .../enums/ChampionClassMapper.cs | 106 ++++++------ .../enums/RuneFamilyMapper.cs | 2 +- .../enums/SkillTypeMapper.cs | 2 +- .../Sources/Client/ApiManager.Champions.cs | 2 +- .../Sources/Client/ApiManager.RunePages.cs | 116 ++++++++++--- .../Sources/Client/ApiManager.Runes.cs | 117 ++++++++++--- .../Sources/Client/ApiManager.Skins.cs | 159 +++++++++++++++--- .../Sources/Client/ApiManager.csproj | 3 +- .../Sources/Client/Program.cs | 6 +- .../Sources/LeagueOfLegends.sln | 6 + .../Sources/LolApp/LolApp.csproj | 1 - .../Sources/LolApp/MauiProgram.cs | 74 ++++---- 26 files changed, 543 insertions(+), 258 deletions(-) create mode 100644 src/EntityFramework_LoL/Sources/ApiMapping/ApiMapping.csproj rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/ChampionMapper.cs (94%) rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/LargeImageMapper.cs (92%) rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/RuneMapper.cs (87%) rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/RunePageMapper.cs (94%) rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/SkillMapper.cs (84%) rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/SkinMapper.cs (95%) rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/enums/CategoryMapper.cs (98%) rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/enums/ChampionClassMapper.cs (96%) rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/enums/RuneFamilyMapper.cs (97%) rename src/EntityFramework_LoL/Sources/{ApiLol/Mapper => ApiMapping}/enums/SkillTypeMapper.cs (97%) diff --git a/src/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj b/src/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj index d01f0d6..b63bbf1 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj +++ b/src/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj @@ -17,6 +17,7 @@ + diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunePagesController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunePagesController.cs index 426cedd..b52a0c7 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunePagesController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunePagesController.cs @@ -1,4 +1,4 @@ -using ApiLol.Mapper; +using ApiMapping; using DTO; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs index 8d7a2e4..8fba7ed 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs @@ -1,4 +1,4 @@ -using ApiLol.Mapper; +using ApiMapping; using DTO; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs index eb9192a..537923b 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs @@ -1,4 +1,4 @@ -using ApiLol.Mapper; +using ApiMapping; using DTO; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs index 2125c70..f33ee8d 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs @@ -1,4 +1,4 @@ -using ApiLol.Mapper; +using ApiMapping; using DTO; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs index e494e6e..161a112 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs @@ -1,5 +1,4 @@ -using ApiLol.Mapper; -using Azure.Core; +using ApiMapping; using DTO; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; diff --git a/src/EntityFramework_LoL/Sources/ApiMapping/ApiMapping.csproj b/src/EntityFramework_LoL/Sources/ApiMapping/ApiMapping.csproj new file mode 100644 index 0000000..65895ab --- /dev/null +++ b/src/EntityFramework_LoL/Sources/ApiMapping/ApiMapping.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/ChampionMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/ChampionMapper.cs similarity index 94% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/ChampionMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/ChampionMapper.cs index 9b48da4..524dfcc 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/ChampionMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/ChampionMapper.cs @@ -1,40 +1,42 @@ -using DTO; -using Model; - -namespace ApiLol.Mapper -{ - public static class ChampionMapper - { - public static ChampionDto ToDto(this Champion champion) - => new() - { - Name = champion.Name, - Bio = champion.Bio, - Class = champion.Class.ToDto(), - Icon = champion.Icon, - Image = champion.Image.ToDto(), - Skins = champion.Skins.Select(e => e.ToDto()), - Skills = champion.Skills.Select(e => e.ToDto()), - Characteristics = champion.Characteristics.ToDictionary(c => c.Key, c => c.Value) - - }; - - - public static Champion ToModel(this ChampionDto championDto) - { - var champ = new Champion(championDto.Name, championDto.Class.ToModel(), championDto.Icon, championDto.Image.Base64, championDto.Bio); - foreach (var skin in championDto.Skins) - { - champ.AddSkin(skin.ToModel(champ)); - } - foreach (var skill in championDto.Skills) - { - champ.AddSkill(skill.ToModel()); - } - if (championDto.Characteristics != null) - champ.AddCharacteristics(championDto.Characteristics.Select(c => Tuple.Create(c.Key, c.Value)).ToArray()); - return champ; - } - - } -} +using ApiMapping; +using ApiMapping.enums; +using DTO; +using Model; + +namespace ApiMapping +{ + public static class ChampionMapper + { + public static ChampionDto ToDto(this Champion champion) + => new() + { + Name = champion.Name, + Bio = champion.Bio, + Class = champion.Class.ToDto(), + Icon = champion.Icon, + Image = champion.Image.ToDto(), + Skins = champion.Skins.Select(e => e.ToDto()), + Skills = champion.Skills.Select(e => e.ToDto()), + Characteristics = champion.Characteristics.ToDictionary(c => c.Key, c => c.Value) + + }; + + + public static Champion ToModel(this ChampionDto championDto) + { + var champ = new Champion(championDto.Name, championDto.Class.ToModel(), championDto.Icon, championDto.Image.Base64, championDto.Bio); + foreach (var skin in championDto.Skins) + { + champ.AddSkin(skin.ToModel(champ)); + } + foreach (var skill in championDto.Skills) + { + champ.AddSkill(skill.ToModel()); + } + if (championDto.Characteristics != null) + champ.AddCharacteristics(championDto.Characteristics.Select(c => Tuple.Create(c.Key, c.Value)).ToArray()); + return champ; + } + + } +} diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/LargeImageMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/LargeImageMapper.cs similarity index 92% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/LargeImageMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/LargeImageMapper.cs index a0b8756..44ee496 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/LargeImageMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/LargeImageMapper.cs @@ -1,7 +1,7 @@ using DTO; using Model; -namespace ApiLol.Mapper +namespace ApiMapping { public static class LargeImageMapper { diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/RuneMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/RuneMapper.cs similarity index 87% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/RuneMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/RuneMapper.cs index b11f1aa..8014276 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/RuneMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/RuneMapper.cs @@ -1,22 +1,23 @@ -using ApiLol.Mapper.enums; -using DTO; -using Model; - -namespace ApiLol.Mapper -{ - public static class RuneMapper - { - public static RuneDto ToDto(this Rune rune) - => new() - { - Name = rune.Name, - Description = rune.Description, - Family = rune.Family.ToDto(), - Icon = rune.Icon, - Image = rune.Image.ToDto() - }; - - public static Rune ToModel(this RuneDto rune) => new(rune.Name, rune.Family.ToModel(), rune.Icon, rune.Image.Base64, rune.Description); - - } -} +using ApiMapping; +using ApiMapping.enums; +using DTO; +using Model; + +namespace ApiMapping +{ + public static class RuneMapper + { + public static RuneDto ToDto(this Rune rune) + => new() + { + Name = rune.Name, + Description = rune.Description, + Family = rune.Family.ToDto(), + Icon = rune.Icon, + Image = rune.Image.ToDto() + }; + + public static Rune ToModel(this RuneDto rune) => new(rune.Name, rune.Family.ToModel(), rune.Icon, rune.Image.Base64, rune.Description); + + } +} diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/RunePageMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/RunePageMapper.cs similarity index 94% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/RunePageMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/RunePageMapper.cs index 6ea2bd8..b54b126 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/RunePageMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/RunePageMapper.cs @@ -1,9 +1,8 @@ -using ApiLol.Mapper.enums; -using DTO; +using DTO; using Model; using static Model.RunePage; -namespace ApiLol.Mapper +namespace ApiMapping { public static class RunePageMapper { diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/SkillMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/SkillMapper.cs similarity index 84% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/SkillMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/SkillMapper.cs index 27bb55b..1eee13c 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/SkillMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/SkillMapper.cs @@ -1,19 +1,21 @@ -using DTO; -using Model; - -namespace ApiLol.Mapper -{ - public static class SkillMapper - { - public static SkillDto ToDto(this Skill skill) - => new() - { - Name = skill.Name, - Description = skill.Description, - Type = skill.Type.ToDto() - }; - - public static Skill ToModel(this SkillDto skillDto) => new(skillDto.Name, skillDto.Type.ToModel(), skillDto.Description); - - } -} +using ApiMapping; +using ApiMapping.enums; +using DTO; +using Model; + +namespace ApiMapping +{ + public static class SkillMapper + { + public static SkillDto ToDto(this Skill skill) + => new() + { + Name = skill.Name, + Description = skill.Description, + Type = skill.Type.ToDto() + }; + + public static Skill ToModel(this SkillDto skillDto) => new(skillDto.Name, skillDto.Type.ToModel(), skillDto.Description); + + } +} diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/SkinMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/SkinMapper.cs similarity index 95% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/SkinMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/SkinMapper.cs index d4d7221..16249c3 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/SkinMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/SkinMapper.cs @@ -1,7 +1,8 @@ -using DTO; +using ApiMapping; +using DTO; using Model; -namespace ApiLol.Mapper +namespace ApiMapping { public static class SkinMapper { diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/CategoryMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/enums/CategoryMapper.cs similarity index 98% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/CategoryMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/enums/CategoryMapper.cs index 55dfe9f..10a5c8d 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/CategoryMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/enums/CategoryMapper.cs @@ -1,7 +1,7 @@ using DTO; using Model; -namespace ApiLol.Mapper.enums +namespace ApiMapping.enums { public static class CategoryMapper { diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/ChampionClassMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/enums/ChampionClassMapper.cs similarity index 96% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/ChampionClassMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/enums/ChampionClassMapper.cs index a61f12f..3ac6636 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/ChampionClassMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/enums/ChampionClassMapper.cs @@ -1,53 +1,53 @@ -using DTO; -using Model; - -namespace ApiLol.Mapper -{ - public static class ChampionClassMapper - { - public static ChampionClassDto ToDto(this ChampionClass championClass) - { - switch (championClass) - { - case ChampionClass.Unknown: - return ChampionClassDto.Unknown; - case ChampionClass.Assassin: - return ChampionClassDto.Assassin; - case ChampionClass.Fighter: - return ChampionClassDto.Fighter; - case ChampionClass.Mage: - return ChampionClassDto.Mage; - case ChampionClass.Marksman: - return ChampionClassDto.Marksman; - case ChampionClass.Support: - return ChampionClassDto.Support; - case ChampionClass.Tank: - return ChampionClassDto.Tank; - default: - return ChampionClassDto.Unknown; - } - } - public static ChampionClass ToModel(this ChampionClassDto championClass) - { - switch (championClass) - { - case ChampionClassDto.Unknown: - return ChampionClass.Unknown; - case ChampionClassDto.Assassin: - return ChampionClass.Assassin; - case ChampionClassDto.Fighter: - return ChampionClass.Fighter; - case ChampionClassDto.Mage: - return ChampionClass.Mage; - case ChampionClassDto.Marksman: - return ChampionClass.Marksman; - case ChampionClassDto.Support: - return ChampionClass.Support; - case ChampionClassDto.Tank: - return ChampionClass.Tank; - default: - return ChampionClass.Unknown; - } - } - } -} +using DTO; +using Model; + +namespace ApiMapping.enums +{ + public static class ChampionClassMapper + { + public static ChampionClassDto ToDto(this ChampionClass championClass) + { + switch (championClass) + { + case ChampionClass.Unknown: + return ChampionClassDto.Unknown; + case ChampionClass.Assassin: + return ChampionClassDto.Assassin; + case ChampionClass.Fighter: + return ChampionClassDto.Fighter; + case ChampionClass.Mage: + return ChampionClassDto.Mage; + case ChampionClass.Marksman: + return ChampionClassDto.Marksman; + case ChampionClass.Support: + return ChampionClassDto.Support; + case ChampionClass.Tank: + return ChampionClassDto.Tank; + default: + return ChampionClassDto.Unknown; + } + } + public static ChampionClass ToModel(this ChampionClassDto championClass) + { + switch (championClass) + { + case ChampionClassDto.Unknown: + return ChampionClass.Unknown; + case ChampionClassDto.Assassin: + return ChampionClass.Assassin; + case ChampionClassDto.Fighter: + return ChampionClass.Fighter; + case ChampionClassDto.Mage: + return ChampionClass.Mage; + case ChampionClassDto.Marksman: + return ChampionClass.Marksman; + case ChampionClassDto.Support: + return ChampionClass.Support; + case ChampionClassDto.Tank: + return ChampionClass.Tank; + default: + return ChampionClass.Unknown; + } + } + } +} diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/RuneFamilyMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/enums/RuneFamilyMapper.cs similarity index 97% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/RuneFamilyMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/enums/RuneFamilyMapper.cs index 00f550e..1876fc8 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/RuneFamilyMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/enums/RuneFamilyMapper.cs @@ -2,7 +2,7 @@ using DTO.enums; using Model; -namespace ApiLol.Mapper.enums +namespace ApiMapping.enums { public static class RuneFamilyMapper { diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/SkillTypeMapper.cs b/src/EntityFramework_LoL/Sources/ApiMapping/enums/SkillTypeMapper.cs similarity index 97% rename from src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/SkillTypeMapper.cs rename to src/EntityFramework_LoL/Sources/ApiMapping/enums/SkillTypeMapper.cs index c21be45..810f031 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/SkillTypeMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiMapping/enums/SkillTypeMapper.cs @@ -1,7 +1,7 @@ using DTO; using Model; -namespace ApiLol.Mapper +namespace ApiMapping.enums { public static class SkillTypeMapper { diff --git a/src/EntityFramework_LoL/Sources/Client/ApiManager.Champions.cs b/src/EntityFramework_LoL/Sources/Client/ApiManager.Champions.cs index 06a50b5..8e2b469 100644 --- a/src/EntityFramework_LoL/Sources/Client/ApiManager.Champions.cs +++ b/src/EntityFramework_LoL/Sources/Client/ApiManager.Champions.cs @@ -1,4 +1,4 @@ -using ApiLol.Mapper; +using ApiMapping; using DTO; using Model; using System.Net.Http.Json; diff --git a/src/EntityFramework_LoL/Sources/Client/ApiManager.RunePages.cs b/src/EntityFramework_LoL/Sources/Client/ApiManager.RunePages.cs index 699353f..a7aa88d 100644 --- a/src/EntityFramework_LoL/Sources/Client/ApiManager.RunePages.cs +++ b/src/EntityFramework_LoL/Sources/Client/ApiManager.RunePages.cs @@ -1,4 +1,8 @@ -using Model; +using DTO; +using Model; +using System.Net.Http.Json; +using ApiMapping; +using System.Data.SqlTypes; namespace ApiManager { @@ -9,44 +13,91 @@ namespace ApiManager private const string UrlApiRunePages = "/api/RunePages"; public RunePagesManager(HttpClient httpClient) : base(httpClient) { } - public Task AddItem(RunePage? item) + public async Task AddItem(RunePage? item) { - throw new NotImplementedException(); + try + { + var resp = await _httpClient.PostAsJsonAsync($"{UrlApiRunePages}", item.ToDto()); + if (resp.IsSuccessStatusCode) + { + var createdItem = await resp.Content.ReadFromJsonAsync(); + return createdItem?.ToModel(); + } + else + { + return null; + } + } + catch (Exception ex) + { + Console.WriteLine($"Error adding runePage: {ex.Message}"); + return null; + } } - public Task DeleteItem(RunePage? item) + public async Task DeleteItem(RunePage? item) { - throw new NotImplementedException(); + try + { + var resp = await _httpClient.DeleteAsync($"{UrlApiRunePages}/{item?.Name}"); + return resp.IsSuccessStatusCode; + } + catch (Exception ex) + { + Console.WriteLine($"Error deleting runePage: {ex.Message}"); + return false; + } } - public Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + private static Func filterByName + = (rp, substring) => rp.Name.Equals(substring, StringComparison.InvariantCultureIgnoreCase); + + private static Func filterByNameContains + = (rp, substring) => rp.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase); + + private static Func filterByRune + = (rp, rune) => rune != null && rp.Runes.Values.Contains(rune!); + + public async Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var runePages = await _httpClient.GetFromJsonAsync>(UrlApiRunePages); + return runePages.Data.Select(r => r.ToModel()).GetItemsWithFilterAndOrdering( + rp => filterByName(rp, substring), + index, count, orderingPropertyName, descending); } - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var runePages = await _httpClient.GetFromJsonAsync>($"{UrlApiRunePages}?&index={index}&count={count}&descending={descending}"); + return runePages.Data.Select(c => c.ToModel()); } - public Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) { throw new NotImplementedException(); } - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var runePages = await _httpClient.GetFromJsonAsync>(UrlApiRunePages); + return runePages.Data.Select(r => r.ToModel()).GetItemsWithFilterAndOrdering( + rp => filterByNameContains(rp, substring), + index, count, orderingPropertyName, descending); } - public Task> GetItemsByRune(Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemsByRune(Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var runePages = await _httpClient.GetFromJsonAsync>(UrlApiRunePages); + return runePages.Data.Select(r => r.ToModel()).GetItemsWithFilterAndOrdering( + rp => filterByRune(rp, rune), + index, count, orderingPropertyName, descending); } - public Task GetNbItems() + public async Task GetNbItems() { - throw new NotImplementedException(); + var response = await _httpClient.GetAsync("/countRunePages"); + var content = await response.Content.ReadAsStringAsync(); + return int.Parse(content); } public Task GetNbItemsByChampion(Champion? champion) @@ -54,19 +105,40 @@ namespace ApiManager throw new NotImplementedException(); } - public Task GetNbItemsByName(string substring) + public async Task GetNbItemsByName(string substring) { - throw new NotImplementedException(); + var runePages = await _httpClient.GetFromJsonAsync>(UrlApiRunePages); + return await runePages.Data.Select(r => r.ToModel()).GetNbItemsWithFilter( + rp => filterByName(rp, substring)); } - public Task GetNbItemsByRune(Rune? rune) + public async Task GetNbItemsByRune(Rune? rune) { - throw new NotImplementedException(); + var runePages = await _httpClient.GetFromJsonAsync>(UrlApiRunePages); + return await runePages.Data.Select(r => r.ToModel()).GetNbItemsWithFilter( + rp => filterByRune(rp, rune)); } - public Task UpdateItem(RunePage? oldItem, RunePage? newItem) + public async Task UpdateItem(RunePage? oldItem, RunePage? newItem) { - throw new NotImplementedException(); + try + { + var resp = await _httpClient.PutAsJsonAsync($"{UrlApiRunePages}/{oldItem?.Name}", newItem.ToDto()); + if (resp.IsSuccessStatusCode) + { + var updatedItem = await resp.Content.ReadFromJsonAsync(); + return updatedItem?.ToModel(); + } + else + { + return null; + } + } + catch (Exception ex) + { + Console.WriteLine($"Error updating runePage: {ex.Message}"); + return null; + } } } } diff --git a/src/EntityFramework_LoL/Sources/Client/ApiManager.Runes.cs b/src/EntityFramework_LoL/Sources/Client/ApiManager.Runes.cs index 19fd81d..086aed9 100644 --- a/src/EntityFramework_LoL/Sources/Client/ApiManager.Runes.cs +++ b/src/EntityFramework_LoL/Sources/Client/ApiManager.Runes.cs @@ -1,4 +1,10 @@ -using Model; +using DTO; +using Model; +using System.Net.Http.Json; +using ApiMapping; +using System; +using System.Data.SqlTypes; +using Microsoft.EntityFrameworkCore.Metadata.Internal; namespace ApiManager { @@ -9,54 +15,123 @@ namespace ApiManager private const string UrlApiRunes = "/api/runes"; public RunesManager(HttpClient httpClient) : base(httpClient) { } - public Task AddItem(Rune? item) + public async Task AddItem(Rune? item) { - throw new NotImplementedException(); + try + { + var resp = await _httpClient.PostAsJsonAsync($"{UrlApiRunes}", item.ToDto()); + if (resp.IsSuccessStatusCode) + { + var createdItem = await resp.Content.ReadFromJsonAsync(); + return createdItem?.ToModel(); + } + else + { + return null; + } + } + catch (Exception ex) + { + Console.WriteLine($"Error adding rune: {ex.Message}"); + return null; + } } - public Task DeleteItem(Rune? item) + public async Task DeleteItem(Rune? item) { - throw new NotImplementedException(); + try + { + var resp = await _httpClient.DeleteAsync($"{UrlApiRunes}/{item?.Name}"); + return resp.IsSuccessStatusCode; + } + catch (Exception ex) + { + Console.WriteLine($"Error deleting rune: {ex.Message}"); + return false; + } } - public Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + private static Func filterByRuneFamily + = (rune, family) => rune.Family == family; + + private static Func filterByName + = (rune, substring) => rune.Name.Equals(substring, StringComparison.InvariantCultureIgnoreCase); + + private static Func filterByNameContains + = (rune, substring) => rune.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase); + + public async Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var runes = await _httpClient.GetFromJsonAsync>(UrlApiRunes); + return runes.Data.Select(r => r.ToModel()).GetItemsWithFilterAndOrdering( + rune => filterByName(rune, substring), + index, count, orderingPropertyName, descending); } - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var runes = await _httpClient.GetFromJsonAsync>($"{UrlApiRunes}?&index={index}&count={count}&descending={descending}"); + return runes.Data.Select(c => c.ToModel()); + } - public Task> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var runes = await _httpClient.GetFromJsonAsync>(UrlApiRunes); + return runes.Data.Select(r => r.ToModel()).GetItemsWithFilterAndOrdering( + rune => filterByRuneFamily(rune, family), + index, count, orderingPropertyName, descending); } - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var runes = await _httpClient.GetFromJsonAsync>(UrlApiRunes); + return runes.Data.Select(r => r.ToModel()).GetItemsWithFilterAndOrdering( + rune => filterByNameContains(rune, substring), + index, count, orderingPropertyName, descending); } - public Task GetNbItems() + public async Task GetNbItems() { - throw new NotImplementedException(); + var response = await _httpClient.GetAsync("/countRunes"); + var content = await response.Content.ReadAsStringAsync(); + return int.Parse(content); } - public Task GetNbItemsByFamily(RuneFamily family) + public async Task GetNbItemsByFamily(RuneFamily family) { - throw new NotImplementedException(); + var runes = await _httpClient.GetFromJsonAsync>(UrlApiRunes); + return await runes.Data.Select(r => r.ToModel()).GetNbItemsWithFilter( + rune => filterByRuneFamily(rune, family)); } - public Task GetNbItemsByName(string substring) + public async Task GetNbItemsByName(string substring) { - throw new NotImplementedException(); + var runes = await _httpClient.GetFromJsonAsync>(UrlApiRunes); + return await runes.Data.Select(r => r.ToModel()).GetNbItemsWithFilter( + rune => filterByName(rune, substring)); } - public Task UpdateItem(Rune? oldItem, Rune? newItem) + public async Task UpdateItem(Rune? oldItem, Rune? newItem) { - throw new NotImplementedException(); + try + { + var resp = await _httpClient.PutAsJsonAsync($"{UrlApiRunes}/{oldItem?.Name}", newItem.ToDto()); + if (resp.IsSuccessStatusCode) + { + var updatedItem = await resp.Content.ReadFromJsonAsync(); + return updatedItem?.ToModel(); + } + else + { + return null; + } + } + catch (Exception ex) + { + Console.WriteLine($"Error updating rune: {ex.Message}"); + return null; + } } } } diff --git a/src/EntityFramework_LoL/Sources/Client/ApiManager.Skins.cs b/src/EntityFramework_LoL/Sources/Client/ApiManager.Skins.cs index 487143c..1153fca 100644 --- a/src/EntityFramework_LoL/Sources/Client/ApiManager.Skins.cs +++ b/src/EntityFramework_LoL/Sources/Client/ApiManager.Skins.cs @@ -1,4 +1,8 @@ -using Model; +using DTO; +using Model; +using System.Net.Http.Json; +using ApiMapping; +using System.Data.SqlTypes; namespace ApiManager { @@ -9,54 +13,165 @@ namespace ApiManager private const string UrlApiSkins = "/api/Skins"; public SkinsManager(HttpClient httpClient) : base(httpClient) { } - public Task AddItem(Skin? item) + public async Task AddItem(Skin? item) { - throw new NotImplementedException(); + try + { + var resp = await _httpClient.PostAsJsonAsync($"{UrlApiSkins}", item.ToDtoC()); + if (resp.IsSuccessStatusCode) + { + var createdItem = await resp.Content.ReadFromJsonAsync(); + var championManager = new ChampionsManager(_httpClient); + var champ = await championManager.GetItemByName(createdItem.ChampionName, 0, 1); + return createdItem?.ToModelC(champ.First()); + } + else + { + return null; + } + } + catch (Exception ex) + { + Console.WriteLine($"Error adding skin: {ex.Message}"); + return null; + } } - public Task DeleteItem(Skin? item) + public async Task DeleteItem(Skin? item) { - throw new NotImplementedException(); + try + { + var resp = await _httpClient.DeleteAsync($"{UrlApiSkins}/{item?.Name}"); + return resp.IsSuccessStatusCode; + } + catch (Exception ex) + { + Console.WriteLine($"Error deleting skin: {ex.Message}"); + return false; + } } - public Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var dtoSkins = await _httpClient.GetFromJsonAsync>(UrlApiSkins); + var skins = new List(); + var championManager = new ChampionsManager(_httpClient); + foreach (var skin in dtoSkins.Data) + { + var champ = await championManager.GetItemByName(skin.ChampionName, 0, 1); + skins.Add(skin.ToModelC(champ.First())); + } + return skins.GetItemsWithFilterAndOrdering( + skin => filterByName(skin, substring), + index, count, orderingPropertyName, descending); } - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var dtoSkins = await _httpClient.GetFromJsonAsync>($"{UrlApiSkins}?&index={index}&count={count}&descending={descending}"); + var skins = new List(); + var championManager = new ChampionsManager(_httpClient); + foreach (var skin in dtoSkins.Data) + { + var champ = await championManager.GetItemByName(skin.ChampionName, 0, 1); + skins.Add(skin.ToModelC(champ.First())); + } + return skins; } - public Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + var dtoSkins = await _httpClient.GetFromJsonAsync>(UrlApiSkins); + var skins = new List(); + var championManager = new ChampionsManager(_httpClient); + foreach (var skin in dtoSkins.Data) + { + var champ = await championManager.GetItemByName(skin.ChampionName, 0, 1); + skins.Add(skin.ToModelC(champ.First())); + } + return skins.GetItemsWithFilterAndOrdering( + skin => filterByChampion(skin, champion), + index, count, orderingPropertyName, descending); } - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); + private static Func filterByChampion = (skin, champion) => champion != null && skin.Champion.Equals(champion!); + + private static Func filterByName = (skin, substring) => skin.Name.Equals(substring, StringComparison.InvariantCultureIgnoreCase); + + private static Func filterByNameContains = (skin, substring) => skin.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase); + + public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + var dtoSkins = await _httpClient.GetFromJsonAsync>(UrlApiSkins); + var skins = new List(); + var championManager = new ChampionsManager(_httpClient); + foreach (var skin in dtoSkins.Data) + { + var champ = await championManager.GetItemByName(skin.ChampionName,0,1); + skins.Add(skin.ToModelC(champ.First())); + } + return skins.GetItemsWithFilterAndOrdering( + skin => filterByNameContains(skin, substring), + index, count, orderingPropertyName, descending); } - public Task GetNbItems() + public async Task GetNbItems() { - throw new NotImplementedException(); + var response = await _httpClient.GetAsync("/countSkins"); + var content = await response.Content.ReadAsStringAsync(); + return int.Parse(content); } - public Task GetNbItemsByChampion(Champion? champion) + public async Task GetNbItemsByChampion(Champion? champion) { - throw new NotImplementedException(); + var dtoSkins = await _httpClient.GetFromJsonAsync>(UrlApiSkins); + var skins = new List(); + var championManager = new ChampionsManager(_httpClient); + foreach (var skin in dtoSkins.Data) + { + var champ = await championManager.GetItemByName(skin.ChampionName, 0, 1); + skins.Add(skin.ToModelC(champ.First())); + } + return await skins.GetNbItemsWithFilter( + skin => filterByChampion(skin, champion)); } - public Task GetNbItemsByName(string substring) + public async Task GetNbItemsByName(string substring) { - throw new NotImplementedException(); + var dtoSkins = await _httpClient.GetFromJsonAsync>(UrlApiSkins); + var skins = new List(); + var championManager = new ChampionsManager(_httpClient); + foreach (var skin in dtoSkins.Data) + { + var champ = await championManager.GetItemByName(skin.ChampionName, 0, 1); + skins.Add(skin.ToModelC(champ.First())); + } + return await skins.GetNbItemsWithFilter( + skin => filterByName(skin, substring)); } - public Task UpdateItem(Skin? oldItem, Skin? newItem) + public async Task UpdateItem(Skin? oldItem, Skin? newItem) { - throw new NotImplementedException(); + try + { + var resp = await _httpClient.PutAsJsonAsync($"{UrlApiSkins}/{oldItem?.Name}", newItem.ToDto()); + if (resp.IsSuccessStatusCode) + { + var updatedItem = await resp.Content.ReadFromJsonAsync(); + var championManager = new ChampionsManager(_httpClient); + var champ = await championManager.GetItemByName(updatedItem.ChampionName, 0, 1); + return updatedItem?.ToModelC(champ.First()); + } + else + { + return null; + } + } + catch (Exception ex) + { + Console.WriteLine($"Error updating skin: {ex.Message}"); + return null; + } } } } diff --git a/src/EntityFramework_LoL/Sources/Client/ApiManager.csproj b/src/EntityFramework_LoL/Sources/Client/ApiManager.csproj index 5a28375..61b1c54 100644 --- a/src/EntityFramework_LoL/Sources/Client/ApiManager.csproj +++ b/src/EntityFramework_LoL/Sources/Client/ApiManager.csproj @@ -7,8 +7,9 @@ - + + diff --git a/src/EntityFramework_LoL/Sources/Client/Program.cs b/src/EntityFramework_LoL/Sources/Client/Program.cs index e83d790..d55ea82 100644 --- a/src/EntityFramework_LoL/Sources/Client/Program.cs +++ b/src/EntityFramework_LoL/Sources/Client/Program.cs @@ -1,5 +1,5 @@ /*// See https://aka.ms/new-console-template for more information -*//*using static ApiManager.ApiManagerData; +using static ApiManager.ApiManagerData; Console.WriteLine("Hello, World!"); var championClient = new ChampionsManager(new HttpClient()); @@ -10,9 +10,9 @@ Console.WriteLine("All champions:"); foreach (var champion in champions) { Console.WriteLine($"{champion.Name} ({champion.Bio})"); -}*/ +} -/*// Add a new champion +*//*// Add a new champion var newChampion = new ChampionDto { Name = "Akali", Role = "Assassin" }; championClient.Add(newChampion); diff --git a/src/EntityFramework_LoL/Sources/LeagueOfLegends.sln b/src/EntityFramework_LoL/Sources/LeagueOfLegends.sln index 339d0ce..0b6b133 100644 --- a/src/EntityFramework_LoL/Sources/LeagueOfLegends.sln +++ b/src/EntityFramework_LoL/Sources/LeagueOfLegends.sln @@ -33,6 +33,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LolApp", "LolApp\LolApp.csp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ViewModels", "ViewModels\ViewModels.csproj", "{65135247-E1AB-4EE4-9473-DFDE6AFCC250}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiMapping", "ApiMapping\ApiMapping.csproj", "{F8D44A32-788B-4E63-B379-278B90216E28}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -93,6 +95,10 @@ Global {65135247-E1AB-4EE4-9473-DFDE6AFCC250}.Debug|Any CPU.Build.0 = Debug|Any CPU {65135247-E1AB-4EE4-9473-DFDE6AFCC250}.Release|Any CPU.ActiveCfg = Release|Any CPU {65135247-E1AB-4EE4-9473-DFDE6AFCC250}.Release|Any CPU.Build.0 = Release|Any CPU + {F8D44A32-788B-4E63-B379-278B90216E28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F8D44A32-788B-4E63-B379-278B90216E28}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F8D44A32-788B-4E63-B379-278B90216E28}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F8D44A32-788B-4E63-B379-278B90216E28}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/EntityFramework_LoL/Sources/LolApp/LolApp.csproj b/src/EntityFramework_LoL/Sources/LolApp/LolApp.csproj index cc14a27..ab4fa0c 100644 --- a/src/EntityFramework_LoL/Sources/LolApp/LolApp.csproj +++ b/src/EntityFramework_LoL/Sources/LolApp/LolApp.csproj @@ -82,7 +82,6 @@ - diff --git a/src/EntityFramework_LoL/Sources/LolApp/MauiProgram.cs b/src/EntityFramework_LoL/Sources/LolApp/MauiProgram.cs index c12deff..c4baebc 100644 --- a/src/EntityFramework_LoL/Sources/LolApp/MauiProgram.cs +++ b/src/EntityFramework_LoL/Sources/LolApp/MauiProgram.cs @@ -1,39 +1,37 @@ using CommunityToolkit.Maui; -using LolApp.ViewModels; -using Microsoft.Extensions.Logging; -using Microsoft.Maui.Handlers; -using Microsoft.Maui.Platform; -using Model; -using StubLib; -using ViewModels; - -namespace LolApp; - -public static class MauiProgram -{ - public static MauiApp CreateMauiApp() - { - var builder = MauiApp.CreateBuilder(); - builder - .UseMauiApp() - .UseMauiCommunityToolkit() - .ConfigureFonts(fonts => - { - fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); - fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); - fonts.AddFont("Font Awesome 6 Free-Solid-900.otf", "FASolid"); - }); - builder.Services.AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(); - -#if DEBUG - builder.Logging.AddDebug(); -#endif - - return builder.Build(); - } -} - +using LolApp.ViewModels; +using Microsoft.Extensions.Logging; +using Model; +using StubLib; +using ViewModels; + +namespace LolApp; + +public static class MauiProgram +{ + public static MauiApp CreateMauiApp() + { + var builder = MauiApp.CreateBuilder(); + builder + .UseMauiApp() + .UseMauiCommunityToolkit() + .ConfigureFonts(fonts => + { + fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); + fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); + fonts.AddFont("Font Awesome 6 Free-Solid-900.otf", "FASolid"); + }); + builder.Services.AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); + +#if DEBUG + builder.Logging.AddDebug(); +#endif + + return builder.Build(); + } +} +