From 25e375f1a9ca08165c3b87315cc1f4d20c2e877e Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Wed, 22 Feb 2023 17:30:13 +0100 Subject: [PATCH] Add skin entity for the champions --- .gitignore | 2 + Sources/EFLol/ChampionEntity.cs | 17 ++++---- Sources/EFLol/MyDbContext.cs | 8 ++++ Sources/EFLol/Program.cs | 72 +++++++++++++++++++++++---------- Sources/EFLol/SkinEntity.cs | 7 ++-- 5 files changed, 74 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 5913f3d..1132ce9 100644 --- a/.gitignore +++ b/.gitignore @@ -428,5 +428,7 @@ FodyWeavers.xsd *.sln.iml *.db +*.db-shm +*.db-wal *Migrations *.sonarqube \ No newline at end of file diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index 1e09902..b23a534 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -1,4 +1,5 @@ -using System.ComponentModel.DataAnnotations; +using System.Collections.ObjectModel; +using System.ComponentModel.DataAnnotations; namespace EFLol { @@ -12,11 +13,11 @@ namespace EFLol [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 ReadOnlyCollection Skins { get; private set; } - //public ReadOnlyDictionary Characteristics { get; private set; } - //private HashSet skills = new HashSet(); - } + //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; } + } } diff --git a/Sources/EFLol/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index 547fb9c..17ad759 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -10,6 +10,7 @@ namespace EFLol public class MyDbContext : DbContext { public DbSet Champions { get; set; } + public DbSet Skins { get; set; } public MyDbContext() { } @@ -25,5 +26,12 @@ namespace EFLol 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(); + } } } diff --git a/Sources/EFLol/Program.cs b/Sources/EFLol/Program.cs index 4fd61f2..fbac836 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -1,37 +1,67 @@ -using EFLol; +using System.Collections.ObjectModel; +using EFLol; -/*ChampionEntity Zeus = new ChampionEntity + +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 king of the gods." + 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 }) -using (var context = new ChampionContext()) +}; +ChampionEntity Poseidon = new ChampionEntity { Name = "Poseidon", Bio = "Poseidon is the god of the sea" }; + + + +using (var context = new MyDbContext()) { - Console.WriteLine("Adding Zeus to the database..."); - context.Champions.Add(Zeus); + // 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(); -}*/ +} -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." }; using (var context = new MyDbContext()) { - // Crée des nounours et les insère dans la base - Console.WriteLine("Creates and inserts new Nounours"); - context.Add(chewie); - context.Add(yoda); - context.Add(ewok); - context.SaveChanges(); + 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.Champions) - { - Console.WriteLine($"{n.Id} - {n.Name}"); - } - context.SaveChanges(); + foreach (var n in context.Skins) + { + Console.WriteLine($"Skin n°{n.Id} - {n.Name}" + (n.Price != null ? $" - {n.Price}" : "")); + } + context.SaveChanges(); } \ No newline at end of file diff --git a/Sources/EFLol/SkinEntity.cs b/Sources/EFLol/SkinEntity.cs index f186268..125d962 100644 --- a/Sources/EFLol/SkinEntity.cs +++ b/Sources/EFLol/SkinEntity.cs @@ -8,9 +8,10 @@ namespace EFLol { public class SkinEntity { - public String Name { get; set; } - public String Description { get; set; } - public String Icon { get; set; } + public int Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public string Icon { get; set; } public float Price { get; set; } } }