diff --git a/Sources/EFManager/ManagerChampion.cs b/Sources/EFManager/ManagerChampion.cs index a38a70c..968e29f 100644 --- a/Sources/EFManager/ManagerChampion.cs +++ b/Sources/EFManager/ManagerChampion.cs @@ -5,108 +5,146 @@ using Model; namespace EFManager { - public class ManagerChampion : IChampionsManager - { - public SQLiteContext context = new SQLiteContext(); - - public async Task AddItem(Champion? item) - { - if (item == null) - return null; - - await context.AddAsync(item.toEF()); - await context.SaveChangesAsync(); - return item; - } - - public async Task DeleteItem(Champion? item) - { - if (item == null) - return false; - - context.Remove(item.toEF()); - await context.SaveChangesAsync(); - return true; - } - - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - - } - - public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - - } - - public Task> GetItemsByClass(ChampionClass championClass, 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) - { - throw new NotImplementedException(); - - } - - 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 Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } - - public Task GetNbItems() - { - throw new NotImplementedException(); - } - - public async Task GetNbItemsByCharacteristic(string charName) - { - throw new NotImplementedException(); - } - - public Task GetNbItemsByClass(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(); + public partial class ManagerData + { + public class ManagerChampion : IChampionsManager + { + private readonly ManagerData parent; + public ManagerChampion(ManagerData parent) + => this.parent = parent; + + public async Task AddItem(Champion? item) + { + if (item == null) + return null; + + await parent.DbContext.Champions.AddAsync(item.toEF()); + await parent.DbContext.SaveChangesAsync(); + return item; + } + + public async Task DeleteItem(Champion? item) + { + if (item == null) + return false; + + parent.DbContext.Champions.Remove(item.toEF()); + await parent.DbContext.SaveChangesAsync(); + return true; + } + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + { + if (orderingPropertyName != null) + { + if (descending) + { + return await Task.FromResult(parent.DbContext.Champions.OrderByDescending( + c => typeof(EFChampion).GetProperty(orderingPropertyName)) + .Skip(index * count) + .Take(count) + .Select(ce => ce.toModel()) + ); + } + else + { + return await Task.FromResult(parent.DbContext.Champions.OrderBy( + c => typeof(EFChampion).GetProperty(orderingPropertyName)) + .Skip(index * count) + .Take(count) + .Select(ce => ce.toModel()) + ); + } + } + else + { + return await Task.FromResult(parent.DbContext.Champions + .Skip(index * count) + .Take(count) + .Select(ce => ce.toModel()) + ); + } + } + + public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + + } + + public Task> GetItemsByClass(ChampionClass championClass, 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) + { + throw new NotImplementedException(); + + } + + 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 Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } + + public async Task GetNbItems() + { + return parent.DbContext.Champions.Count(); + } + + public async Task GetNbItemsByCharacteristic(string charName) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByClass(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 async Task GetNbItemsBySkill(string skill) + { + return parent.DbContext.Champions.Where(champ => skill != null && champ.Skills.Any(Skill => Skill.Name.Equals(skill))) + .Count(); + } + + public async Task UpdateItem(Champion? oldItem, Champion? newItem) + { + var toUpdate = parent.DbContext.Champions.Find(oldItem.Name); + + toUpdate = newItem.toEF(); + parent.DbContext.SaveChanges(); + + return newItem; + } } } } \ No newline at end of file diff --git a/Sources/EFManager/ManagerData.cs b/Sources/EFManager/ManagerData.cs index 4aa040f..6550534 100644 --- a/Sources/EFManager/ManagerData.cs +++ b/Sources/EFManager/ManagerData.cs @@ -5,21 +5,26 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace EFManager { - public class ManagerData : IDataManager + public partial class ManagerData : IDataManager { - public ManagerData() + public ManagerData(SQLiteContext ContextData) { - //ChampionMgr = new ChampionManager(this); TO DO + DbContext = ContextData; + ChampionsMgr = new ManagerChampion(this); + SkinsMgr = new ManagerSkin(this); } - public IChampionsManager ChampionsMgr { get; set; } + protected SQLiteContext DbContext { get; } + + public IChampionsManager ChampionsMgr { get; } - public ISkinsManager SkinsMgr { get; set; } + public ISkinsManager SkinsMgr { get; } - public IRunesManager RunesMgr { get; set; } + public IRunesManager RunesMgr { get; } - public IRunePagesManager RunePagesMgr { get; set; } + public IRunePagesManager RunePagesMgr { get; } } } diff --git a/Sources/EFManager/ManagerSkin.cs b/Sources/EFManager/ManagerSkin.cs index b028fb2..dde4c6d 100644 --- a/Sources/EFManager/ManagerSkin.cs +++ b/Sources/EFManager/ManagerSkin.cs @@ -9,6 +9,9 @@ namespace EFManager { public class ManagerSkin : ISkinsManager { + private readonly ManagerData parent; + public ManagerSkin(ManagerData parent) + => this.parent = parent; public Task AddItem(Skin? item) { throw new NotImplementedException(); diff --git a/Sources/EFManager/ManagerTranslate.cs b/Sources/EFManager/ManagerTranslate.cs index 929b9d2..8cf390d 100644 --- a/Sources/EFManager/ManagerTranslate.cs +++ b/Sources/EFManager/ManagerTranslate.cs @@ -11,8 +11,37 @@ namespace EFManager public static class ManagerTranslate { // Champion - public static EFChampion toEF(this Champion Champ) => new EFChampion { Name = Champ.Name, Bio = Champ.Bio, Icon = Champ.Icon }; - public static Champion toModel(this EFChampion EFChamp) => new Champion(EFChamp.Name); + public static EFChampion toEF(this Champion Champ, SQLiteContext context) + { + + EFChampion? EfChampion = context.Champions.Find(Champ.Name); + if (EfChampion == null) + { + EfChampion = new() + { + Name = Champ.Name, + Bio = Champ.Bio, + Icon = Champ.Icon, + Class = Champ.Class, + Image = new() { Id = Guid.NewGuid(), Base64 = Champ.Image.Base64 }, + }; + EfChampion.Skills = Champ.Skills.Select(Skill => Skill.toEF(EfChampion, context)).ToList(); + EfChampion.Characteristics = Champ.Characteristics.Select(Charac => Charac.toEF(EfChampion, context)).ToList(); + + } + return EfChampion; + + } + + public static Champion toModel(this EFChampion EFChamp) + { + var champion = new Champion(EFChamp.Name, EFChamp.Class, EFChamp.Icon, "", EFChamp.Bio); + if (EFChamp.Skills != null) foreach (var s in EFChamp.Skills) { champion.AddSkill(s.toModel()); } + if (EFChamp.Characteristics != null) foreach (var c in EFChamp.Characteristics) { champion.AddCharacteristics(c.toModel()); } + return champion; + } + + // Characteristics // Skin public static EFSkin toEF(this Skin Skin) => new EFSkin { Name = Skin.Name, Description = Skin.Description, Icon = Skin.Icon, Price = Skin.Price }; @@ -20,12 +49,13 @@ namespace EFManager // Skill - // Rune - // LargeImage public static EFLargeImage toEF(this LargeImage LargeImage) => new EFLargeImage { Id = Guid.NewGuid(), Base64 = LargeImage.Base64 }; public static LargeImage toModel(this EFLargeImage EFlargeImage) => new LargeImage(EFlargeImage.Base64); + // Rune + + // RunePage } } diff --git a/Sources/EFlib/EFCharacteristics.cs b/Sources/EFlib/EFCharacteristics.cs index 4e07186..7a48513 100644 --- a/Sources/EFlib/EFCharacteristics.cs +++ b/Sources/EFlib/EFCharacteristics.cs @@ -17,8 +17,8 @@ namespace EFlib public int Value { get; set; } // Clé étrangère vers l'entité parente + public EFChampion EFChampion { get; set; } [ForeignKey("EFChampion")] public string EFChampionName { get; set; } - public EFChampion EFChampion { get; set; } } } diff --git a/Sources/TestAPI/UnitTestSkinController.cs b/Sources/TestAPI/UnitTestSkinController.cs new file mode 100644 index 0000000..bbfd1ab --- /dev/null +++ b/Sources/TestAPI/UnitTestSkinController.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestAPI +{ + public class UnitTestSkinController + { + } +} diff --git a/Sources/Tests/TestEF/UnitTestSkin.cs b/Sources/Tests/TestEF/UnitTestSkin.cs new file mode 100644 index 0000000..2cd4b1c --- /dev/null +++ b/Sources/Tests/TestEF/UnitTestSkin.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestEF +{ + public class UnitTestSkin + { + } +} diff --git a/Sources/Tests/TestManagerEF/UnitTestManagerSkin.cs b/Sources/Tests/TestManagerEF/UnitTestManagerSkin.cs new file mode 100644 index 0000000..343c4b3 --- /dev/null +++ b/Sources/Tests/TestManagerEF/UnitTestManagerSkin.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestManagerEF +{ + public class UnitTestManagerSkin + { + } +}