diff --git a/Sources/Api-lol/Controllers/Skins.cs b/Sources/Api-lol/Controllers/Skins.cs index 0d1d7cb..55152ce 100644 --- a/Sources/Api-lol/Controllers/Skins.cs +++ b/Sources/Api-lol/Controllers/Skins.cs @@ -24,9 +24,9 @@ namespace Api_lol.Controllers [HttpGet] public async Task Get() { - var champs = (await data.SkinsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Select(Model => Model.ModelToDto()); + var skins = (await data.SkinsMgr.GetItems(0, await data.SkinsMgr.GetNbItems())).Select(Model => Model.ModelToDto()); - return Ok(champs); + return Ok(skins); } [HttpGet] diff --git a/Sources/Client/Program.cs b/Sources/Client/Program.cs index eed9cc0..28c9b3e 100644 --- a/Sources/Client/Program.cs +++ b/Sources/Client/Program.cs @@ -1,6 +1,7 @@ // See https://aka.ms/new-console-template for more information +using Api_lol.Factories; using Client; using DTO; using EntityFramwork; @@ -10,16 +11,8 @@ using StubLib; StubData tmp = new StubData(); -var tmpListe = await tmp.ChampionsMgr.GetItemsByName("Akali", 0, 6); -Champion champ = tmpListe.First(); -Factories facto = new Factories(); - - -using ( BDDContext db = new BDDContext()) -{ - - db.SaveChanges(); -} +StubEntityInit dbStub = new StubEntityInit(); +dbStub.Init(); /* Console.WriteLine("Start"); diff --git a/Sources/Client/StubEntityInit.cs b/Sources/Client/StubEntityInit.cs new file mode 100644 index 0000000..874ff27 --- /dev/null +++ b/Sources/Client/StubEntityInit.cs @@ -0,0 +1,61 @@ +using DTO; +using EntityFramwork.Factories; +using EntityFramwork; +using Model; +using StubLib; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Api_lol.Factories; + +namespace Client +{ + public class StubEntityInit + { + private Factories facto = new Factories(); + + + public async void AddAllChampions() + { + StubData data = new StubData(); + var liste = await data.ChampionsMgr.GetItems(0,await data.ChampionsMgr.GetNbItems()); + var listeDto = liste.ToList().Select(model => facto.ChampionModelToEntity(model)); + + using (BDDContext db = new BDDContext()) + { + foreach(var item in listeDto) + { + db.Add(item); + } + db.SaveChanges(); + } + } + + public async void AddAllSkins() + { + StubData data = new StubData(); + var skins = await data.SkinsMgr.GetItems(0, await data.SkinsMgr.GetNbItems()); + //var skinsDto = skins.ToList().Select(model => facto.SkinsModelToEntity(model)); + + using (BDDContext db = new BDDContext()) + { + foreach (var item in skins) + { + int idChampion = (db.Champions.Where(m => m.Name == item.Champion.Name).First()).Id; + EntitySkins skin = facto.SkinsModelToEntity(item,idChampion); + db.Add(skin); + } + db.SaveChanges(); + } + } + + + public void Init() + { + AddAllChampions(); + AddAllSkins(); + } + } +} diff --git a/Sources/EntityFramwork/BDD-APILOL.db b/Sources/EntityFramwork/BDD-APILOL.db index 4d1ae4c..e5854ee 100644 Binary files a/Sources/EntityFramwork/BDD-APILOL.db and b/Sources/EntityFramwork/BDD-APILOL.db differ diff --git a/Sources/EntityFramwork/BDD-APILOL.db-shm b/Sources/EntityFramwork/BDD-APILOL.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/Sources/EntityFramwork/BDD-APILOL.db-shm differ diff --git a/Sources/EntityFramwork/BDD-APILOL.db-wal b/Sources/EntityFramwork/BDD-APILOL.db-wal new file mode 100644 index 0000000..e69de29 diff --git a/Sources/EntityFramwork/BDDContext.cs b/Sources/EntityFramwork/BDDContext.cs index fb9fa44..853d1f7 100644 --- a/Sources/EntityFramwork/BDDContext.cs +++ b/Sources/EntityFramwork/BDDContext.cs @@ -16,20 +16,36 @@ namespace EntityFramwork //création de la table Champion modelBuilder.Entity().HasKey(a => a.Id); modelBuilder.Entity().Property(a => a.Id) - .ValueGeneratedOnAdd(); - + .ValueGeneratedOnAdd(); + modelBuilder.Entity().HasIndex(a => a.Name) + .IsUnique(true); + //Clé avec skins + modelBuilder.Entity() + .HasMany(e => e.Skins) + .WithOne(e => e.Champion) + .HasForeignKey(e => e.ChampionForeignKey); + //Clé avec Images + modelBuilder.Entity().HasOne(n => n.Image) + .WithOne(c => c.Champion) + .HasForeignKey(c => c.Champion); + // -------------------------------------------------------------------------------// //création de la table Skins modelBuilder.Entity().HasKey(m => m.Id); modelBuilder.Entity().Property(m => m.Id) .ValueGeneratedOnAdd(); - //modelBuilder.Entity().Property("ChampionForeignKey"); - // Use the shadow property as a foreign key - /* - modelBuilder.Entity() - .HasOne(m => m.Champion) - .WithMany(a => a.Skins) - .HasForeignKey("ChampionsForeignKey");*/ + modelBuilder.Entity() + .HasMany(e => e.Skins) + .WithOne(e => e.Champion) + .HasForeignKey(e => e.ChampionForeignKey); + + // -------------------------------------------------------------------------------// + //création de la table Images + modelBuilder.Entity().HasKey(c => c.Id); + modelBuilder.Entity().Property(c => c.Id) + .ValueGeneratedOnAdd(); + + } public DbSet Champions { get; set; } diff --git a/Sources/EntityFramwork/EntityChampions.cs b/Sources/EntityFramwork/EntityChampions.cs index 8b878ad..3c8285a 100644 --- a/Sources/EntityFramwork/EntityChampions.cs +++ b/Sources/EntityFramwork/EntityChampions.cs @@ -20,5 +20,7 @@ namespace DTO public List Skins { get; set; } + public EntityLargeImage Image { get; set; } + } } diff --git a/Sources/EntityFramwork/EntityFramwork.csproj b/Sources/EntityFramwork/EntityFramwork.csproj index b4abec4..0275498 100644 --- a/Sources/EntityFramwork/EntityFramwork.csproj +++ b/Sources/EntityFramwork/EntityFramwork.csproj @@ -21,8 +21,4 @@ - - - - diff --git a/Sources/EntityFramwork/EntityLargeImage.cs b/Sources/EntityFramwork/EntityLargeImage.cs index a5f291c..cf892b8 100644 --- a/Sources/EntityFramwork/EntityLargeImage.cs +++ b/Sources/EntityFramwork/EntityLargeImage.cs @@ -1,4 +1,5 @@ -using System; +using DTO; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,8 +9,10 @@ namespace EntityFramwork { public class EntityLargeImage { - public long Id { get; set; } + public int Id { get; set; } public string Base64 { get; set; } + + public EntityChampions Champion { get; set; } } } diff --git a/Sources/EntityFramwork/EntitySkins.cs b/Sources/EntityFramwork/EntitySkins.cs index c956415..c72d4ff 100644 --- a/Sources/EntityFramwork/EntitySkins.cs +++ b/Sources/EntityFramwork/EntitySkins.cs @@ -2,6 +2,7 @@ using Model; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; @@ -18,6 +19,8 @@ namespace EntityFramwork public float Price { get; set; } + public int ChampionForeignKey { get; set; } + [ForeignKey("ChampionForeignKey")] public EntityChampions Champion { get; set; } } diff --git a/Sources/EntityFramwork/Factories/Factories.cs b/Sources/EntityFramwork/Factories/Factories.cs index 6c9f980..557c405 100644 --- a/Sources/EntityFramwork/Factories/Factories.cs +++ b/Sources/EntityFramwork/Factories/Factories.cs @@ -5,29 +5,26 @@ namespace EntityFramwork.Factories { public class Factories { - public EntityChampions ChampionModelToEntity(Champion champ,int sansSkin = 0) + public EntityChampions ChampionModelToEntity(Champion champ) { EntityChampions entity = new EntityChampions(); entity.Name = champ.Name; entity.Bio = champ.Bio; entity.Icon = champ.Icon; - if ( sansSkin == 0) - { - //entity.Skins = champ.Skins.Select(Model => this.SkinsModelToEntity(Model)).ToList(); - } return entity; } - public EntitySkins SkinsModelToEntity(Skin skin) + public EntitySkins SkinsModelToEntity(Skin skin,int id) { EntitySkins entity= new EntitySkins(); entity.Price = skin.Price; entity.Icon = skin.Icon; - entity.Description= skin.Description; - //entity.Champion = ChampionModelToEntity(skin.Champion,1); + entity.Description = skin.Description; + entity.ChampionForeignKey = id; + //entity.Champion = ChampionModelToEntity(skin.Champion); return entity; } diff --git a/Sources/EntityFramwork/Migrations/20230206102420_testMigra.Designer.cs b/Sources/EntityFramwork/Migrations/20230208121743_test.Designer.cs similarity index 81% rename from Sources/EntityFramwork/Migrations/20230206102420_testMigra.Designer.cs rename to Sources/EntityFramwork/Migrations/20230208121743_test.Designer.cs index 4831a4b..60e5442 100644 --- a/Sources/EntityFramwork/Migrations/20230206102420_testMigra.Designer.cs +++ b/Sources/EntityFramwork/Migrations/20230208121743_test.Designer.cs @@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace EntityFramwork.Migrations { [DbContext(typeof(BDDContext))] - [Migration("20230206102420_testMigra")] - partial class testMigra + [Migration("20230208121743_test")] + partial class test { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -39,9 +39,27 @@ namespace EntityFramwork.Migrations b.HasKey("Id"); + b.HasIndex("Name") + .IsUnique(); + b.ToTable("Champions"); }); + modelBuilder.Entity("EntityFramwork.EntityLargeImage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Base64") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Images"); + }); + modelBuilder.Entity("EntityFramwork.EntitySkins", b => { b.Property("Id") diff --git a/Sources/EntityFramwork/Migrations/20230206102420_testMigra.cs b/Sources/EntityFramwork/Migrations/20230208121743_test.cs similarity index 74% rename from Sources/EntityFramwork/Migrations/20230206102420_testMigra.cs rename to Sources/EntityFramwork/Migrations/20230208121743_test.cs index f21f03f..2a337a8 100644 --- a/Sources/EntityFramwork/Migrations/20230206102420_testMigra.cs +++ b/Sources/EntityFramwork/Migrations/20230208121743_test.cs @@ -5,7 +5,7 @@ namespace EntityFramwork.Migrations { /// - public partial class testMigra : Migration + public partial class test : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -25,6 +25,19 @@ namespace EntityFramwork.Migrations table.PrimaryKey("PK_Champions", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Images", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Base64 = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Images", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Skins", columns: table => new @@ -47,6 +60,12 @@ namespace EntityFramwork.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateIndex( + name: "IX_Champions_Name", + table: "Champions", + column: "Name", + unique: true); + migrationBuilder.CreateIndex( name: "IX_Skins_ChampionId", table: "Skins", @@ -56,6 +75,9 @@ namespace EntityFramwork.Migrations /// protected override void Down(MigrationBuilder migrationBuilder) { + migrationBuilder.DropTable( + name: "Images"); + migrationBuilder.DropTable( name: "Skins"); diff --git a/Sources/EntityFramwork/Migrations/20230208121840_test2.Designer.cs b/Sources/EntityFramwork/Migrations/20230208121840_test2.Designer.cs new file mode 100644 index 0000000..70c622a --- /dev/null +++ b/Sources/EntityFramwork/Migrations/20230208121840_test2.Designer.cs @@ -0,0 +1,108 @@ +// +using EntityFramwork; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFramwork.Migrations +{ + [DbContext(typeof(BDDContext))] + [Migration("20230208121840_test2")] + partial class test2 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("DTO.EntityChampions", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Bio") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Champions"); + }); + + modelBuilder.Entity("EntityFramwork.EntityLargeImage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Base64") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Images"); + }); + + modelBuilder.Entity("EntityFramwork.EntitySkins", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ChampionId") + .HasColumnType("INTEGER"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Price") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("ChampionId"); + + b.ToTable("Skins"); + }); + + modelBuilder.Entity("EntityFramwork.EntitySkins", b => + { + b.HasOne("DTO.EntityChampions", "Champion") + .WithMany("Skins") + .HasForeignKey("ChampionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Champion"); + }); + + modelBuilder.Entity("DTO.EntityChampions", b => + { + b.Navigation("Skins"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFramwork/Migrations/20230208121840_test2.cs b/Sources/EntityFramwork/Migrations/20230208121840_test2.cs new file mode 100644 index 0000000..845120e --- /dev/null +++ b/Sources/EntityFramwork/Migrations/20230208121840_test2.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EntityFramwork.Migrations +{ + /// + public partial class test2 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/Sources/EntityFramwork/Migrations/20230208135031_test3.Designer.cs b/Sources/EntityFramwork/Migrations/20230208135031_test3.Designer.cs new file mode 100644 index 0000000..3e06bb3 --- /dev/null +++ b/Sources/EntityFramwork/Migrations/20230208135031_test3.Designer.cs @@ -0,0 +1,108 @@ +// +using EntityFramwork; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFramwork.Migrations +{ + [DbContext(typeof(BDDContext))] + [Migration("20230208135031_test3")] + partial class test3 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("DTO.EntityChampions", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Bio") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Champions"); + }); + + modelBuilder.Entity("EntityFramwork.EntityLargeImage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Base64") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Images"); + }); + + modelBuilder.Entity("EntityFramwork.EntitySkins", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ChampionForeignKey") + .HasColumnType("INTEGER"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Price") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("ChampionForeignKey"); + + b.ToTable("Skins"); + }); + + modelBuilder.Entity("EntityFramwork.EntitySkins", b => + { + b.HasOne("DTO.EntityChampions", "Champion") + .WithMany("Skins") + .HasForeignKey("ChampionForeignKey") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Champion"); + }); + + modelBuilder.Entity("DTO.EntityChampions", b => + { + b.Navigation("Skins"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFramwork/Migrations/20230208135031_test3.cs b/Sources/EntityFramwork/Migrations/20230208135031_test3.cs new file mode 100644 index 0000000..7e99d9f --- /dev/null +++ b/Sources/EntityFramwork/Migrations/20230208135031_test3.cs @@ -0,0 +1,62 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EntityFramwork.Migrations +{ + /// + public partial class test3 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Skins_Champions_ChampionId", + table: "Skins"); + + migrationBuilder.RenameColumn( + name: "ChampionId", + table: "Skins", + newName: "ChampionForeignKey"); + + migrationBuilder.RenameIndex( + name: "IX_Skins_ChampionId", + table: "Skins", + newName: "IX_Skins_ChampionForeignKey"); + + migrationBuilder.AddForeignKey( + name: "FK_Skins_Champions_ChampionForeignKey", + table: "Skins", + column: "ChampionForeignKey", + principalTable: "Champions", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Skins_Champions_ChampionForeignKey", + table: "Skins"); + + migrationBuilder.RenameColumn( + name: "ChampionForeignKey", + table: "Skins", + newName: "ChampionId"); + + migrationBuilder.RenameIndex( + name: "IX_Skins_ChampionForeignKey", + table: "Skins", + newName: "IX_Skins_ChampionId"); + + migrationBuilder.AddForeignKey( + name: "FK_Skins_Champions_ChampionId", + table: "Skins", + column: "ChampionId", + principalTable: "Champions", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs b/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs index 85c3e43..8fb37ab 100644 --- a/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs +++ b/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs @@ -36,16 +36,34 @@ namespace EntityFramwork.Migrations b.HasKey("Id"); + b.HasIndex("Name") + .IsUnique(); + b.ToTable("Champions"); }); + modelBuilder.Entity("EntityFramwork.EntityLargeImage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Base64") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Images"); + }); + modelBuilder.Entity("EntityFramwork.EntitySkins", b => { b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("ChampionId") + b.Property("ChampionForeignKey") .HasColumnType("INTEGER"); b.Property("Description") @@ -61,7 +79,7 @@ namespace EntityFramwork.Migrations b.HasKey("Id"); - b.HasIndex("ChampionId"); + b.HasIndex("ChampionForeignKey"); b.ToTable("Skins"); }); @@ -70,7 +88,7 @@ namespace EntityFramwork.Migrations { b.HasOne("DTO.EntityChampions", "Champion") .WithMany("Skins") - .HasForeignKey("ChampionId") + .HasForeignKey("ChampionForeignKey") .OnDelete(DeleteBehavior.Cascade) .IsRequired();