diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index b23a534..39c0042 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -11,13 +11,17 @@ namespace EFLol public string Name { get; set; } [MaxLength(256, ErrorMessage = "Bio cannot be longer than 256 characters.")] - public string Bio { get; set; } - - //public ChampionClass Class { get; set; } - //public string Icon { get; set; } - //public LargeImage Image { get; set; } - //public ReadOnlyDictionary Characteristics { get; private set; } - //private HashSet skills = new HashSet(); - public ReadOnlyCollection Skins { get; set; } - } + public string Bio { get; set; } + + //public ChampionClass Class { get; set; } + //public string Icon { get; set; } + //public LargeImage Image { get; set; } + //public ReadOnlyDictionary Characteristics { get; private set; } + + + + public HashSet skills = new HashSet(); + public Collection Skins { get; set; } + public Collection ListRunePages { get; set; } +} } diff --git a/Sources/EFLol/DBDataManager/DataConverter.cs b/Sources/EFLol/DBDataManager/DataConverter.cs new file mode 100644 index 0000000..d0e23f8 --- /dev/null +++ b/Sources/EFLol/DBDataManager/DataConverter.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Model; + +namespace EFLol.DBDataManager +{ + public static class DataConverter + { + public static Champion ChampionToPoco(this ChampionEntity champion) + { + return new Champion(champion.Name, champion.Bio); + } + + public static ChampionEntity ChampionToEntity(this Champion champion) + { + return new ChampionEntity + { + Name = champion.Name, + Bio = champion.Bio + }; + } + } +} diff --git a/Sources/EFLol/DBDataManager/EFDataManager.cs b/Sources/EFLol/DBDataManager/EFDataManager.cs new file mode 100644 index 0000000..5537631 --- /dev/null +++ b/Sources/EFLol/DBDataManager/EFDataManager.cs @@ -0,0 +1,132 @@ +using Model; + + +namespace EFLol.DBDataManager +{ + public class EFDataManager : IDataManager + { + public IChampionsManager ChampionsMgr => new EFChampionManager(); + + public ISkinsManager SkinsMgr => throw new NotImplementedException(); + + public IRunesManager RunesMgr => throw new NotImplementedException(); + + public IRunePagesManager RunePagesMgr => throw new NotImplementedException(); + } + + public class EFChampionManager : IChampionsManager + { + private MyDbContext _context; + + public async Task AddItem(Champion? item) + { + if (item == null) + { + return null; + } + var addItem = await _context.AddAsync(item); + await _context.SaveChangesAsync(); + return addItem.Entity; + + // Va chercher les info du context (Context.champions.get)(DbSet) et les map en champion model + // Dans Program.cs de API rajouter un scope (AddScope ) -> Ca va dire que a chaque fois que j'utilise un IDataManager, il va créer un EFDataManager + } + + + public async Task DeleteItem(Champion? item) + { + if (item == null) + { + return false; + } + var deletedItem = _context.Remove(item); + await _context.SaveChangesAsync(); + return true; + } + + private Func filterByName = (champion, substring) => + champion.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase); + + + public Task UpdateItem(Champion? oldItem, Champion? newItem) + { + throw new NotImplementedException(); + } + + public Task GetNbItems() + { + throw new NotImplementedException(); + } + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool @descending = false) + { + IEnumerable champions = _context.Champions.Skip(index * count).Take(count).OrderBy(champion => orderingPropertyName).Select(champion => champion.ChampionToPoco()); + return champions; + } + + public Task GetNbItemsByName(string substring) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, + bool @descending = false) + { + throw new NotImplementedException(); + } + + + public Task GetNbItemsByCharacteristic(string charName) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, + bool @descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByClass(ChampionClass championClass) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, + bool @descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsBySkill(Skill? skill) + { + throw new NotImplementedException(); + } + + public Task> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool @descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByRunePage(RunePage? runePage) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, + bool @descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsBySkill(string skill) + { + throw new NotImplementedException(); + } + + public Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool @descending = false) + { + throw new NotImplementedException(); + } + } +} diff --git a/Sources/EFLol/EFLol.csproj b/Sources/EFLol/EFLol.csproj index bbc403a..17e021f 100644 --- a/Sources/EFLol/EFLol.csproj +++ b/Sources/EFLol/EFLol.csproj @@ -24,5 +24,10 @@ + + + + + diff --git a/Sources/EFLol/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index 17ad759..7416c4d 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -1,37 +1,46 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; - -namespace EFLol -{ - public class MyDbContext : DbContext - { - public DbSet Champions { get; set; } - public DbSet Skins { get; set; } - - public MyDbContext() - { } - - public MyDbContext(DbContextOptions options) - : base(options) - { } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (!optionsBuilder.IsConfigured) - { - optionsBuilder.UseSqlite("Data Source=loldb.db"); - } - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.Entity().Property(c => c.Id).ValueGeneratedOnAdd(); - modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace EFLol +{ + public class MyDbContext : DbContext + { + public DbSet Champions { get; set; } + public DbSet Skins { get; set; } + public DbSet Skill { get; set; } + public DbSet RunePages { get; set; } + public DbSet Runes { get; set; } + + public MyDbContext() + { } + + public MyDbContext(DbContextOptions options) + : base(options) + { } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseSqlite("Data Source=loldb.db"); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity().Property(c => c.Id).ValueGeneratedOnAdd(); + modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + + modelBuilder.Entity().HasMany(p => p.skills).WithMany(p => p.champions).UsingEntity(j => j.ToTable("ChampionSkills")); + + modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + } + } +} diff --git a/Sources/EFLol/Program.cs b/Sources/EFLol/Program.cs index fbac836..6720439 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -1,67 +1,105 @@ -using System.Collections.ObjectModel; -using EFLol; - - -using (var context = new MyDbContext()) -{ - // Clean the DB before starting - Console.WriteLine("Clean the DB before starting"); - context.Database.EnsureDeleted(); - context.Database.EnsureCreated(); -} - -SkinEntity black = new SkinEntity { Name = "Black", Description = "Black skin", Icon = "black.png", Price = 0.99f }; -SkinEntity white = new SkinEntity { Name = "White", Description = "White skin", Icon = "white.png", Price = 150.99f }; -SkinEntity green = new SkinEntity { Name = "Green", Description = "Green skin", Icon = "green.png", Price = 4.99f }; - -ChampionEntity Zeus = new ChampionEntity -{ - Name = "Zeus", - Bio = "Zeus is the god of the sky", - Skins = new ReadOnlyCollection(new List { black, white }) - -}; -ChampionEntity Hera = new ChampionEntity -{ - Name = "Hera", - Bio = "Hera is the goddess of marriage", - Skins = new ReadOnlyCollection(new List { green }) - -}; -ChampionEntity Poseidon = new ChampionEntity { Name = "Poseidon", Bio = "Poseidon is the god of the sea" }; - - - -using (var context = new MyDbContext()) -{ - // Crée des champions et les insère dans la base - Console.WriteLine("Creates and inserts new Champions"); - context.Add(Zeus); - context.Add(Hera); - context.Add(Poseidon); - context.SaveChanges(); -} - - -using (var context = new MyDbContext()) -{ - foreach (var n in context.Champions) - { - // Use LINQ to display the skins for each champion - var skins = from s in context.Skins - where n.Id == s.Id - select s; - - Console.WriteLine($"Champion n°{n.Id} - {n.Name}"); - } - context.SaveChanges(); -} - -using (var context = new MyDbContext()) -{ - foreach (var n in context.Skins) - { - Console.WriteLine($"Skin n°{n.Id} - {n.Name}" + (n.Price != null ? $" - {n.Price}" : "")); - } - context.SaveChanges(); +using System.Collections.Generic; +using System.Collections.ObjectModel; +using EFLol; + + +internal class Program +{ + private static void Main(string[] args) + { + using (var context = new MyDbContext()) + { + // Clean the DB before starting + Console.WriteLine("Clean the DB before starting"); + context.Database.EnsureDeleted(); + context.Database.EnsureCreated(); + } + + + SkinEntity black = new SkinEntity { Name = "Black", Description = "Black skin", Icon = "black.png", Price = 0.99f }; + SkinEntity white = new SkinEntity { Name = "White", Description = "White skin", Icon = "white.png", Price = 150.99f }; + SkinEntity green = new SkinEntity { Name = "Green", Description = "Green skin", Icon = "green.png", Price = 4.99f }; + + + RuneEntity rune1 = new RuneEntity { Id = 1, Name = "rune1", Description = "super rune1" }; + RuneEntity rune2 = new RuneEntity { Id = 2, Name = "rune2", Description = "super rune2" }; + RuneEntity rune3 = new RuneEntity { Id = 3, Name = "rune3", Description = "super rune3" }; + + RunePageEntity runePage1 = new RunePageEntity { Id = 1, Name = "runepage1"/*, Runes = new Dictionary { { Category.Major, rune1 } }*/ }; + + ChampionEntity Zeus = new ChampionEntity { Name = "Zeus", Bio = "Zeus is the god of the sky", Skins = new Collection(new List { black, white }) }; + ChampionEntity Hera = new ChampionEntity { Name = "Hera", Bio = "Hera is the goddess of marriage", Skins = new Collection(new List { green }) }; + ChampionEntity Poseidon = new ChampionEntity { + Name = "Poseidon", Bio = "Poseidon is the god of the sea", + ListRunePages = new Collection(new List { { runePage1 } }) + }; + + SkillEntity skill1 = new SkillEntity + { + Id = 1, + Name = "skill1", + Description = "super skill", + SkillType = SkillTypeEntity.Basic, + champions = new HashSet { Zeus, Hera } + }; + + SkillEntity skill2 = new SkillEntity + { + Id = 2, + Name = "skill2", + Description = "super skill", + SkillType = SkillTypeEntity.Basic, + champions = new HashSet { Zeus} + }; + + Zeus.skills.Add(skill1); + Zeus.skills.Add(skill2); + + Hera.skills.Add(skill1); + + using (var context = new MyDbContext()) + { + Console.WriteLine("Creates and inserts new Champions"); + context.Add(Zeus); + context.Add(Hera); + context.Add(Poseidon); + + context.Add(skill1); + context.Add(skill2); + context.SaveChanges(); + } + + + using (var context = new MyDbContext()) + { + foreach (var n in context.Champions) + { + // LINQ to select the skins of the current champion + var skins = context.Champions.Where(c => c.Id == n.Id).SelectMany(c => c.Skins); + var runePage = context.Champions.Where(c => c.Id == n.Id).SelectMany(c => c.ListRunePages); + + // Display the champion and its skins + Console.WriteLine($"Champion n°{n.Id} - {n.Name} : {skins.Count()} skins, {runePage.Count()} runePage"); + } + context.SaveChanges(); + } + + using (var context = new MyDbContext()) + { + foreach (var n in context.Skins) + { + Console.WriteLine($"Skin n°{n.Id} - {n.Name}" + (n.Price != null ? $" - {n.Price}" : "")); + } + context.SaveChanges(); + } + + using (var context = new MyDbContext()) + { + foreach (var n in context.RunePages) + { + Console.WriteLine($"runePage n°{n.Id} - {n.Name}"); + } + context.SaveChanges(); + } + } } \ No newline at end of file diff --git a/Sources/EFLol/RuneEntity.cs b/Sources/EFLol/RuneEntity.cs new file mode 100644 index 0000000..6b492fb --- /dev/null +++ b/Sources/EFLol/RuneEntity.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EFLol +{ + public class RuneEntity + { + public int Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public RuneFamilyEntity RuneFamily { get; set; } + public Collection ListRunePages { get; set; } + } + + public enum RuneFamilyEntity + { + Unknown, + Precision, + Domination + } + +} + diff --git a/Sources/EFLol/RunePageEntity.cs b/Sources/EFLol/RunePageEntity.cs new file mode 100644 index 0000000..4649f76 --- /dev/null +++ b/Sources/EFLol/RunePageEntity.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EFLol +{ + public class RunePageEntity + { + public int Id { get; set; } + public String Name { get; set; } + //public Dictionary Runes { get; set; } + } + + public enum Category + { + Major, + Minor1, + Minor2, + Minor3, + OtherMinor1, + OtherMinor2 + } +} diff --git a/Sources/EFLol/SkillEntity.cs b/Sources/EFLol/SkillEntity.cs new file mode 100644 index 0000000..c9c90ee --- /dev/null +++ b/Sources/EFLol/SkillEntity.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EFLol +{ + public class SkillEntity + { + public int Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public SkillTypeEntity SkillType { get; set; } + public HashSet champions { get; set; } + + } + + public enum SkillTypeEntity + { + Unknown, + Basic, + Passive, + Ultimate + } +} diff --git a/Sources/LeagueOfLegends.sln b/Sources/LeagueOfLegends.sln index 8a34ca4..b0489f8 100644 --- a/Sources/LeagueOfLegends.sln +++ b/Sources/LeagueOfLegends.sln @@ -35,10 +35,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUnitaire", "TestUnitair {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LolApp", "LolApp\LolApp.csproj", "{0C898A04-092A-49AA-BE65-8AE818A2AF50}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ViewModels", "ViewModels\ViewModels.csproj", "{0ECDCB33-39F0-444B-B787-2EFDD9422870}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -73,16 +69,6 @@ Global {5702F240-97BD-4757-918C-6E485C57D0EC}.Debug|Any CPU.Build.0 = Debug|Any CPU {5702F240-97BD-4757-918C-6E485C57D0EC}.Release|Any CPU.ActiveCfg = Release|Any CPU {5702F240-97BD-4757-918C-6E485C57D0EC}.Release|Any CPU.Build.0 = Release|Any CPU - {0C898A04-092A-49AA-BE65-8AE818A2AF50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {0C898A04-092A-49AA-BE65-8AE818A2AF50}.Debug|Any CPU.Build.0 = Debug|Any CPU - {0C898A04-092A-49AA-BE65-8AE818A2AF50}.Debug|Any CPU.Deploy.0 = Debug|Any CPU - {0C898A04-092A-49AA-BE65-8AE818A2AF50}.Release|Any CPU.ActiveCfg = Release|Any CPU - {0C898A04-092A-49AA-BE65-8AE818A2AF50}.Release|Any CPU.Build.0 = Release|Any CPU - {0C898A04-092A-49AA-BE65-8AE818A2AF50}.Release|Any CPU.Deploy.0 = Release|Any CPU - {0ECDCB33-39F0-444B-B787-2EFDD9422870}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {0ECDCB33-39F0-444B-B787-2EFDD9422870}.Debug|Any CPU.Build.0 = Debug|Any CPU - {0ECDCB33-39F0-444B-B787-2EFDD9422870}.Release|Any CPU.ActiveCfg = Release|Any CPU - {0ECDCB33-39F0-444B-B787-2EFDD9422870}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Sources/Model/Champion.cs b/Sources/Model/Champion.cs index fd4a5ca..0e33455 100644 --- a/Sources/Model/Champion.cs +++ b/Sources/Model/Champion.cs @@ -53,7 +53,13 @@ public class Champion : IEquatable Skins = new ReadOnlyCollection(skins); } - public ReadOnlyCollection Skins { get; private set; } + public Champion(string name, string bio) + { + this.name = name; + this.bio = bio; + } + + public ReadOnlyCollection Skins { get; private set; } private List skins = new (); public ReadOnlyDictionary Characteristics { get; private set; } diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index d9bae90..02244e0 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -1,6 +1,7 @@ using EFLol; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using System.Collections.ObjectModel; namespace TestUnitaire { @@ -9,14 +10,14 @@ namespace TestUnitaire [Theory] - [InlineData("Zeus", "Dieu de la foudre", true)] - [InlineData("Hades", "Dieu des enfers", true)] - [InlineData("Aphrodite", "Déesse de l'amour", true)] - [InlineData("AresAresAresAresAresAresAresAresAresAres", + [InlineData(0,"Zeus", "Dieu de la foudre", true)] + [InlineData(10,"Hades", "Dieu des enfers", true)] + [InlineData(1,"Aphrodite", "Déesse de l'amour", true)] + [InlineData(10,"AresAresAresAresAresAresAresAresAresAres", "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" + "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" + "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre", false)] - public async Task TestAddInMemory(String name, String bio, bool expected) + public async Task TestAddChampionInMemory(int id, String name, String bio, bool expected) { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); @@ -28,11 +29,20 @@ namespace TestUnitaire using (var context = new MyDbContext(options)) { await context.Database.EnsureCreatedAsync(); - var Dieu = new ChampionEntity + SkinEntity black = new SkinEntity { Name = "Black", Description = "Black skin", Icon = "black.png", Price = 0.99f }; + SkinEntity white = new SkinEntity { Name = "White", Description = "White skin", Icon = "white.png", Price = 150.99f }; + SkinEntity green = new SkinEntity { Name = "Green", Description = "Green skin", Icon = "green.png", Price = 4.99f }; + + RunePageEntity runePage1 = new RunePageEntity { Id = 1, Name = "runepage1"}; + + var Dieu = new ChampionEntity { + Id = id, Name = name, - Bio = bio - }; + Bio = bio, + Skins = new Collection(new List { black, white, green }), + ListRunePages = new Collection(new List { { runePage1 } }) + }; ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); Assert.Null(found); @@ -45,24 +55,26 @@ namespace TestUnitaire Assert.Equal(1, await context.Champions.CountAsync()); Assert.Equal(name, found.Name); + Assert.Equal(3,found.Skins.Count); + Assert.Equal(1, found.ListRunePages.Count); - // Test if the max length of the name is respected (30) and the max length of the bio is respected (256) - if (expected) + // Test if the max length of the name is respected (30) and the max length of the bio is respected (256) + if (expected) { Assert.True(found.Name.Length <= 30); Assert.True(found.Bio.Length <= 256); } else { - Assert.False(found.Bio.Length <= 256); Assert.False(found.Name.Length <= 30); + Assert.False(found.Bio.Length <= 256); } } } [Fact] - public void ModifyTestInMemory() + public void TestModifyChampionInMemory() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); @@ -79,7 +91,7 @@ namespace TestUnitaire ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is the king of the gods." }; ChampionEntity yoda = new ChampionEntity { Name = "Yoda", Bio = "Zeus is the king of the gods." }; - ChampionEntity ewok = new ChampionEntity { Name = "Ewok", Bio = "Zeus is the king of the gods." }; + ChampionEntity ewok = new ChampionEntity {Name = "Ewok", Bio = "Zeus is the king of the gods." }; context.Champions.Add(chewie); @@ -113,5 +125,54 @@ namespace TestUnitaire Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); } } - } + + [Theory] + + [InlineData(0,"black", "super Skin", "icon1.png",190000000.2f,true)] + [InlineData(1,"White", "skin1", "icon1", 19, true)] + [InlineData(2,"Green", "skin", "icon1.jpg", -1, false)] + public async Task TestAddSkinToChampionInMemory(int id,String name, String description, String icon,float price,bool expected) + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new MyDbContext(options)) + { + await context.Database.EnsureCreatedAsync(); + + var Dieu = new ChampionEntity + { + Id = 0, + Name = "Zeus", + Bio = "Dieu de la foudre", + Skins = new Collection() + }; + + SkinEntity item = new SkinEntity + { + Id = id, + Name = name, + Description = description, + Icon = icon, + Price = price + }; + + Dieu.Skins.Add(item); + + ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); + Assert.Null(found); + + await context.Champions.AddAsync(Dieu); + await context.SaveChangesAsync(); + + found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); + } + + } + + } } \ No newline at end of file diff --git a/Sources/apiLOL/Controllers/ControllerChampions.cs b/Sources/apiLOL/Controllers/ControllerChampions.cs index 0817c85..d2f5bd0 100644 --- a/Sources/apiLOL/Controllers/ControllerChampions.cs +++ b/Sources/apiLOL/Controllers/ControllerChampions.cs @@ -12,12 +12,13 @@ namespace apiLOL.Controllers public class ControllerChampions : Controller { - private readonly IDataManager _data; + //private readonly IDataManager data; + private IChampionsManager _dataManager; private readonly ILogger _logger; public ControllerChampions(IDataManager manager, ILogger log) { - _data = manager; + _dataManager = manager.ChampionsMgr; _logger = log; } @@ -26,18 +27,17 @@ namespace apiLOL.Controllers [ProducesResponseType(typeof(ChampionPageDTO), 200)] public async Task Get([FromQuery] int index = 0, int count = 10, string? name = "") { - - _logger.LogInformation($"methode Get de ControllerChampions appelée index:{index}, count: {count} et name:{name}"); - int nbChampions = await _data.ChampionsMgr.GetNbItems(); + //FromQuery permet de filtrer dans la collection de champions en fonction du nom + // Possible de faire une classe PageRequest pour gérer les paramètres index et count + _logger.LogInformation($"methode Get de ControllerChampions appelée"); + int nbChampions = await _dataManager.GetNbItems(); _logger.LogInformation($"Nombre de champions : {nbChampions}"); - var champions = await _data.ChampionsMgr.GetItems(index, nbChampions); - var filteredChampions = champions.Where(Model => Model.Name.Contains(name)).Skip(index * count).Take(count); - var championDTO = filteredChampions.Select(Model => Model.ToDTO()); + var champs = (await _dataManager.GetItems(index, count)).Select(Model => Model.ToDTO()); var page = new ChampionPageDTO { - Data = championDTO, + Data = champs, Index = index, Count = count, TotalCount = nbChampions @@ -54,7 +54,7 @@ namespace apiLOL.Controllers _logger.LogInformation($"methode GetChampion de ControllerChampions appelée avec le paramètre {name}"); try { - var champs = (await _data.ChampionsMgr.GetItemsByName(name, 0, 1)); + var champs = (await _dataManager.GetItemsByName(name, 0, 1)); return Ok(champs.First().ToDTO()); } catch (Exception ex) @@ -72,7 +72,7 @@ namespace apiLOL.Controllers try { Champion tmp = champDTO.ToModel(); - Champion champion = await _data.ChampionsMgr.AddItem(tmp); + Champion champion = await _dataManager.AddItem(tmp); ChampionDTO dtoChamp = champion.ToDTO(); return CreatedAtAction(nameof(GetChampion), new {name = dtoChamp.Name}, dtoChamp); } @@ -91,7 +91,7 @@ namespace apiLOL.Controllers try { - var champs = (await _data.ChampionsMgr.GetItemsByName(name, 0, 1)).First(); + var champs = (await _dataManager.GetItemsByName(name, 0, 1)).First(); champs.Bio = bio; return Ok(champs.ToDTO()); } @@ -109,8 +109,8 @@ namespace apiLOL.Controllers try { - var champ = (await _data.ChampionsMgr.GetItemsByName(name, 0, 1)).First(); - await _data.ChampionsMgr.DeleteItem(champ); + var champ = (await _dataManager.GetItemsByName(name, 0, 1)).First(); + _dataManager.DeleteItem(champ); return Ok(champ.ToDTO()); } catch (Exception ex) diff --git a/Sources/apiLOL/Program.cs b/Sources/apiLOL/Program.cs index 80d2be9..063a905 100644 --- a/Sources/apiLOL/Program.cs +++ b/Sources/apiLOL/Program.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.AspNetCore.Mvc; +using EFLol.DBDataManager; using Model; using StubLib; @@ -12,7 +13,8 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddSingleton(); +//builder.Services.AddSingleton(); +builder.Services.AddScoped(); // Scoped et Transient sont des types de cycle de vie // Transient : une instance a chaque contruction d'objet // Scoped : une instance par requete client @@ -35,6 +37,7 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } + app.UseHttpsRedirection(); app.UseAuthorization();