From e6e16bd6afda70b8b8cdad58045197309f8f9d37 Mon Sep 17 00:00:00 2001 From: Corentin R <76619184+Koroh63@users.noreply.github.com> Date: Thu, 23 Mar 2023 00:39:08 +0100 Subject: [PATCH] Liaison de l'API avec l'EF --- Sources/API_LoL/API_LoL.csproj | 1 + .../Controllers/ChampionsController.cs | 5 +- Sources/API_LoL/Program.cs | 14 +- Sources/API_LoL/champion.db | Bin 0 -> 45056 bytes Sources/API_LoL/champion.db-shm | Bin 0 -> 32768 bytes Sources/API_LoL/champion.db-wal | 0 Sources/EF_UT/EFDataManagerChampionTest.cs | 13 ++ .../Manager/EFDataManager.Champions.cs | 46 ++++++- .../Manager/EFDataManager.Skins.cs | 85 +++++++++++++ .../EntityFramework/Manager/EFDataManager.cs | 1 + Sources/EntityFramework/Mapper/SkinMapper.cs | 24 ++++ Sources/EntityFramework/Program.cs | 120 ++++++++++-------- 12 files changed, 241 insertions(+), 68 deletions(-) create mode 100644 Sources/API_LoL/champion.db create mode 100644 Sources/API_LoL/champion.db-shm create mode 100644 Sources/API_LoL/champion.db-wal create mode 100644 Sources/EntityFramework/Manager/EFDataManager.Skins.cs create mode 100644 Sources/EntityFramework/Mapper/SkinMapper.cs diff --git a/Sources/API_LoL/API_LoL.csproj b/Sources/API_LoL/API_LoL.csproj index a8c4de7..db2c1c8 100644 --- a/Sources/API_LoL/API_LoL.csproj +++ b/Sources/API_LoL/API_LoL.csproj @@ -17,6 +17,7 @@ + diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index 1b2b021..1a077b8 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -92,9 +92,10 @@ namespace API_LoL.Controllers var list = await ChampionsManager.GetItemsByName(name, 0, 1); if (list.Count() == 1) { - var skins = await SkinsManager.GetItemsByChampion(list.First(), 0, await SkinsManager.GetNbItemsByChampion(list.First())); - if (skins.Count() != 0) + var nb = await SkinsManager.GetNbItemsByChampion(list.First()); + if (nb != 0) { + var skins = await SkinsManager.GetItemsByChampion(list.First(), 0, nb); return Ok(skins.Select(skin => skin?.ToDTO())); } else { return NoContent(); } diff --git a/Sources/API_LoL/Program.cs b/Sources/API_LoL/Program.cs index c192eac..7c535a4 100644 --- a/Sources/API_LoL/Program.cs +++ b/Sources/API_LoL/Program.cs @@ -1,4 +1,6 @@ using API_LoL; +using EntityFramework; +using EntityFramework.Manager; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Versioning; using Model; @@ -37,13 +39,19 @@ builder.Services.AddControllers(); -builder.Services.AddScoped(); - - +//builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddDbContext(); var app = builder.Build(); +using(var scope = app.Services.CreateScope()) +{ + var context = scope.ServiceProvider.GetService(); + context.Database.EnsureCreated(); +} + var apiVersionDescriptionProvider = app.Services.GetRequiredService(); diff --git a/Sources/API_LoL/champion.db b/Sources/API_LoL/champion.db new file mode 100644 index 0000000000000000000000000000000000000000..bea5ebdc870b51ff37a7cbd336d99b26a24da2d7 GIT binary patch literal 45056 zcmeI(y>H`m9LI4x4^HDWH4GI*x3E@YU3JU9sNb%BqUvHL69NbzfB*srAh4_ggOXa%8@fDr==mq^3;VEraPIj*d*{r# zxb(Wd<@de*)o!=vdZ+%LdlhXh?KG`w%QEcxBkRyG|F@B8H0p*KH8qWU=AwO@mBHIZ zQO)LLHJ#3|h^dnQT?()s$?U(=@FqdChEP;StQr*QIbc6)vmV;bAN9rZ5sPc`$@HV zWb9c-58}p;+~Bz9UG_x)X1ov4KG|v?k5(sGhE3*(qM z@_7Bbas4Un;xS_yyN#x0Kdy&eyfJ@K;I7rQ>O0nfkq(m}OlFuxS*=u76#bztOVMx* zo^`}!MB91Z??%h<;N6PyDPEuF3;O1!{PMjdxg_hcaih$nn7l|F_O-GcwVRV@>*`YY zi{!1xR&yqTIeAY7I~mgW;2I5#*?5wY7KSw3(M|CT;a?$`j}%3>H*W|gEhda7St#u1 z6#f2v`6!Ag&NFbIJ$L;G z4bqv+WQpneQJLe>FL+-WYc*DBTASB3jiBH2{L`6n_!{plqBmO^MYlEb009ILKmY**5I_I{1Q2)w1d2-jPPr^? zJH3F;cJ|Julqt3y-~Cu zB<*kQPwjW@SM4XUz=Qw-2q1s}0tg_000IagfB*t-n!rl-jvU?zP)gama(o{kuV&Zf z;az}SF{_JNaStFHzB0T6kSS)@i`D4*Km7h*{OF$v0R#|0009ILKmY**5I_I{1a7ea z*Z;RTT)Kt;0tg_000IagfB*srAb>zD!1X^v0RaRMKmY**5I_I{1Q0*~f!i+-{{KI} z|G)h?(?bLhKmY**5I_I{1Q0*~f#nw9`~T(6pWY*Y00IagfB*srAbX literal 0 HcmV?d00001 diff --git a/Sources/API_LoL/champion.db-shm b/Sources/API_LoL/champion.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 GIT binary patch literal 32768 zcmeIuAr62r3> 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(); + using(var context = new LoLDBContextWithStub() ) + { + var champ = context.Champions.ToArray(); + if (descending == false) + { + return champ.ToList().Skip(index * count).Take(count).Select(c => c.ToChampion()).OrderBy(c => c.Name); + } + else + { + return champ.ToList().Skip(index * count).Take(count).Select(c => c.ToChampion()).OrderByDescending(c => c.Name); + } + } } public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -60,11 +71,20 @@ namespace EntityFramework.Manager { using (var context = new LoLDBContextWithStub()) { - var champ = context.Champions.Where(c => c.Name.Contains(substring)).AsEnumerable(); - return champ.Select(c => c.ToChampion()).ToList(); + if (descending == false) + { + return champ.Select(c => c.ToChampion()).ToList().Skip(index * count).Take(count).OrderBy(c=> c.Name); + + } + else + { + return champ.Select(c => c.ToChampion()).ToList().Skip(index*count).Take(count).OrderByDescending(c => c.Name); + + } + + - } } @@ -78,9 +98,21 @@ namespace EntityFramework.Manager throw new NotImplementedException(); } - public Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + using(var context = new LoLDBContextWithStub()) + { + var champ = context.Champions.Where(c => c.Skills.Any(c => c.Name.Contains(skill))); + if (descending.Equals(false)) + { + return champ.Select(c=> c.ToChampion()).ToList().Skip(index * count).Take(count).OrderBy(c => c.Name); + + } + else + { + return champ.Select(c => c.ToChampion()).ToList().Skip(index * count).Take(count).OrderByDescending(c => c.Name); + } + } } public Task GetNbItems() diff --git a/Sources/EntityFramework/Manager/EFDataManager.Skins.cs b/Sources/EntityFramework/Manager/EFDataManager.Skins.cs new file mode 100644 index 0000000..bed5678 --- /dev/null +++ b/Sources/EntityFramework/Manager/EFDataManager.Skins.cs @@ -0,0 +1,85 @@ +using EntityFramework.Mapper; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFramework.Manager +{ + public partial class EFDataManager + { + public class SkinsManager : ISkinsManager + { + private readonly EFDataManager parent; + + public SkinsManager(EFDataManager parent) + { + this.parent = parent; + } + + public Task AddItem(Skin? item) + { + throw new NotImplementedException(); + } + + public Task DeleteItem(Skin? item) + { + throw new NotImplementedException(); + } + + public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } + + public async Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + using (var context = new LoLDBContextWithStub()) + { + var skins = context.Skins.Where(c => c.Champion.Equals(champion)).ToList(); + + if (descending == false) + { + return skins.Select(c => c.ToSkin()).ToList().Skip(index * count).Take(count).OrderBy(c => c.Name); + + } + else + { + return skins.Select(c => c.ToSkin()).ToList().Skip(index * count).Take(count).OrderByDescending(c => c.Name); + + } + } + } + + public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItems() + { + throw new NotImplementedException(); + } + + public async Task GetNbItemsByChampion(Champion? champion) + { + using(var context = new LoLDBContextWithStub()) + { + return context.Skins.Where(c => c.Champion.Equals(champion.ToEntity())).Count(); + } + } + + public Task GetNbItemsByName(string substring) + { + throw new NotImplementedException(); + } + + public Task UpdateItem(Skin? oldItem, Skin? newItem) + { + throw new NotImplementedException(); + } + } + } +} diff --git a/Sources/EntityFramework/Manager/EFDataManager.cs b/Sources/EntityFramework/Manager/EFDataManager.cs index 9465809..5c5d677 100644 --- a/Sources/EntityFramework/Manager/EFDataManager.cs +++ b/Sources/EntityFramework/Manager/EFDataManager.cs @@ -11,6 +11,7 @@ namespace EntityFramework.Manager { public EFDataManager() { ChampionsMgr = new ChampionsManager(this); + SkinsMgr = new SkinsManager(this); } public IChampionsManager ChampionsMgr { get; } diff --git a/Sources/EntityFramework/Mapper/SkinMapper.cs b/Sources/EntityFramework/Mapper/SkinMapper.cs new file mode 100644 index 0000000..3ce09ff --- /dev/null +++ b/Sources/EntityFramework/Mapper/SkinMapper.cs @@ -0,0 +1,24 @@ +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFramework.Mapper +{ + public static class SkinMapper + { + public static SkinEntity ToEntity(this Skin skin) + { + return new SkinEntity { Champion = skin.Champion.ToEntity(), Description = skin.Description, Icon = skin.Icon, Image = skin.Image.Base64, Name = skin.Name, Price = skin.Price }; + } + + public static Skin ToSkin(this SkinEntity entity) + { + return new Skin(entity.Name,entity.Champion.ToChampion(),price: entity.Price,icon: entity.Icon, image: entity.Image,description: entity.Description); + } + + + } +} diff --git a/Sources/EntityFramework/Program.cs b/Sources/EntityFramework/Program.cs index 818faa5..8c67f3f 100644 --- a/Sources/EntityFramework/Program.cs +++ b/Sources/EntityFramework/Program.cs @@ -1,78 +1,86 @@ // See https://aka.ms/new-console-template for more information using EntityFramework; +using EntityFramework.Manager; using Microsoft.EntityFrameworkCore; +using Model; -using ( var context = new LoLDbContext()) -{ - //context.Add(new ChampionEntity{ Name = "test", Bio = "test", Icon = "test" } ); - context.SaveChanges(); +IDataManager dataManager = new EFDataManager(); +IChampionsManager championsManager = dataManager.ChampionsMgr; +IEnumerable champions = await championsManager.GetItemsByName("A", 0, 1); +Console.WriteLine(champions.First().Name); - ChampionEntity champ = context.Find("Akali"); - if( champ != null) - { - Console - .WriteLine(champ.ToString()); +//using ( var context = new LoLDbContext()) +//{ +// //context.Add(new ChampionEntity{ Name = "test", Bio = "test", Icon = "test" } ); +// context.SaveChanges(); - } - else - { - Console.WriteLine("Not Found"); - } +// ChampionEntity champ = context.Find("Akali"); - //Test BDD Skills - ChampionEntity champSkill = new ChampionEntity { Name="nomSkill", Bio="bioSkill", Icon="iconSkill" }; +// if( champ != null) +// { +// Console +// .WriteLine(champ.ToString()); - //SkillEntity s1 = new SkillEntity { Name = "Skill1", Description = "desc", Type = SkillType.Unknown }; - SkillEntity s2 = new SkillEntity { Name="Skill2", Description="desc2", Type=SkillType.Ultimate }; - SkillEntity s3 = new SkillEntity { Name = "Skill3", Description = "desc3", Type = SkillType.Passive }; +// } +// else +// { +// Console.WriteLine("Not Found"); +// } - champSkill.AddSkill(new SkillEntity { Name = "Skill1", Description = "desc", Type = SkillType.Unknown }); - champSkill.AddSkill(s2); - champSkill.AddSkill(s3); +// //Test BDD Skills +// ChampionEntity champSkill = new ChampionEntity { Name="nomSkill", Bio="bioSkill", Icon="iconSkill" }; - context.Add(champSkill); +// //SkillEntity s1 = new SkillEntity { Name = "Skill1", Description = "desc", Type = SkillType.Unknown }; +// SkillEntity s2 = new SkillEntity { Name="Skill2", Description="desc2", Type=SkillType.Ultimate }; +// SkillEntity s3 = new SkillEntity { Name = "Skill3", Description = "desc3", Type = SkillType.Passive }; - context.SaveChanges(); +// champSkill.AddSkill(new SkillEntity { Name = "Skill1", Description = "desc", Type = SkillType.Unknown }); +// champSkill.AddSkill(s2); +// champSkill.AddSkill(s3); +// context.Add(champSkill); - //OneToMany - Console.WriteLine("Champions : "); - foreach (var champi in context.Champions.Include(a => a.skins)) - { - Console.WriteLine($"\t{champi.Name} : {champi.Bio}"); - foreach (var s in champi.skins) - { - Console.WriteLine($"\t\t{s.Name}"); - } - } +// context.SaveChanges(); - Console.WriteLine(); - Console.WriteLine("Skin :"); - foreach (var s in context.Skins) - { - Console.WriteLine($"\t{s.Name}: {s.Description} (Champion : {s.Champion.Name})"); - } +// //OneToMany +// Console.WriteLine("Champions : "); +// foreach (var champi in context.Champions.Include(a => a.skins)) +// { +// Console.WriteLine($"\t{champi.Name} : {champi.Bio}"); +// foreach (var s in champi.skins) +// { +// Console.WriteLine($"\t\t{s.Name}"); +// } +// } +// Console.WriteLine(); - Console.WriteLine("\nAjout d'un Champion et 6 Skins...\n"); +// Console.WriteLine("Skin :"); +// foreach (var s in context.Skins) +// { +// Console.WriteLine($"\t{s.Name}: {s.Description} (Champion : {s.Champion.Name})"); +// } - ChampionEntity captainMarvel = new ChampionEntity { Name = "Captain Marvel", Bio="Mais que fait un avenger ici ??", Icon="Icon.png"}; - SkinEntity[] skins = { new SkinEntity {Name = "La Fiesta", Champion = captainMarvel}, - new SkinEntity { Name = "Five Hundred Miles High", Champion = captainMarvel }, - new SkinEntity { Name = "Captain Marvel", Champion = captainMarvel }, - new SkinEntity { Name = "Time's Lie", Champion = captainMarvel }, - new SkinEntity { Name = "Lush Life", Champion = captainMarvel }, - new SkinEntity { Name = "Day Waves", Champion = captainMarvel } - }; - foreach (var s in skins) - { - captainMarvel.skins.Add(s); - } - context.Add(captainMarvel); - context.SaveChanges(); +// Console.WriteLine("\nAjout d'un Champion et 6 Skins...\n"); +// ChampionEntity captainMarvel = new ChampionEntity { Name = "Captain Marvel", Bio="Mais que fait un avenger ici ??", Icon="Icon.png"}; +// SkinEntity[] skins = { new SkinEntity {Name = "La Fiesta", Champion = captainMarvel}, +// new SkinEntity { Name = "Five Hundred Miles High", Champion = captainMarvel }, +// new SkinEntity { Name = "Captain Marvel", Champion = captainMarvel }, +// new SkinEntity { Name = "Time's Lie", Champion = captainMarvel }, +// new SkinEntity { Name = "Lush Life", Champion = captainMarvel }, +// new SkinEntity { Name = "Day Waves", Champion = captainMarvel } +// }; +// foreach (var s in skins) +// { +// captainMarvel.skins.Add(s); +// } -} +// context.Add(captainMarvel); +// context.SaveChanges(); + + +//}