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 0000000..bea5ebd Binary files /dev/null and b/Sources/API_LoL/champion.db differ diff --git a/Sources/API_LoL/champion.db-shm b/Sources/API_LoL/champion.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/Sources/API_LoL/champion.db-shm differ diff --git a/Sources/API_LoL/champion.db-wal b/Sources/API_LoL/champion.db-wal new file mode 100644 index 0000000..e69de29 diff --git a/Sources/EF_UT/EFDataManagerChampionTest.cs b/Sources/EF_UT/EFDataManagerChampionTest.cs new file mode 100644 index 0000000..80f34af --- /dev/null +++ b/Sources/EF_UT/EFDataManagerChampionTest.cs @@ -0,0 +1,39 @@ +using EntityFramework; +using EntityFramework.Manager; +using FluentAssertions; +using FluentAssertions.Primitives; +using Microsoft.EntityFrameworkCore; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EF_UT +{ + [TestClass] + public class EFDataManagerChampionTest + { + [TestMethod] + public void Add_ValidChampion_Added() + { + IDataManager dataManager = new EFDataManager(); + IChampionsManager championsManager = dataManager.ChampionsMgr; + + var champ = championsManager.AddItem(new Champion("test")); + } + + //[TestMethod] + //public async Task GetItemsByName_DefaultChamp_One() + //{ + // IDataManager dataManager = new EFDataManager(); + // IChampionsManager championsManager = dataManager.ChampionsMgr; + + // var ak = (await championsManager.GetItemsByName("A",0,1)).First(); + + // Assert.IsNotNull(ak); + // //Assert.AreEqual("Akali", ak.Name); + //} + } +} diff --git a/Sources/EntityFramework/EntityFramework.csproj b/Sources/EntityFramework/EntityFramework.csproj index 2d1f7fc..358fe36 100644 --- a/Sources/EntityFramework/EntityFramework.csproj +++ b/Sources/EntityFramework/EntityFramework.csproj @@ -1,4 +1,4 @@ - + Exe @@ -8,6 +8,14 @@ $(MSBuildProjectDirectory) + + + + + + + + @@ -18,6 +26,7 @@ + diff --git a/Sources/EntityFramework/LoLDbContext.cs b/Sources/EntityFramework/LoLDbContext.cs index af9be3b..003ff03 100644 --- a/Sources/EntityFramework/LoLDbContext.cs +++ b/Sources/EntityFramework/LoLDbContext.cs @@ -35,7 +35,7 @@ namespace EntityFramework protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasKey(entity => entity.Name); - modelBuilder.Entity().ToTable("Champion"); + modelBuilder.Entity().ToTable("Champions"); //modelBuilder.Entity().Property(entity => entity.Id) // .ValueGeneratedOnAdd(); diff --git a/Sources/EntityFramework/Manager/EFDataManager.Champions.cs b/Sources/EntityFramework/Manager/EFDataManager.Champions.cs new file mode 100644 index 0000000..e784096 --- /dev/null +++ b/Sources/EntityFramework/Manager/EFDataManager.Champions.cs @@ -0,0 +1,159 @@ +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 ChampionsManager : IChampionsManager + { + private readonly EFDataManager parent; + + public ChampionsManager(EFDataManager parent) + { + this.parent = parent; + } + + public async Task AddItem(Champion? item) + { + using(var context = new LoLDBContextWithStub()) + { + if (item != null) + { + context.Add(item.ToEntity()); + return item; + } + else + { + throw new Exception(); + } + } + } + + public Task DeleteItem(Champion? item) + { + throw new NotImplementedException(); + } + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + { + 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) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByClass(Model.ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } + + public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + using (var context = new LoLDBContextWithStub()) + { + var champ = context.Champions.Where(c => c.Name.Contains(substring)).AsEnumerable(); + 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); + + } + + + + } + } + + public Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } + + public Task> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } + + public async Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + 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() + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByCharacteristic(string charName) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByClass(Model.ChampionClass championClass) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByName(string substring) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByRunePage(RunePage? runePage) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsBySkill(Skill? skill) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsBySkill(string skill) + { + throw new NotImplementedException(); + } + + public Task UpdateItem(Champion? oldItem, Champion? newItem) + { + throw new NotImplementedException(); + } + } + } +} 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 new file mode 100644 index 0000000..5c5d677 --- /dev/null +++ b/Sources/EntityFramework/Manager/EFDataManager.cs @@ -0,0 +1,25 @@ +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 : IDataManager + { + public EFDataManager() { + ChampionsMgr = new ChampionsManager(this); + SkinsMgr = new SkinsManager(this); + } + public IChampionsManager ChampionsMgr { get; } + + public ISkinsManager SkinsMgr { get; } + + public IRunesManager RunesMgr { get; } + + public IRunePagesManager RunePagesMgr { get; } + + } +} diff --git a/Sources/EntityFramework/Mapper/ChampionMapper.cs b/Sources/EntityFramework/Mapper/ChampionMapper.cs index 14942d1..da146b2 100644 --- a/Sources/EntityFramework/Mapper/ChampionMapper.cs +++ b/Sources/EntityFramework/Mapper/ChampionMapper.cs @@ -12,5 +12,11 @@ namespace EntityFramework.Mapper public static ChampionEntity ToEntity(this Champion champion) { return new ChampionEntity { Name = champion.Name, Bio = champion.Bio, Icon = champion.Icon }; } + + public static Champion ToChampion(this ChampionEntity champion) + { + return new Champion(champion.Name,bio: champion.Bio,icon: champion.Icon); + } + } } 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/Migrations/20230315145258_myMig.Designer.cs b/Sources/EntityFramework/Migrations/20230319224555_myMig.Designer.cs similarity index 98% rename from Sources/EntityFramework/Migrations/20230315145258_myMig.Designer.cs rename to Sources/EntityFramework/Migrations/20230319224555_myMig.Designer.cs index 34ce7d8..2a90b9d 100644 --- a/Sources/EntityFramework/Migrations/20230315145258_myMig.Designer.cs +++ b/Sources/EntityFramework/Migrations/20230319224555_myMig.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace EntityFramework.Migrations { [DbContext(typeof(LoLDBContextWithStub))] - [Migration("20230315145258_myMig")] + [Migration("20230319224555_myMig")] partial class myMig { /// @@ -40,7 +40,7 @@ namespace EntityFramework.Migrations b.HasKey("Name"); - b.ToTable("Champion", (string)null); + b.ToTable("Champions", (string)null); b.HasData( new diff --git a/Sources/EntityFramework/Migrations/20230315145258_myMig.cs b/Sources/EntityFramework/Migrations/20230319224555_myMig.cs similarity index 91% rename from Sources/EntityFramework/Migrations/20230315145258_myMig.cs rename to Sources/EntityFramework/Migrations/20230319224555_myMig.cs index 86b3a87..0e61c80 100644 --- a/Sources/EntityFramework/Migrations/20230315145258_myMig.cs +++ b/Sources/EntityFramework/Migrations/20230319224555_myMig.cs @@ -13,7 +13,7 @@ namespace EntityFramework.Migrations protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( - name: "Champion", + name: "Champions", columns: table => new { Name = table.Column(type: "TEXT", maxLength: 50, nullable: false), @@ -23,7 +23,7 @@ namespace EntityFramework.Migrations }, constraints: table => { - table.PrimaryKey("PK_Champion", x => x.Name); + table.PrimaryKey("PK_Champions", x => x.Name); }); migrationBuilder.CreateTable( @@ -52,9 +52,9 @@ namespace EntityFramework.Migrations { table.PrimaryKey("PK_SkillEntity", x => x.Name); table.ForeignKey( - name: "FK_SkillEntity_Champion_ChampionEntityName", + name: "FK_SkillEntity_Champions_ChampionEntityName", column: x => x.ChampionEntityName, - principalTable: "Champion", + principalTable: "Champions", principalColumn: "Name"); }); @@ -73,14 +73,14 @@ namespace EntityFramework.Migrations { table.PrimaryKey("PK_Skins", x => x.Name); table.ForeignKey( - name: "FK_Skins_Champion_ChampionEntityForeignKey", + name: "FK_Skins_Champions_ChampionEntityForeignKey", column: x => x.ChampionEntityForeignKey, - principalTable: "Champion", + principalTable: "Champions", principalColumn: "Name"); }); migrationBuilder.InsertData( - table: "Champion", + table: "Champions", columns: new[] { "Name", "Bio", "Icon", "Image" }, values: new object[,] { @@ -116,7 +116,7 @@ namespace EntityFramework.Migrations name: "Skins"); migrationBuilder.DropTable( - name: "Champion"); + name: "Champions"); } } } diff --git a/Sources/EntityFramework/Migrations/LoLDBContextWithStubModelSnapshot.cs b/Sources/EntityFramework/Migrations/LoLDBContextWithStubModelSnapshot.cs index 1bbd357..e00a86a 100644 --- a/Sources/EntityFramework/Migrations/LoLDBContextWithStubModelSnapshot.cs +++ b/Sources/EntityFramework/Migrations/LoLDBContextWithStubModelSnapshot.cs @@ -37,7 +37,7 @@ namespace EntityFramework.Migrations b.HasKey("Name"); - b.ToTable("Champion", (string)null); + b.ToTable("Champions", (string)null); b.HasData( new 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(); + + +//} diff --git a/Sources/EntityFramework/champion.db b/Sources/EntityFramework/champion.db index d4cefaf..d6b07a7 100644 Binary files a/Sources/EntityFramework/champion.db and b/Sources/EntityFramework/champion.db differ