From 636ae58e55b5f720e762b01fe98c262a15fecff9 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@gmail.com" Date: Wed, 1 Mar 2023 16:44:22 +0100 Subject: [PATCH 01/14] add TestAddSkinToChampionInMemory --- Sources/EFLol/ChampionEntity.cs | 2 +- Sources/EFLol/Program.cs | 4 +- Sources/TestUnitaire/TestEfLol.cs | 61 +++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index b23a534..7cdd4da 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -18,6 +18,6 @@ namespace EFLol //public LargeImage Image { get; set; } //public ReadOnlyDictionary Characteristics { get; private set; } //private HashSet skills = new HashSet(); - public ReadOnlyCollection Skins { get; set; } + public Collection Skins { get; set; } } } diff --git a/Sources/EFLol/Program.cs b/Sources/EFLol/Program.cs index fbac836..9897c31 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -18,14 +18,14 @@ ChampionEntity Zeus = new ChampionEntity { Name = "Zeus", Bio = "Zeus is the god of the sky", - Skins = new ReadOnlyCollection(new List { black, white }) + Skins = new Collection(new List { black, white }) }; ChampionEntity Hera = new ChampionEntity { Name = "Hera", Bio = "Hera is the goddess of marriage", - Skins = new ReadOnlyCollection(new List { green }) + Skins = new Collection(new List { green }) }; ChampionEntity Poseidon = new ChampionEntity { Name = "Poseidon", Bio = "Poseidon is the god of the sea" }; diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index d9bae90..21851dd 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 { @@ -28,10 +29,16 @@ namespace TestUnitaire using (var context = new MyDbContext(options)) { await context.Database.EnsureCreatedAsync(); + 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 }; + + var Dieu = new ChampionEntity { Name = name, - Bio = bio + Bio = bio, + Skins = new Collection(new List { black, white, green }) }; ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); @@ -45,6 +52,7 @@ namespace TestUnitaire Assert.Equal(1, await context.Champions.CountAsync()); Assert.Equal(name, found.Name); + Assert.Equal(3,found.Skins.Count); // Test if the max length of the name is respected (30) and the max length of the bio is respected (256) if (expected) @@ -54,8 +62,8 @@ namespace TestUnitaire } else { - Assert.False(found.Bio.Length <= 256); Assert.False(found.Name.Length <= 30); + Assert.False(found.Bio.Length <= 256); } } @@ -113,5 +121,52 @@ namespace TestUnitaire Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); } } - } + + [Theory] + + [InlineData("black", "super Skin", "icon1.png",190000000.2f,true)] + [InlineData("White", "skin1", "icon1", 19, true)] + [InlineData("Green", "skin", "icon1.jpg", -1, false)] + public async Task TestAddSkinToChampionInMemory(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 + { + Name = "Zeus", + Bio = "Dieu de la foudre", + Skins = new Collection() + }; + + SkinEntity item = new SkinEntity + { + 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 From ff581216885a0dc8e04891f291fbabbee5745fcf Mon Sep 17 00:00:00 2001 From: "bastien.ollier@gmail.com" Date: Wed, 1 Mar 2023 16:48:04 +0100 Subject: [PATCH 02/14] add test id chmapion --- Sources/TestUnitaire/TestEfLol.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index 21851dd..4a8c3a5 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -10,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 TestAddInMemory(int id, String name, String bio, bool expected) { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); @@ -36,6 +36,7 @@ namespace TestUnitaire var Dieu = new ChampionEntity { + Id = id, Name = name, Bio = bio, Skins = new Collection(new List { black, white, green }) @@ -85,9 +86,9 @@ namespace TestUnitaire //context.Database.OpenConnection(); context.Database.EnsureCreated(); - 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 chewie = new ChampionEntity {Id = 0, Name = "Chewbacca", Bio = "Zeus is the king of the gods." }; + ChampionEntity yoda = new ChampionEntity {Id = 1, Name = "Yoda", Bio = "Zeus is the king of the gods." }; + ChampionEntity ewok = new ChampionEntity { Id = 2, Name = "Ewok", Bio = "Zeus is the king of the gods." }; context.Champions.Add(chewie); @@ -142,6 +143,7 @@ namespace TestUnitaire var Dieu = new ChampionEntity { + Id = 0, Name = "Zeus", Bio = "Dieu de la foudre", Skins = new Collection() From 98b092e1301b25802dcfd2b85fa6caa245aa963a Mon Sep 17 00:00:00 2001 From: "bastien.ollier@gmail.com" Date: Wed, 1 Mar 2023 16:49:25 +0100 Subject: [PATCH 03/14] add test id skin --- Sources/TestUnitaire/TestEfLol.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index 4a8c3a5..26c10b1 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -125,10 +125,10 @@ namespace TestUnitaire [Theory] - [InlineData("black", "super Skin", "icon1.png",190000000.2f,true)] - [InlineData("White", "skin1", "icon1", 19, true)] - [InlineData("Green", "skin", "icon1.jpg", -1, false)] - public async Task TestAddSkinToChampionInMemory(String name, String description, String icon,float price,bool expected) + [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(); @@ -151,6 +151,7 @@ namespace TestUnitaire SkinEntity item = new SkinEntity { + Id = id, Name = name, Description = description, Icon = icon, From 2419cb1e9fc349ecd3a4e79e3adac727ccf04fb7 Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Wed, 1 Mar 2023 16:50:43 +0100 Subject: [PATCH 04/14] RuneEntity --- Sources/EFLol/ChampionEntity.cs | 2 +- Sources/EFLol/MyDbContext.cs | 2 ++ Sources/EFLol/Program.cs | 14 ++++++-------- Sources/EFLol/RuneEntity.cs | 19 +++++++++++++++++++ 4 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 Sources/EFLol/RuneEntity.cs diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index b23a534..7cdd4da 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -18,6 +18,6 @@ namespace EFLol //public LargeImage Image { get; set; } //public ReadOnlyDictionary Characteristics { get; private set; } //private HashSet skills = new HashSet(); - public ReadOnlyCollection Skins { get; set; } + public Collection Skins { get; set; } } } diff --git a/Sources/EFLol/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index 17ad759..8735e18 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -11,6 +11,7 @@ namespace EFLol { public DbSet Champions { get; set; } public DbSet Skins { get; set; } + public DbSet Runes { get; set; } public MyDbContext() { } @@ -32,6 +33,7 @@ namespace EFLol base.OnModelCreating(modelBuilder); modelBuilder.Entity().Property(c => c.Id).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + } } } diff --git a/Sources/EFLol/Program.cs b/Sources/EFLol/Program.cs index fbac836..a7a69de 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -18,14 +18,14 @@ ChampionEntity Zeus = new ChampionEntity { Name = "Zeus", Bio = "Zeus is the god of the sky", - Skins = new ReadOnlyCollection(new List { black, white }) + Skins = new Collection(new List { black, white }) }; ChampionEntity Hera = new ChampionEntity { Name = "Hera", Bio = "Hera is the goddess of marriage", - Skins = new ReadOnlyCollection(new List { green }) + Skins = new Collection(new List { green }) }; ChampionEntity Poseidon = new ChampionEntity { Name = "Poseidon", Bio = "Poseidon is the god of the sea" }; @@ -34,7 +34,6 @@ ChampionEntity Poseidon = new ChampionEntity { Name = "Poseidon", Bio = "Poseido 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); @@ -47,12 +46,11 @@ 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; + // LINQ to select the skins of the current champion + var skins = context.Champions.Where(c => c.Id == n.Id).SelectMany(c => c.Skins); - Console.WriteLine($"Champion n°{n.Id} - {n.Name}"); + // Display the champion and its skins + Console.WriteLine($"Champion n°{n.Id} - {n.Name} : {skins.Count()} skins"); } context.SaveChanges(); } diff --git a/Sources/EFLol/RuneEntity.cs b/Sources/EFLol/RuneEntity.cs new file mode 100644 index 0000000..3f099dc --- /dev/null +++ b/Sources/EFLol/RuneEntity.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +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 RuneFamily Family { get; set; } + //public string Icon { get; set; } + //public string Image { get; set; } + + } +} From 85697ac6c0c0f8bc5a95a6647df0936906fb7145 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@gmail.com" Date: Wed, 1 Mar 2023 16:50:48 +0100 Subject: [PATCH 05/14] modif test update champion --- Sources/TestUnitaire/TestEfLol.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index 26c10b1..33fffb9 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -86,9 +86,9 @@ namespace TestUnitaire //context.Database.OpenConnection(); context.Database.EnsureCreated(); - ChampionEntity chewie = new ChampionEntity {Id = 0, Name = "Chewbacca", Bio = "Zeus is the king of the gods." }; - ChampionEntity yoda = new ChampionEntity {Id = 1, Name = "Yoda", Bio = "Zeus is the king of the gods." }; - ChampionEntity ewok = new ChampionEntity { Id = 2, Name = "Ewok", Bio = "Zeus is the king of the gods." }; + 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." }; context.Champions.Add(chewie); From 56dfa7a5ce15c7ea20bd6ff08801c45fdcdcffe2 Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Wed, 1 Mar 2023 17:32:43 +0100 Subject: [PATCH 06/14] =?UTF-8?q?D=C3=A9but=20Many=20to=20Many?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/EFLol/ChampionEntity.cs | 5 ++++- Sources/EFLol/RuneEntity.cs | 4 +--- Sources/EFLol/RuneFamilyEntity.cs | 15 +++++++++++++++ Sources/EFLol/RunePageEntity.cs | 25 +++++++++++++++++++++++++ Sources/EFLol/SkillEntity.cs | 24 ++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 Sources/EFLol/RuneFamilyEntity.cs create mode 100644 Sources/EFLol/RunePageEntity.cs create mode 100644 Sources/EFLol/SkillEntity.cs diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index 7cdd4da..b73f713 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -17,7 +17,10 @@ namespace EFLol //public string Icon { get; set; } //public LargeImage Image { get; set; } //public ReadOnlyDictionary Characteristics { get; private set; } - //private HashSet skills = new HashSet(); + + private HashSet skills = new HashSet(); public Collection Skins { get; set; } + + //public List> championsAndRunePages } } diff --git a/Sources/EFLol/RuneEntity.cs b/Sources/EFLol/RuneEntity.cs index 3f099dc..934762a 100644 --- a/Sources/EFLol/RuneEntity.cs +++ b/Sources/EFLol/RuneEntity.cs @@ -11,9 +11,7 @@ namespace EFLol public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } - //public RuneFamily Family { get; set; } - //public string Icon { get; set; } - //public string Image { get; set; } + public RuneFamilyEntity RuneFamily { get; set; } } } diff --git a/Sources/EFLol/RuneFamilyEntity.cs b/Sources/EFLol/RuneFamilyEntity.cs new file mode 100644 index 0000000..47618b8 --- /dev/null +++ b/Sources/EFLol/RuneFamilyEntity.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EFLol +{ + public class RuneFamilyEntity + { + public string Name { get; set; } + public string Description { get; set; } + + } +} diff --git a/Sources/EFLol/RunePageEntity.cs b/Sources/EFLol/RunePageEntity.cs new file mode 100644 index 0000000..04fe543 --- /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 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..1f670d1 --- /dev/null +++ b/Sources/EFLol/SkillEntity.cs @@ -0,0 +1,24 @@ +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 enum SkillTypeEntity + { + Unknown, + Basic, + Passive, + Ultimate + } +} From 3465261b700a8032fe4eaa4460b858de35581dc2 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Sat, 11 Mar 2023 11:16:33 +0100 Subject: [PATCH 07/14] one to many : championEntity -> RunePageEntity --- Sources/EFLol/ChampionEntity.cs | 7 +- Sources/EFLol/MyDbContext.cs | 18 ++-- Sources/EFLol/Program.cs | 134 ++++++++++++++++-------------- Sources/EFLol/RuneEntity.cs | 19 ++++- Sources/EFLol/RuneFamilyEntity.cs | 15 ---- Sources/EFLol/RunePageEntity.cs | 8 +- 6 files changed, 107 insertions(+), 94 deletions(-) delete mode 100644 Sources/EFLol/RuneFamilyEntity.cs diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index b73f713..10e36fe 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -19,8 +19,7 @@ namespace EFLol //public ReadOnlyDictionary Characteristics { get; private set; } private HashSet skills = new HashSet(); - public Collection Skins { get; set; } - - //public List> championsAndRunePages - } + public Collection Skins { get; set; } + public Collection ListRunePages { get; set; } +} } diff --git a/Sources/EFLol/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index 8735e18..7358097 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -11,9 +11,10 @@ namespace EFLol { public DbSet Champions { get; set; } public DbSet Skins { get; set; } - public DbSet Runes { get; set; } - - public MyDbContext() + public DbSet Runes { get; set; } + public DbSet RunePages { get; set; } + + public MyDbContext() { } public MyDbContext(DbContextOptions options) @@ -32,8 +33,11 @@ namespace EFLol { 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().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 a7a69de..89b837e 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -1,65 +1,77 @@ -using System.Collections.ObjectModel; +using System.Collections.Generic; +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 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" }; - - - -using (var context = new MyDbContext()) -{ - 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) - { - // LINQ to select the skins of the current champion - var skins = context.Champions.Where(c => c.Id == n.Id).SelectMany(c => c.Skins); - - // Display the champion and its skins - Console.WriteLine($"Champion n°{n.Id} - {n.Name} : {skins.Count()} skins"); - } - 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(); +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 } }) + }; + + using (var context = new MyDbContext()) + { + 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) + { + // 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 index 934762a..1bd31e3 100644 --- a/Sources/EFLol/RuneEntity.cs +++ b/Sources/EFLol/RuneEntity.cs @@ -11,7 +11,20 @@ namespace EFLol public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } - public RuneFamilyEntity RuneFamily { get; set; } - - } + public RuneFamilyEntity RuneFamily { get; set; } + + public static implicit operator Dictionary(RuneEntity v) + { + throw new NotImplementedException(); + } + } + + public enum RuneFamilyEntity + { + Unknown, + Precision, + Domination + } + } + diff --git a/Sources/EFLol/RuneFamilyEntity.cs b/Sources/EFLol/RuneFamilyEntity.cs deleted file mode 100644 index 47618b8..0000000 --- a/Sources/EFLol/RuneFamilyEntity.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EFLol -{ - public class RuneFamilyEntity - { - public string Name { get; set; } - public string Description { get; set; } - - } -} diff --git a/Sources/EFLol/RunePageEntity.cs b/Sources/EFLol/RunePageEntity.cs index 04fe543..144f7dd 100644 --- a/Sources/EFLol/RunePageEntity.cs +++ b/Sources/EFLol/RunePageEntity.cs @@ -7,10 +7,10 @@ using System.Threading.Tasks; namespace EFLol { public class RunePageEntity - { - public String Name { get; set; } - public Dictionary Runes { get; set; } - + { + public int Id { get; set; } + public String Name { get; set; } + //public Dictionary Runes { get; set; } } public enum Category From 3e78ebb015d95bee0773ef2875af0e0fd349b079 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Sat, 11 Mar 2023 11:34:52 +0100 Subject: [PATCH 08/14] correction --- Sources/EFLol/MyDbContext.cs | 7 ++--- Sources/EFLol/RuneEntity.cs | 7 ++--- Sources/EFLol/RunePageEntity.cs | 46 ++++++++++++++++----------------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/Sources/EFLol/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index 7358097..a339870 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -10,9 +10,9 @@ namespace EFLol public class MyDbContext : DbContext { public DbSet Champions { get; set; } - public DbSet Skins { get; set; } - public DbSet Runes { get; set; } + public DbSet Skins { get; set; } public DbSet RunePages { get; set; } + public DbSet Runes { get; set; } public MyDbContext() { } @@ -36,7 +36,8 @@ namespace EFLol modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); - modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + modelBuilder.Entity().HasKey(sc => new { sc.StudentId, sc.CourseId }); modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); } } diff --git a/Sources/EFLol/RuneEntity.cs b/Sources/EFLol/RuneEntity.cs index 1bd31e3..6b492fb 100644 --- a/Sources/EFLol/RuneEntity.cs +++ b/Sources/EFLol/RuneEntity.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -12,11 +13,7 @@ namespace EFLol public string Name { get; set; } public string Description { get; set; } public RuneFamilyEntity RuneFamily { get; set; } - - public static implicit operator Dictionary(RuneEntity v) - { - throw new NotImplementedException(); - } + public Collection ListRunePages { get; set; } } public enum RuneFamilyEntity diff --git a/Sources/EFLol/RunePageEntity.cs b/Sources/EFLol/RunePageEntity.cs index 144f7dd..4649f76 100644 --- a/Sources/EFLol/RunePageEntity.cs +++ b/Sources/EFLol/RunePageEntity.cs @@ -1,25 +1,25 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EFLol -{ - public class RunePageEntity +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 - } -} + public String Name { get; set; } + //public Dictionary Runes { get; set; } + } + + public enum Category + { + Major, + Minor1, + Minor2, + Minor3, + OtherMinor1, + OtherMinor2 + } +} From 10590639f4e7276b2c7c956e1140497e8409072e Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Sat, 11 Mar 2023 11:36:00 +0100 Subject: [PATCH 09/14] oups --- Sources/EFLol/MyDbContext.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/EFLol/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index a339870..55f4036 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -37,7 +37,6 @@ namespace EFLol modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); - modelBuilder.Entity().HasKey(sc => new { sc.StudentId, sc.CourseId }); modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); } } From e7f6d20133968ff2a21e806beec7cbc1f4977320 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Sat, 11 Mar 2023 11:42:03 +0100 Subject: [PATCH 10/14] add test championEntity avec RunePageEntity --- Sources/TestUnitaire/TestEfLol.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index 33fffb9..af46a29 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -33,14 +33,16 @@ namespace TestUnitaire 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 + var Dieu = new ChampionEntity { Id = id, Name = name, Bio = bio, - Skins = new Collection(new List { black, white, green }) - }; + 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); @@ -54,9 +56,10 @@ 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); From 747a1e08499e8ebe22bfb2949cbf263830598ef4 Mon Sep 17 00:00:00 2001 From: "nathan.boileau" Date: Wed, 15 Mar 2023 16:08:35 +0100 Subject: [PATCH 11/14] rename test unitaire --- Sources/TestUnitaire/TestEfLol.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index af46a29..02244e0 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -17,7 +17,7 @@ namespace TestUnitaire "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(int id, 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(); @@ -74,7 +74,7 @@ namespace TestUnitaire } [Fact] - public void ModifyTestInMemory() + public void TestModifyChampionInMemory() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); From 3b61dbe8cfd00780c457ae53ca544d242170e7c7 Mon Sep 17 00:00:00 2001 From: baollier1 Date: Wed, 15 Mar 2023 16:31:24 +0100 Subject: [PATCH 12/14] add many to many with champions skill --- Sources/EFLol/ChampionEntity.cs | 18 +++++---- Sources/EFLol/MyDbContext.cs | 69 +++++++++++++++++---------------- Sources/EFLol/Program.cs | 36 +++++++++++++++-- Sources/EFLol/SkillEntity.cs | 10 +++-- 4 files changed, 84 insertions(+), 49 deletions(-) diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index 10e36fe..39c0042 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -11,14 +11,16 @@ 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 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/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index 55f4036..7416c4d 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -1,43 +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; } +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(); + 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 89b837e..6720439 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; -using System.Collections.ObjectModel; -using EFLol; - - +using System.Collections.ObjectModel; +using EFLol; + + internal class Program { private static void Main(string[] args) @@ -15,10 +15,12 @@ internal class Program 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" }; @@ -32,12 +34,38 @@ internal class Program 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(); } diff --git a/Sources/EFLol/SkillEntity.cs b/Sources/EFLol/SkillEntity.cs index 1f670d1..c9c90ee 100644 --- a/Sources/EFLol/SkillEntity.cs +++ b/Sources/EFLol/SkillEntity.cs @@ -11,10 +11,12 @@ namespace EFLol public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } - public SkillTypeEntity SkillType { get; set; } - } - - public enum SkillTypeEntity + public SkillTypeEntity SkillType { get; set; } + public HashSet champions { get; set; } + + } + + public enum SkillTypeEntity { Unknown, Basic, From 569cc5214629ef7d0f2dd953d795def8a5b23070 Mon Sep 17 00:00:00 2001 From: "nathan.boileau" Date: Wed, 15 Mar 2023 17:21:54 +0100 Subject: [PATCH 13/14] add: Info sur lien entre API et EF --- Sources/EFLol/EFDataManager.cs | 131 +++++++++++++++++++++++++++++++++ Sources/EFLol/EFLol.csproj | 5 ++ 2 files changed, 136 insertions(+) create mode 100644 Sources/EFLol/EFDataManager.cs diff --git a/Sources/EFLol/EFDataManager.cs b/Sources/EFLol/EFDataManager.cs new file mode 100644 index 0000000..66a7efc --- /dev/null +++ b/Sources/EFLol/EFDataManager.cs @@ -0,0 +1,131 @@ +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EFLol +{ + public class EFDataManager : IDataManager + { + // Connect to the MyDbContext class + private readonly MyDbContext _context; + + // Constructor + public EFDataManager(MyDbContext context) + { + _context = context; + } + + public IChampionsManager ChampionsMgr => throw new NotImplementedException(); + + public ISkinsManager SkinsMgr => throw new NotImplementedException(); + + public IRunesManager RunesMgr => throw new NotImplementedException(); + + public IRunePagesManager RunePagesMgr => throw new NotImplementedException(); + } + + public class EFChampionManager : IChampionsManager + { + private readonly MyDbContext _context; + + public EFChampionManager(MyDbContext context) + { + _context = context; + } + + public Task AddItem(Champion? item) + { + // 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 + throw new NotImplementedException(); + } + + + public Task DeleteItem(Champion? item) + { + throw new NotImplementedException(); + } + public Task UpdateItem(Champion? oldItem, Champion? newItem) + { + throw new NotImplementedException(); + } + + public Task GetNbItems() + { + throw new NotImplementedException(); + } + + public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool @descending = false) + { + throw new NotImplementedException(); + } + + 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 @@ + + + + + From 602b05abdbb63aa0b8b174af205db3e5c21fc702 Mon Sep 17 00:00:00 2001 From: "nathan.boileau" Date: Wed, 22 Mar 2023 16:56:56 +0100 Subject: [PATCH 14/14] :construction: Beginning of link between DB and API --- Sources/EFLol/DBDataManager/DataConverter.cs | 26 ++++ Sources/EFLol/DBDataManager/EFDataManager.cs | 132 ++++++++++++++++++ Sources/EFLol/EFDataManager.cs | 131 ----------------- Sources/Model/Champion.cs | 8 +- .../apiLOL/Controllers/ControllerChampions.cs | 19 +-- Sources/apiLOL/Program.cs | 5 +- Sources/apiLOL/apiLOL.csproj | 1 + 7 files changed, 180 insertions(+), 142 deletions(-) create mode 100644 Sources/EFLol/DBDataManager/DataConverter.cs create mode 100644 Sources/EFLol/DBDataManager/EFDataManager.cs delete mode 100644 Sources/EFLol/EFDataManager.cs 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/EFDataManager.cs b/Sources/EFLol/EFDataManager.cs deleted file mode 100644 index 66a7efc..0000000 --- a/Sources/EFLol/EFDataManager.cs +++ /dev/null @@ -1,131 +0,0 @@ -using Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EFLol -{ - public class EFDataManager : IDataManager - { - // Connect to the MyDbContext class - private readonly MyDbContext _context; - - // Constructor - public EFDataManager(MyDbContext context) - { - _context = context; - } - - public IChampionsManager ChampionsMgr => throw new NotImplementedException(); - - public ISkinsManager SkinsMgr => throw new NotImplementedException(); - - public IRunesManager RunesMgr => throw new NotImplementedException(); - - public IRunePagesManager RunePagesMgr => throw new NotImplementedException(); - } - - public class EFChampionManager : IChampionsManager - { - private readonly MyDbContext _context; - - public EFChampionManager(MyDbContext context) - { - _context = context; - } - - public Task AddItem(Champion? item) - { - // 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 - throw new NotImplementedException(); - } - - - public Task DeleteItem(Champion? item) - { - throw new NotImplementedException(); - } - public Task UpdateItem(Champion? oldItem, Champion? newItem) - { - throw new NotImplementedException(); - } - - public Task GetNbItems() - { - throw new NotImplementedException(); - } - - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool @descending = false) - { - throw new NotImplementedException(); - } - - 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/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/apiLOL/Controllers/ControllerChampions.cs b/Sources/apiLOL/Controllers/ControllerChampions.cs index 5e129e5..20e85b6 100644 --- a/Sources/apiLOL/Controllers/ControllerChampions.cs +++ b/Sources/apiLOL/Controllers/ControllerChampions.cs @@ -11,12 +11,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; } @@ -28,10 +29,10 @@ namespace apiLOL.Controllers //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 data.ChampionsMgr.GetNbItems(); + int nbChampions = await _dataManager.GetNbItems(); _logger.LogInformation($"Nombre de champions : {nbChampions}"); - var champs = (await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ToDTO()); + var champs = (await _dataManager.GetItems(index, count)).Select(Model => Model.ToDTO()); var page = new ChampionPageDTO { @@ -52,7 +53,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 @@ -72,7 +73,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); } @@ -90,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()); } @@ -106,8 +107,8 @@ namespace apiLOL.Controllers { try { - var champ = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First(); - data.ChampionsMgr.DeleteItem(champ); + var champ = (await _dataManager.GetItemsByName(name, 0, 1)).First(); + _dataManager.DeleteItem(champ); return Ok(champ.ToDTO()); } catch diff --git a/Sources/apiLOL/Program.cs b/Sources/apiLOL/Program.cs index 1a8abfa..ec1f140 100644 --- a/Sources/apiLOL/Program.cs +++ b/Sources/apiLOL/Program.cs @@ -1,3 +1,4 @@ +using EFLol.DBDataManager; using Model; using StubLib; @@ -10,7 +11,8 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddSingleton(); +//builder.Services.AddSingleton(); +builder.Services.AddScoped(); var app = builder.Build(); @@ -21,6 +23,7 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } + app.UseHttpsRedirection(); app.UseAuthorization(); diff --git a/Sources/apiLOL/apiLOL.csproj b/Sources/apiLOL/apiLOL.csproj index b90f766..d8a99b3 100644 --- a/Sources/apiLOL/apiLOL.csproj +++ b/Sources/apiLOL/apiLOL.csproj @@ -20,6 +20,7 @@ +