one to many skins fini

master
Jolys Enzo 2 years ago
parent b8cd36d890
commit aa57c35e5f

@ -24,9 +24,9 @@ namespace Api_lol.Controllers
[HttpGet]
public async Task<IActionResult> 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]

@ -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");

@ -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();
}
}
}

@ -16,20 +16,36 @@ namespace EntityFramwork
//création de la table Champion
modelBuilder.Entity<EntityChampions>().HasKey(a => a.Id);
modelBuilder.Entity<EntityChampions>().Property(a => a.Id)
.ValueGeneratedOnAdd();
.ValueGeneratedOnAdd();
modelBuilder.Entity<EntityChampions>().HasIndex(a => a.Name)
.IsUnique(true);
//Clé avec skins
modelBuilder.Entity<EntityChampions>()
.HasMany(e => e.Skins)
.WithOne(e => e.Champion)
.HasForeignKey(e => e.ChampionForeignKey);
//Clé avec Images
modelBuilder.Entity<EntityChampions>().HasOne(n => n.Image)
.WithOne(c => c.Champion)
.HasForeignKey<EntityLargeImage>(c => c.Champion);
// -------------------------------------------------------------------------------//
//création de la table Skins
modelBuilder.Entity<EntitySkins>().HasKey(m => m.Id);
modelBuilder.Entity<EntitySkins>().Property(m => m.Id)
.ValueGeneratedOnAdd();
//modelBuilder.Entity<EntitySkins>().Property<int>("ChampionForeignKey");
// Use the shadow property as a foreign key
/*
modelBuilder.Entity<EntitySkins>()
.HasOne(m => m.Champion)
.WithMany(a => a.Skins)
.HasForeignKey("ChampionsForeignKey");*/
modelBuilder.Entity<EntityChampions>()
.HasMany(e => e.Skins)
.WithOne(e => e.Champion)
.HasForeignKey(e => e.ChampionForeignKey);
// -------------------------------------------------------------------------------//
//création de la table Images
modelBuilder.Entity<EntityLargeImage>().HasKey(c => c.Id);
modelBuilder.Entity<EntityLargeImage>().Property(c => c.Id)
.ValueGeneratedOnAdd();
}
public DbSet<EntityChampions> Champions { get; set; }

@ -20,5 +20,7 @@ namespace DTO
public List<EntitySkins> Skins { get; set; }
public EntityLargeImage Image { get; set; }
}
}

@ -21,8 +21,4 @@
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project>

@ -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; }
}
}

@ -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; }
}

@ -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;
}

@ -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
{
/// <inheritdoc />
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<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Base64")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Images");
});
modelBuilder.Entity("EntityFramwork.EntitySkins", b =>
{
b.Property<int>("Id")

@ -5,7 +5,7 @@
namespace EntityFramwork.Migrations
{
/// <inheritdoc />
public partial class testMigra : Migration
public partial class test : Migration
{
/// <inheritdoc />
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<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Base64 = table.Column<string>(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
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Images");
migrationBuilder.DropTable(
name: "Skins");

@ -0,0 +1,108 @@
// <auto-generated />
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
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("DTO.EntityChampions", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Bio")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Champions");
});
modelBuilder.Entity("EntityFramwork.EntityLargeImage", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Base64")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Images");
});
modelBuilder.Entity("EntityFramwork.EntitySkins", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("ChampionId")
.HasColumnType("INTEGER");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<float>("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
}
}
}

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFramwork.Migrations
{
/// <inheritdoc />
public partial class test2 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

@ -0,0 +1,108 @@
// <auto-generated />
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
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("DTO.EntityChampions", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Bio")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Champions");
});
modelBuilder.Entity("EntityFramwork.EntityLargeImage", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Base64")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Images");
});
modelBuilder.Entity("EntityFramwork.EntitySkins", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("ChampionForeignKey")
.HasColumnType("INTEGER");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<float>("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
}
}
}

@ -0,0 +1,62 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFramwork.Migrations
{
/// <inheritdoc />
public partial class test3 : Migration
{
/// <inheritdoc />
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);
}
/// <inheritdoc />
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);
}
}
}

@ -36,16 +36,34 @@ namespace EntityFramwork.Migrations
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Champions");
});
modelBuilder.Entity("EntityFramwork.EntityLargeImage", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Base64")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Images");
});
modelBuilder.Entity("EntityFramwork.EntitySkins", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("ChampionId")
b.Property<int>("ChampionForeignKey")
.HasColumnType("INTEGER");
b.Property<string>("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();

Loading…
Cancel
Save