diff --git a/Sources/APILOL/Mapper/ChampionMapper.cs b/Sources/APILOL/Mapper/ChampionMapper.cs index 4e276ba..a3ede0b 100644 --- a/Sources/APILOL/Mapper/ChampionMapper.cs +++ b/Sources/APILOL/Mapper/ChampionMapper.cs @@ -1,4 +1,5 @@ using DTO; +using EntityFrameworkLOL.DBContexts; using EntityFrameworkLOL.Entities; using Model; @@ -24,21 +25,31 @@ namespace APILOL.Mapper return new Champion(champion.Name, champion.Class, champion.Icon, champion.Image.ToString(), champion.Bio); } - public static ChampionEntity ToEntity(this Champion item) + public static Champion ToModel(this ChampionEntity entity) { - return new() - { - Name = item.Name, - Bio = item.Bio, - Icon = item.Icon, - Class = item.Class, - Image = new() { Base64 = item.Image.Base64 }, - }; + var champion = new Champion(entity.Name, entity.Class, entity.Icon, "", entity.Bio); + if (entity.Skills != null) foreach (var s in entity.Skills) { champion.AddSkill(s.ToModel()); } + if (entity.Characteristics != null) foreach (var c in entity.Characteristics) { champion.AddCharacteristics(c.ToModel()); } + return champion; } - public static Champion ToModel(this ChampionEntity entity) + public static ChampionEntity ToEntity(this Champion item, SQLiteContext context) { - return new(entity.Name, entity.Class, entity.Icon, entity.Image.Base64, entity.Bio); + ChampionEntity? championEntity = context.Champion.Find(item.Name); + if (championEntity == null) + { + championEntity = new() + { + Name = item.Name, + Bio = item.Bio, + Icon = item.Icon, + Class = item.Class, + Image = new() { Base64 = item.Image.Base64 }, + }; + championEntity.Skills = item.Skills.Select(x => x.ToEntity(championEntity, context)).ToList(); + championEntity.Characteristics = item.Characteristics.Select(x => x.ToEntity(championEntity, context)).ToList(); + } + return championEntity; } } -} +} \ No newline at end of file diff --git a/Sources/APILOL/Mapper/CharacteristicsMapper.cs b/Sources/APILOL/Mapper/CharacteristicsMapper.cs new file mode 100644 index 0000000..ce2ca06 --- /dev/null +++ b/Sources/APILOL/Mapper/CharacteristicsMapper.cs @@ -0,0 +1,26 @@ +using EntityFrameworkLOL.DBContexts; +using EntityFrameworkLOL.Entities; + +namespace APILOL.Mapper +{ + public static class CharacteristicsMapper + { + public static CharacteristicEntity ToEntity(this KeyValuePair item, ChampionEntity champion, SQLiteContext context) + { + var characteristicEntity = context.Characteristic.Find(item.Key, champion.Name); + if (characteristicEntity == null) + { + return new() + { + Name = item.Key, + Value = item.Value, + }; + } + return characteristicEntity; + } + + + public static Tuple ToModel(this CharacteristicEntity entity) + => new(entity.Name, entity.Value); + } +} diff --git a/Sources/APILOL/Mapper/RuneMapper.cs b/Sources/APILOL/Mapper/RuneMapper.cs index e1a582b..e280663 100644 --- a/Sources/APILOL/Mapper/RuneMapper.cs +++ b/Sources/APILOL/Mapper/RuneMapper.cs @@ -1,4 +1,6 @@ using DTO; +using EntityFrameworkLOL.DBContexts; +using EntityFrameworkLOL.Entities; using Model; namespace APILOL.Mapper @@ -20,5 +22,25 @@ namespace APILOL.Mapper { return new Rune(rune.Name, rune.Family, rune.Image, rune.Image, rune.Description); } + + public static Rune ToModel(this RuneEntity entity) + { + return new Rune(entity.Name, entity.Family, "", entity.Image.Base64, entity.Description); + } + + public static RuneEntity ToEntity(this Rune item, SQLiteContext context) + { + RuneEntity? runeEntity = context.Rune.Find(item.Name); + if (runeEntity == null) + { + return new() + { + Name = item.Name, + Description = item.Description, + Family = item.Family + }; + } + return runeEntity; + } } } diff --git a/Sources/APILOL/Mapper/RunePageMapper.cs b/Sources/APILOL/Mapper/RunePageMapper.cs index 6bc4de2..b99eafd 100644 --- a/Sources/APILOL/Mapper/RunePageMapper.cs +++ b/Sources/APILOL/Mapper/RunePageMapper.cs @@ -1,4 +1,6 @@ using DTO; +using EntityFrameworkLOL.DBContexts; +using EntityFrameworkLOL.Entities; using Model; namespace APILOL.Mapper @@ -18,5 +20,37 @@ namespace APILOL.Mapper { return new RunePage(runePage.Name); } + + public static RunePage ToModel(this RunePageEntity entity, SQLiteContext context) + { + RunePage runePage = new(entity.Name); + if (entity.Runes != null) + { + foreach (var r in entity.Runes) + { + var rune = context.Rune.Find(r.Name); + //if (rune != null) runePage[r.category] = rune.ToModel(); + }; + } + return runePage; + } + + public static RunePageEntity ToEntity(this RunePage item, SQLiteContext context) + { + RunePageEntity? runePageEntity = context.RunePage.Find(item.Name); + if (runePageEntity == null) + { + runePageEntity = new() + { + Name = item.Name, + }; + runePageEntity.Runes = new List(); + foreach (var r in item.Runes) + { + runePageEntity.Runes.Add(r.Value.ToEntity(context)); + } + } + return runePageEntity; + } } } diff --git a/Sources/APILOL/Mapper/SkillMapper.cs b/Sources/APILOL/Mapper/SkillMapper.cs index f8c0364..3101b6a 100644 --- a/Sources/APILOL/Mapper/SkillMapper.cs +++ b/Sources/APILOL/Mapper/SkillMapper.cs @@ -1,23 +1,31 @@ using DTO; +using EntityFrameworkLOL.DBContexts; +using EntityFrameworkLOL.Entities; using Model; namespace APILOL.Mapper { - public static class SkillMapper + public static class SkillMapper { - public static SkillDTO ToDto(this Skill skill) + public static SkillEntity ToEntity(this Skill item, ChampionEntity champion, SQLiteContext context) { - return new SkillDTO() + var skillEntity = context.Skill.Find(item.Name); + if (skillEntity == null) { - Type = skill.Type, - Name = skill.Name, - Description = skill.Description - }; + return new() + { + Name = item.Name, + Description = item.Description, + Type = item.Type, + Champions = new List() { champion } + }; + } + skillEntity!.Champions?.Add(champion); + return skillEntity; } - public static Skill ToModel(this SkillDTO skill) - { - return new Skill(skill.Name, skill.Type, skill.Description); - } + + public static Skill ToModel(this SkillEntity entity) + => new(entity.Name, entity.Type, entity.Description); } } diff --git a/Sources/APILOL/Mapper/SkinMapper.cs b/Sources/APILOL/Mapper/SkinMapper.cs index 7fc1a2c..eff88cf 100644 --- a/Sources/APILOL/Mapper/SkinMapper.cs +++ b/Sources/APILOL/Mapper/SkinMapper.cs @@ -1,4 +1,6 @@ using DTO; +using EntityFrameworkLOL.DBContexts; +using EntityFrameworkLOL.Entities; using Model; namespace APILOL.Mapper @@ -22,5 +24,23 @@ namespace APILOL.Mapper { return new Skin(skin.Name, skin.Champion.ToModel(),skin.Price, skin.Image, skin.Icon, skin.Description); } + + public static Skin ToModel(this SkinEntity entity) + { + return new Skin(entity.Name, entity.ChampionSkin.ToModel(), entity.Price, entity.Icon, entity.Description); + } + + public static SkinEntity ToEntity(this Skin item, SQLiteContext? context = null) + { + return new() + { + Name = item.Name, + ChampionSkin = context?.Champion.Find(item.Champion.Name) ?? item.Champion.ToEntity(context), + Description = item.Description, + Icon = item.Icon, + Image = null, + Price = item.Price + }; + } } -} +} \ No newline at end of file diff --git a/Sources/EntityFrameworkLOL/DBContexts/SQLiteContext.cs b/Sources/EntityFrameworkLOL/DBContexts/SQLiteContext.cs index 8d9ad94..587ec70 100644 --- a/Sources/EntityFrameworkLOL/DBContexts/SQLiteContext.cs +++ b/Sources/EntityFrameworkLOL/DBContexts/SQLiteContext.cs @@ -1,5 +1,7 @@ using EntityFrameworkLOL.Entities; +using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; using Model; namespace EntityFrameworkLOL.DBContexts @@ -11,20 +13,30 @@ namespace EntityFrameworkLOL.DBContexts public DbSet Skin { get; set; } public DbSet Rune { get; set; } public DbSet RunePage { get; set; } + public DbSet Characteristic { get; set; } public DbSet Champion { get; set; } + public SQLiteContext() { } + + public SQLiteContext(DbContextOptions options) + : base(options) + { } + protected override void OnConfiguring(DbContextOptionsBuilder options) - => options.UseSqlite($"Data Source=DBLOL.db"); + { + //var connection = new SqliteConnection("DataSource=:memory:"); + var connection = new SqliteConnection("Data Source=DBLOL.db"); + connection.Open(); + options.UseSqlite(connection); + } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().Property(i => i.Base64).ValueGeneratedOnAdd(); - modelBuilder.Entity().HasKey(c => new { c.Name, c.Champion }); + modelBuilder.Entity().HasKey(c => new { c.Name }); modelBuilder.Entity().Property(rp => rp.Name).ValueGeneratedOnAdd(); modelBuilder.Entity() - .HasMany(rp => rp.Runes) - .WithMany(r => r.RunePages) - .UsingEntity(); + .HasMany(rp => rp.Runes); modelBuilder.Entity() .HasMany(c => c.RunePages) .WithMany(rp => rp.Champions); @@ -41,13 +53,14 @@ namespace EntityFrameworkLOL.DBContexts Name = "WinKer", Bio = "Best front-end designer", Class = ChampionClass.Mage, - + Icon = "", }, new() { Name = "Jonquille", Bio = "Daffodil", Class = ChampionClass.Support, + Icon = "", } }); modelBuilder.Entity().HasData(new List() { diff --git a/Sources/EntityFrameworkLOL/DBLOL.db b/Sources/EntityFrameworkLOL/DBLOL.db index 5ecb42a..a3995b7 100644 Binary files a/Sources/EntityFrameworkLOL/DBLOL.db and b/Sources/EntityFrameworkLOL/DBLOL.db differ diff --git a/Sources/EntityFrameworkLOL/Entities/ChampionEntity.cs b/Sources/EntityFrameworkLOL/Entities/ChampionEntity.cs index 104744b..d3b0e7d 100644 --- a/Sources/EntityFrameworkLOL/Entities/ChampionEntity.cs +++ b/Sources/EntityFrameworkLOL/Entities/ChampionEntity.cs @@ -12,6 +12,7 @@ using Model; using static System.Net.Mime.MediaTypeNames; using System.Reflection.PortableExecutable; using System.Security.Claims; +using EntityFrameworkLOL.Entities; namespace EntityFrameworkLOL.Entities { @@ -23,17 +24,17 @@ namespace EntityFrameworkLOL.Entities [Required] public string Bio { get; set; } - public string Icon { get; set; } + public string? Icon { get; set; } [Required] public ChampionClass Class { get; set; } - public ImageEntity Image { get; set; } + public ImageEntity? Image { get; set; } public virtual ICollection Skills { get; set; } public virtual ICollection Characteristics { get; set; } - public ICollection RunePages { get; set; } + public virtual ICollection RunePages { get; set; } } } \ No newline at end of file diff --git a/Sources/EntityFrameworkLOL/Entities/CharacteristicEntity.cs b/Sources/EntityFrameworkLOL/Entities/CharacteristicEntity.cs index 7c7da71..ca87641 100644 --- a/Sources/EntityFrameworkLOL/Entities/CharacteristicEntity.cs +++ b/Sources/EntityFrameworkLOL/Entities/CharacteristicEntity.cs @@ -17,7 +17,8 @@ namespace EntityFrameworkLOL.Entities [Required] public int Value { get; set; } - [ForeignKey("ChampionEntity")] public ChampionEntity Champion { get; set; } + + //public virtual ICollection Champions { get; set; } } } \ No newline at end of file diff --git a/Sources/EntityFrameworkLOL/Entities/RuneEntity.cs b/Sources/EntityFrameworkLOL/Entities/RuneEntity.cs index 83bef82..4340ca3 100644 --- a/Sources/EntityFrameworkLOL/Entities/RuneEntity.cs +++ b/Sources/EntityFrameworkLOL/Entities/RuneEntity.cs @@ -21,6 +21,6 @@ namespace EntityFrameworkLOL.Entities [Required] public RuneFamily Family { get; set; } - public ICollection RunePages { get; set; } + public virtual ICollection RunePages { get; set; } } } \ No newline at end of file diff --git a/Sources/EntityFrameworkLOL/Entities/RunePageEntity.cs b/Sources/EntityFrameworkLOL/Entities/RunePageEntity.cs index 6c30ddf..028bfe5 100644 --- a/Sources/EntityFrameworkLOL/Entities/RunePageEntity.cs +++ b/Sources/EntityFrameworkLOL/Entities/RunePageEntity.cs @@ -20,8 +20,8 @@ namespace EntityFrameworkLOL.Entities [Key] public string Name { get; set; } - public ICollection Runes { get; set; } + public virtual ICollection Runes { get; set; } - public ICollection Champions { get; set; } + public virtual ICollection Champions { get; set; } } } \ No newline at end of file diff --git a/Sources/EntityFrameworkLOL/Entities/RunePageRuneEntity.cs b/Sources/EntityFrameworkLOL/Entities/RunePageRuneEntity.cs index 02e256d..3bf8800 100644 --- a/Sources/EntityFrameworkLOL/Entities/RunePageRuneEntity.cs +++ b/Sources/EntityFrameworkLOL/Entities/RunePageRuneEntity.cs @@ -11,5 +11,9 @@ namespace EntityFrameworkLOL.Entities public class RunePageRuneEntity { public Category Category { get; set; } + + public RuneEntity Rune { get; set; } + + public RunePageEntity RunePage { get; set; } } } \ No newline at end of file diff --git a/Sources/EntityFrameworkLOL/Entities/SkinEntity.cs b/Sources/EntityFrameworkLOL/Entities/SkinEntity.cs index eb6496e..05954a1 100644 --- a/Sources/EntityFrameworkLOL/Entities/SkinEntity.cs +++ b/Sources/EntityFrameworkLOL/Entities/SkinEntity.cs @@ -15,7 +15,7 @@ namespace EntityFrameworkLOL.Entities public string Description { get; set; } - public string Icon { get; set; } + public string? Icon { get; set; } public float Price { get; set; } diff --git a/Sources/EntityFrameworkLOL/Migrations/20230315161305_MigrationWallah6.Designer.cs b/Sources/EntityFrameworkLOL/Migrations/20230315161305_MigrationWallah6.Designer.cs new file mode 100644 index 0000000..6dd9313 --- /dev/null +++ b/Sources/EntityFrameworkLOL/Migrations/20230315161305_MigrationWallah6.Designer.cs @@ -0,0 +1,374 @@ +// +using EntityFrameworkLOL.DBContexts; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFrameworkLOL.Migrations +{ + [DbContext(typeof(SQLiteContext))] + [Migration("20230315161305_MigrationWallah6")] + partial class MigrationWallah6 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("ChampionEntityRunePageEntity", b => + { + b.Property("ChampionsName") + .HasColumnType("TEXT"); + + b.Property("RunePagesName") + .HasColumnType("TEXT"); + + b.HasKey("ChampionsName", "RunePagesName"); + + b.HasIndex("RunePagesName"); + + b.ToTable("ChampionEntityRunePageEntity"); + }); + + modelBuilder.Entity("ChampionEntitySkillEntity", b => + { + b.Property("ChampionsName") + .HasColumnType("TEXT"); + + b.Property("SkillsName") + .HasColumnType("TEXT"); + + b.HasKey("ChampionsName", "SkillsName"); + + b.HasIndex("SkillsName"); + + b.ToTable("ChampionEntitySkillEntity"); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.ChampionEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("Bio") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Class") + .HasColumnType("INTEGER"); + + b.Property("Icon") + .HasColumnType("TEXT"); + + b.Property("ImageBase64") + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.HasIndex("ImageBase64"); + + b.ToTable("Champion"); + + b.HasData( + new + { + Name = "WinKer", + Bio = "Best front-end designer", + Class = 3, + Icon = "" + }, + new + { + Name = "Jonquille", + Bio = "Daffodil", + Class = 5, + Icon = "" + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.CharacteristicEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("ChampionName") + .HasColumnType("TEXT"); + + b.Property("Value") + .HasColumnType("INTEGER"); + + b.HasKey("Name"); + + b.HasIndex("ChampionName"); + + b.ToTable("Characteristic"); + + b.HasData( + new + { + Name = "Front-end", + Value = 100 + }, + new + { + Name = "Back-end", + Value = 100 + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.ImageEntity", b => + { + b.Property("Base64") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.HasKey("Base64"); + + b.ToTable("Image"); + + b.HasData( + new + { + Base64 = "default" + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.RuneEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Family") + .HasColumnType("INTEGER"); + + b.Property("ImageBase64") + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.HasIndex("ImageBase64"); + + b.ToTable("Rune"); + + b.HasData( + new + { + Name = "Mastering of Blue", + Description = "Blue shades", + Family = 2 + }, + new + { + Name = "Empty Shards", + Description = "Remove runes", + Family = 1 + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.RunePageEntity", b => + { + b.Property("Name") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("RunePage"); + + b.HasData( + new + { + Name = "FirstRunepage" + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.SkillEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Type") + .HasColumnType("INTEGER"); + + b.HasKey("Name"); + + b.ToTable("Skill"); + + b.HasData( + new + { + Name = "Beautiful layout", + Description = "Bowl'In", + Type = 3 + }, + new + { + Name = "DB Support", + Description = "DB", + Type = 1 + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.SkinEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("ChampionSkinName") + .HasColumnType("TEXT"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Icon") + .HasColumnType("TEXT"); + + b.Property("ImageBase64") + .HasColumnType("TEXT"); + + b.Property("Price") + .HasColumnType("REAL"); + + b.HasKey("Name"); + + b.HasIndex("ChampionSkinName"); + + b.HasIndex("ImageBase64"); + + b.ToTable("Skin"); + + b.HasData( + new + { + Name = "Darker WinKer", + Description = "Be invisible in the darkness but never alone", + Icon = "default", + Price = 9.99f + }, + new + { + Name = "Summer Daffodil", + Description = "A jewel of Summer for all year long", + Icon = "default", + Price = 9.99f + }); + }); + + modelBuilder.Entity("RuneEntityRunePageEntity", b => + { + b.Property("RunePagesName") + .HasColumnType("TEXT"); + + b.Property("RunesName") + .HasColumnType("TEXT"); + + b.HasKey("RunePagesName", "RunesName"); + + b.HasIndex("RunesName"); + + b.ToTable("RuneEntityRunePageEntity"); + }); + + modelBuilder.Entity("ChampionEntityRunePageEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ChampionEntity", null) + .WithMany() + .HasForeignKey("ChampionsName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("EntityFrameworkLOL.Entities.RunePageEntity", null) + .WithMany() + .HasForeignKey("RunePagesName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ChampionEntitySkillEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ChampionEntity", null) + .WithMany() + .HasForeignKey("ChampionsName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("EntityFrameworkLOL.Entities.SkillEntity", null) + .WithMany() + .HasForeignKey("SkillsName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.ChampionEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ImageEntity", "Image") + .WithMany() + .HasForeignKey("ImageBase64"); + + b.Navigation("Image"); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.CharacteristicEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ChampionEntity", "Champion") + .WithMany() + .HasForeignKey("ChampionName"); + + b.Navigation("Champion"); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.RuneEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ImageEntity", "Image") + .WithMany() + .HasForeignKey("ImageBase64"); + + b.Navigation("Image"); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.SkinEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ChampionEntity", "ChampionSkin") + .WithMany() + .HasForeignKey("ChampionSkinName"); + + b.HasOne("EntityFrameworkLOL.Entities.ImageEntity", "Image") + .WithMany() + .HasForeignKey("ImageBase64"); + + b.Navigation("ChampionSkin"); + + b.Navigation("Image"); + }); + + modelBuilder.Entity("RuneEntityRunePageEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.RunePageEntity", null) + .WithMany() + .HasForeignKey("RunePagesName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("EntityFrameworkLOL.Entities.RuneEntity", null) + .WithMany() + .HasForeignKey("RunesName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFrameworkLOL/Migrations/20230315161305_MigrationWallah6.cs b/Sources/EntityFrameworkLOL/Migrations/20230315161305_MigrationWallah6.cs new file mode 100644 index 0000000..5d59d7b --- /dev/null +++ b/Sources/EntityFrameworkLOL/Migrations/20230315161305_MigrationWallah6.cs @@ -0,0 +1,335 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + +namespace EntityFrameworkLOL.Migrations +{ + /// + public partial class MigrationWallah6 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Image", + columns: table => new + { + Base64 = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Image", x => x.Base64); + }); + + migrationBuilder.CreateTable( + name: "RunePage", + columns: table => new + { + Name = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RunePage", x => x.Name); + }); + + migrationBuilder.CreateTable( + name: "Skill", + columns: table => new + { + Name = table.Column(type: "TEXT", nullable: false), + Description = table.Column(type: "TEXT", nullable: false), + Type = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Skill", x => x.Name); + }); + + migrationBuilder.CreateTable( + name: "Champion", + columns: table => new + { + Name = table.Column(type: "TEXT", nullable: false), + Bio = table.Column(type: "TEXT", nullable: false), + Icon = table.Column(type: "TEXT", nullable: true), + Class = table.Column(type: "INTEGER", nullable: false), + ImageBase64 = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Champion", x => x.Name); + table.ForeignKey( + name: "FK_Champion_Image_ImageBase64", + column: x => x.ImageBase64, + principalTable: "Image", + principalColumn: "Base64"); + }); + + migrationBuilder.CreateTable( + name: "Rune", + columns: table => new + { + Name = table.Column(type: "TEXT", nullable: false), + Description = table.Column(type: "TEXT", nullable: false), + ImageBase64 = table.Column(type: "TEXT", nullable: true), + Family = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Rune", x => x.Name); + table.ForeignKey( + name: "FK_Rune_Image_ImageBase64", + column: x => x.ImageBase64, + principalTable: "Image", + principalColumn: "Base64"); + }); + + migrationBuilder.CreateTable( + name: "ChampionEntityRunePageEntity", + columns: table => new + { + ChampionsName = table.Column(type: "TEXT", nullable: false), + RunePagesName = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ChampionEntityRunePageEntity", x => new { x.ChampionsName, x.RunePagesName }); + table.ForeignKey( + name: "FK_ChampionEntityRunePageEntity_Champion_ChampionsName", + column: x => x.ChampionsName, + principalTable: "Champion", + principalColumn: "Name", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ChampionEntityRunePageEntity_RunePage_RunePagesName", + column: x => x.RunePagesName, + principalTable: "RunePage", + principalColumn: "Name", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ChampionEntitySkillEntity", + columns: table => new + { + ChampionsName = table.Column(type: "TEXT", nullable: false), + SkillsName = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ChampionEntitySkillEntity", x => new { x.ChampionsName, x.SkillsName }); + table.ForeignKey( + name: "FK_ChampionEntitySkillEntity_Champion_ChampionsName", + column: x => x.ChampionsName, + principalTable: "Champion", + principalColumn: "Name", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ChampionEntitySkillEntity_Skill_SkillsName", + column: x => x.SkillsName, + principalTable: "Skill", + principalColumn: "Name", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Characteristic", + columns: table => new + { + Name = table.Column(type: "TEXT", nullable: false), + Value = table.Column(type: "INTEGER", nullable: false), + ChampionName = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Characteristic", x => x.Name); + table.ForeignKey( + name: "FK_Characteristic_Champion_ChampionName", + column: x => x.ChampionName, + principalTable: "Champion", + principalColumn: "Name"); + }); + + migrationBuilder.CreateTable( + name: "Skin", + columns: table => new + { + Name = table.Column(type: "TEXT", nullable: false), + Description = table.Column(type: "TEXT", nullable: false), + Icon = table.Column(type: "TEXT", nullable: true), + Price = table.Column(type: "REAL", nullable: false), + ImageBase64 = table.Column(type: "TEXT", nullable: true), + ChampionSkinName = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Skin", x => x.Name); + table.ForeignKey( + name: "FK_Skin_Champion_ChampionSkinName", + column: x => x.ChampionSkinName, + principalTable: "Champion", + principalColumn: "Name"); + table.ForeignKey( + name: "FK_Skin_Image_ImageBase64", + column: x => x.ImageBase64, + principalTable: "Image", + principalColumn: "Base64"); + }); + + migrationBuilder.CreateTable( + name: "RuneEntityRunePageEntity", + columns: table => new + { + RunePagesName = table.Column(type: "TEXT", nullable: false), + RunesName = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RuneEntityRunePageEntity", x => new { x.RunePagesName, x.RunesName }); + table.ForeignKey( + name: "FK_RuneEntityRunePageEntity_RunePage_RunePagesName", + column: x => x.RunePagesName, + principalTable: "RunePage", + principalColumn: "Name", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RuneEntityRunePageEntity_Rune_RunesName", + column: x => x.RunesName, + principalTable: "Rune", + principalColumn: "Name", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.InsertData( + table: "Champion", + columns: new[] { "Name", "Bio", "Class", "Icon", "ImageBase64" }, + values: new object[,] + { + { "Jonquille", "Daffodil", 5, "", null }, + { "WinKer", "Best front-end designer", 3, "", null } + }); + + migrationBuilder.InsertData( + table: "Characteristic", + columns: new[] { "Name", "ChampionName", "Value" }, + values: new object[,] + { + { "Back-end", null, 100 }, + { "Front-end", null, 100 } + }); + + migrationBuilder.InsertData( + table: "Image", + column: "Base64", + value: "default"); + + migrationBuilder.InsertData( + table: "Rune", + columns: new[] { "Name", "Description", "Family", "ImageBase64" }, + values: new object[,] + { + { "Empty Shards", "Remove runes", 1, null }, + { "Mastering of Blue", "Blue shades", 2, null } + }); + + migrationBuilder.InsertData( + table: "RunePage", + column: "Name", + value: "FirstRunepage"); + + migrationBuilder.InsertData( + table: "Skill", + columns: new[] { "Name", "Description", "Type" }, + values: new object[,] + { + { "Beautiful layout", "Bowl'In", 3 }, + { "DB Support", "DB", 1 } + }); + + migrationBuilder.InsertData( + table: "Skin", + columns: new[] { "Name", "ChampionSkinName", "Description", "Icon", "ImageBase64", "Price" }, + values: new object[,] + { + { "Darker WinKer", null, "Be invisible in the darkness but never alone", "default", null, 9.99f }, + { "Summer Daffodil", null, "A jewel of Summer for all year long", "default", null, 9.99f } + }); + + migrationBuilder.CreateIndex( + name: "IX_Champion_ImageBase64", + table: "Champion", + column: "ImageBase64"); + + migrationBuilder.CreateIndex( + name: "IX_ChampionEntityRunePageEntity_RunePagesName", + table: "ChampionEntityRunePageEntity", + column: "RunePagesName"); + + migrationBuilder.CreateIndex( + name: "IX_ChampionEntitySkillEntity_SkillsName", + table: "ChampionEntitySkillEntity", + column: "SkillsName"); + + migrationBuilder.CreateIndex( + name: "IX_Characteristic_ChampionName", + table: "Characteristic", + column: "ChampionName"); + + migrationBuilder.CreateIndex( + name: "IX_Rune_ImageBase64", + table: "Rune", + column: "ImageBase64"); + + migrationBuilder.CreateIndex( + name: "IX_RuneEntityRunePageEntity_RunesName", + table: "RuneEntityRunePageEntity", + column: "RunesName"); + + migrationBuilder.CreateIndex( + name: "IX_Skin_ChampionSkinName", + table: "Skin", + column: "ChampionSkinName"); + + migrationBuilder.CreateIndex( + name: "IX_Skin_ImageBase64", + table: "Skin", + column: "ImageBase64"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ChampionEntityRunePageEntity"); + + migrationBuilder.DropTable( + name: "ChampionEntitySkillEntity"); + + migrationBuilder.DropTable( + name: "Characteristic"); + + migrationBuilder.DropTable( + name: "RuneEntityRunePageEntity"); + + migrationBuilder.DropTable( + name: "Skin"); + + migrationBuilder.DropTable( + name: "Skill"); + + migrationBuilder.DropTable( + name: "RunePage"); + + migrationBuilder.DropTable( + name: "Rune"); + + migrationBuilder.DropTable( + name: "Champion"); + + migrationBuilder.DropTable( + name: "Image"); + } + } +} diff --git a/Sources/EntityFrameworkLOL/Migrations/SQLiteContextModelSnapshot.cs b/Sources/EntityFrameworkLOL/Migrations/SQLiteContextModelSnapshot.cs new file mode 100644 index 0000000..cc7759d --- /dev/null +++ b/Sources/EntityFrameworkLOL/Migrations/SQLiteContextModelSnapshot.cs @@ -0,0 +1,371 @@ +// +using EntityFrameworkLOL.DBContexts; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFrameworkLOL.Migrations +{ + [DbContext(typeof(SQLiteContext))] + partial class SQLiteContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("ChampionEntityRunePageEntity", b => + { + b.Property("ChampionsName") + .HasColumnType("TEXT"); + + b.Property("RunePagesName") + .HasColumnType("TEXT"); + + b.HasKey("ChampionsName", "RunePagesName"); + + b.HasIndex("RunePagesName"); + + b.ToTable("ChampionEntityRunePageEntity"); + }); + + modelBuilder.Entity("ChampionEntitySkillEntity", b => + { + b.Property("ChampionsName") + .HasColumnType("TEXT"); + + b.Property("SkillsName") + .HasColumnType("TEXT"); + + b.HasKey("ChampionsName", "SkillsName"); + + b.HasIndex("SkillsName"); + + b.ToTable("ChampionEntitySkillEntity"); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.ChampionEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("Bio") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Class") + .HasColumnType("INTEGER"); + + b.Property("Icon") + .HasColumnType("TEXT"); + + b.Property("ImageBase64") + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.HasIndex("ImageBase64"); + + b.ToTable("Champion"); + + b.HasData( + new + { + Name = "WinKer", + Bio = "Best front-end designer", + Class = 3, + Icon = "" + }, + new + { + Name = "Jonquille", + Bio = "Daffodil", + Class = 5, + Icon = "" + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.CharacteristicEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("ChampionName") + .HasColumnType("TEXT"); + + b.Property("Value") + .HasColumnType("INTEGER"); + + b.HasKey("Name"); + + b.HasIndex("ChampionName"); + + b.ToTable("Characteristic"); + + b.HasData( + new + { + Name = "Front-end", + Value = 100 + }, + new + { + Name = "Back-end", + Value = 100 + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.ImageEntity", b => + { + b.Property("Base64") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.HasKey("Base64"); + + b.ToTable("Image"); + + b.HasData( + new + { + Base64 = "default" + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.RuneEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Family") + .HasColumnType("INTEGER"); + + b.Property("ImageBase64") + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.HasIndex("ImageBase64"); + + b.ToTable("Rune"); + + b.HasData( + new + { + Name = "Mastering of Blue", + Description = "Blue shades", + Family = 2 + }, + new + { + Name = "Empty Shards", + Description = "Remove runes", + Family = 1 + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.RunePageEntity", b => + { + b.Property("Name") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("RunePage"); + + b.HasData( + new + { + Name = "FirstRunepage" + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.SkillEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Type") + .HasColumnType("INTEGER"); + + b.HasKey("Name"); + + b.ToTable("Skill"); + + b.HasData( + new + { + Name = "Beautiful layout", + Description = "Bowl'In", + Type = 3 + }, + new + { + Name = "DB Support", + Description = "DB", + Type = 1 + }); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.SkinEntity", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("ChampionSkinName") + .HasColumnType("TEXT"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Icon") + .HasColumnType("TEXT"); + + b.Property("ImageBase64") + .HasColumnType("TEXT"); + + b.Property("Price") + .HasColumnType("REAL"); + + b.HasKey("Name"); + + b.HasIndex("ChampionSkinName"); + + b.HasIndex("ImageBase64"); + + b.ToTable("Skin"); + + b.HasData( + new + { + Name = "Darker WinKer", + Description = "Be invisible in the darkness but never alone", + Icon = "default", + Price = 9.99f + }, + new + { + Name = "Summer Daffodil", + Description = "A jewel of Summer for all year long", + Icon = "default", + Price = 9.99f + }); + }); + + modelBuilder.Entity("RuneEntityRunePageEntity", b => + { + b.Property("RunePagesName") + .HasColumnType("TEXT"); + + b.Property("RunesName") + .HasColumnType("TEXT"); + + b.HasKey("RunePagesName", "RunesName"); + + b.HasIndex("RunesName"); + + b.ToTable("RuneEntityRunePageEntity"); + }); + + modelBuilder.Entity("ChampionEntityRunePageEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ChampionEntity", null) + .WithMany() + .HasForeignKey("ChampionsName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("EntityFrameworkLOL.Entities.RunePageEntity", null) + .WithMany() + .HasForeignKey("RunePagesName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ChampionEntitySkillEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ChampionEntity", null) + .WithMany() + .HasForeignKey("ChampionsName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("EntityFrameworkLOL.Entities.SkillEntity", null) + .WithMany() + .HasForeignKey("SkillsName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.ChampionEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ImageEntity", "Image") + .WithMany() + .HasForeignKey("ImageBase64"); + + b.Navigation("Image"); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.CharacteristicEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ChampionEntity", "Champion") + .WithMany() + .HasForeignKey("ChampionName"); + + b.Navigation("Champion"); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.RuneEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ImageEntity", "Image") + .WithMany() + .HasForeignKey("ImageBase64"); + + b.Navigation("Image"); + }); + + modelBuilder.Entity("EntityFrameworkLOL.Entities.SkinEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.ChampionEntity", "ChampionSkin") + .WithMany() + .HasForeignKey("ChampionSkinName"); + + b.HasOne("EntityFrameworkLOL.Entities.ImageEntity", "Image") + .WithMany() + .HasForeignKey("ImageBase64"); + + b.Navigation("ChampionSkin"); + + b.Navigation("Image"); + }); + + modelBuilder.Entity("RuneEntityRunePageEntity", b => + { + b.HasOne("EntityFrameworkLOL.Entities.RunePageEntity", null) + .WithMany() + .HasForeignKey("RunePagesName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("EntityFrameworkLOL.Entities.RuneEntity", null) + .WithMany() + .HasForeignKey("RunesName") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/ManagersEF/EFManager.Champions.cs b/Sources/ManagersEF/EFManager.Champions.cs index 8e4b040..4983b9d 100644 --- a/Sources/ManagersEF/EFManager.Champions.cs +++ b/Sources/ManagersEF/EFManager.Champions.cs @@ -2,6 +2,8 @@ using Shared; using System.Data.SqlTypes; using APILOL.Mapper; +using Microsoft.EntityFrameworkCore; +using System.Xml.Linq; namespace ManagersEF { @@ -16,18 +18,26 @@ namespace ManagersEF public async Task AddItem(Champion? item) { - await parent.DbContext.Champion.AddAsync(item.ToEntity()); + await parent.DbContext.Champion.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); return item; } public async Task DeleteItem(Champion? item) { - parent.DbContext.Champion.Remove(item.ToEntity()); - return true; + var toDelete = parent.DbContext.Champion.Find(item.Name); + if (toDelete != null) + { + parent.DbContext.Champion.Remove(toDelete); + parent.DbContext.SaveChanges(); + return true; + } + return false; } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { + Console.WriteLine("GET"); return parent.DbContext.Champion.GetItemsWithFilterAndOrdering( c => true, index, count, @@ -37,7 +47,7 @@ namespace ManagersEF public async Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.Champion.GetItemsWithFilterAndOrdering( + return parent.DbContext.Champion.Include("Characteristics").GetItemsWithFilterAndOrdering( c => c.Characteristics.Any(ch => ch.Name.Equals(charName)), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -62,8 +72,8 @@ namespace ManagersEF public async Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.Champion.GetItemsWithFilterAndOrdering( - c => c.RunePages.Any(rp => rp.Equals(runePage.ToEntity())), + return parent.DbContext.Champion.Include("runepages").GetItemsWithFilterAndOrdering( + c => c.RunePages.Any(rp => rp.Equals(runePage.ToEntity(parent.DbContext))), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -79,7 +89,7 @@ namespace ManagersEF public async Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.Champion.GetItemsWithFilterAndOrdering( + return parent.DbContext.Champion.Include("Skills").GetItemsWithFilterAndOrdering( c => skill != null && c.Skills.Any(s => s.Name.Equals(skill)), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -109,7 +119,7 @@ namespace ManagersEF public async Task GetNbItemsByRunePage(RunePage? runePage) { - return parent.DbContext.Champion.Where(c => c.RunePages.Any(rp => rp.Equals(runePage.ToEntity()))).Count(); + return parent.DbContext.Champion.Where(c => c.RunePages.Any(rp => rp.Equals(runePage.ToEntity(parent.DbContext)))).Count(); } @@ -127,8 +137,9 @@ namespace ManagersEF public async Task UpdateItem(Champion? oldItem, Champion? newItem) { - parent.DbContext.Champion.Remove(oldItem.ToEntity()); - parent.DbContext.Champion.Add(newItem.ToEntity()); + var toUpdate = parent.DbContext.Champion.Find(oldItem.Name); + toUpdate = newItem.ToEntity(parent.DbContext); + parent.DbContext.SaveChanges(); return newItem; } } diff --git a/Sources/ManagersEF/EFManager.RunePages.cs b/Sources/ManagersEF/EFManager.RunePages.cs index 00e7a12..af9df17 100644 --- a/Sources/ManagersEF/EFManager.RunePages.cs +++ b/Sources/ManagersEF/EFManager.RunePages.cs @@ -1,8 +1,11 @@ using APILOL.Mapper; +using EntityFrameworkLOL.Entities; using ManagersEF; +using Microsoft.EntityFrameworkCore; using Model; using System.Data.SqlTypes; using System.Linq; +using System.Xml.Linq; namespace ManagersEF { @@ -17,14 +20,21 @@ namespace ManagersEF public async Task AddItem(RunePage? item) { - await parent.DbContext.RunePage.AddAsync(item.ToEntity()); + await parent.DbContext.RunePage.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); return item; } public async Task DeleteItem(RunePage? item) { - parent.DbContext.RunePage.Remove(item.ToEntity()); - return true; + var toDelete = parent.DbContext.RunePage.Find(item.Name); + if (toDelete != null) + { + parent.DbContext.RunePage.Remove(toDelete); + parent.DbContext.SaveChanges(); + return true; + } + return false; } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -32,15 +42,15 @@ namespace ManagersEF return parent.DbContext.RunePage.GetItemsWithFilterAndOrdering( rp => true, index, count, - orderingPropertyName, descending).Select(rp => rp.ToModel()); + orderingPropertyName, descending).Select(rp => rp.ToModel(parent.DbContext)); } public async Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.RunePage.GetItemsWithFilterAndOrdering( + return parent.DbContext.RunePage.Include("champions").GetItemsWithFilterAndOrdering( rp => rp.Champions.Any(c => c.Name.Equals(champion.Name)), index, count, - orderingPropertyName, descending).Select(rp => rp.ToModel()); + orderingPropertyName, descending).Select(rp => rp.ToModel(parent.DbContext)); } public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -48,15 +58,15 @@ namespace ManagersEF return parent.DbContext.RunePage.GetItemsWithFilterAndOrdering( rp => rp.Name.Contains(substring), index, count, - orderingPropertyName, descending).Select(rp => rp.ToModel()); + orderingPropertyName, descending).Select(rp => rp.ToModel(parent.DbContext)); } public async Task> GetItemsByRune(Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.RunePage.GetItemsWithFilterAndOrdering( - rp => rp.Runes.Any(r => r.Name.Equals(rune.Name)), + return parent.DbContext.RunePage.Include("entries").GetItemsWithFilterAndOrdering( + rp => rp.entries.Any(r => r.RuneName.Equals(rune.Name)), index, count, - orderingPropertyName, descending).Select(rp => rp.ToModel()); + orderingPropertyName, descending).Select(rp => rp.ToModel(parent.DbContext)); } public async Task GetNbItems() @@ -67,7 +77,7 @@ namespace ManagersEF public async Task GetNbItemsByChampion(Champion? champion) { - return parent.DbContext.RunePage.Where(rp => rp.Champions.Any(c => c.Name.Equals(champion.Name))).Count(); + return parent.DbContext.RunePage.Where(rp => rp.champions.Any(c => c.Name.Equals(champion.Name))).Count(); } @@ -78,14 +88,27 @@ namespace ManagersEF public async Task GetNbItemsByRune(Model.Rune? rune) { - return parent.DbContext.RunePage.Where(rp => rp.Runes.Any(r => r.Name.Equals(rune.Name))).Count(); + return parent.DbContext.RunePage.Where(rp => rp.entries.Any(r => r.RuneName.Equals(rune.Name))).Count(); } public async Task UpdateItem(RunePage? oldItem, RunePage? newItem) { - parent.DbContext.RunePage.Remove(oldItem.ToEntity()); - parent.DbContext.RunePage.Add(newItem.ToEntity()); + var toUpdate = parent.DbContext.RunePage.Include("entries") + .Where(x => x.Name.Equals(newItem.Name)).First(); + try + { + toUpdate.entries = newItem.Runes.Select(r => new RunePageRuneEntity() + { + category = r.Key, + rune = r.Value.ToEntity(parent.DbContext), + }).ToList(); + + parent.DbContext.SaveChanges(); + + } + catch (DbUpdateException) { } return newItem; + } } } diff --git a/Sources/ManagersEF/EFManager.Runes.cs b/Sources/ManagersEF/EFManager.Runes.cs index 025cd1e..c9160ef 100644 --- a/Sources/ManagersEF/EFManager.Runes.cs +++ b/Sources/ManagersEF/EFManager.Runes.cs @@ -1,6 +1,8 @@ using APILOL.Mapper; +using Microsoft.EntityFrameworkCore; using Model; using Shared; +using System.Xml.Linq; namespace ManagersEF { @@ -14,14 +16,21 @@ namespace ManagersEF => this.parent = parent; public async Task AddItem(Rune? item) { - await parent.DbContext.Rune.AddAsync(item.ToEntity()); + await parent.DbContext.Rune.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); return item; } public async Task DeleteItem(Rune? item) { - parent.DbContext.Rune.Remove(item.ToEntity()); - return true; + var toDelete = parent.DbContext.Rune.Find(item?.Name); + if (toDelete != null) + { + parent.DbContext.Rune.Remove(item?.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); + return true; + } + return false; } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -65,8 +74,10 @@ namespace ManagersEF public async Task UpdateItem(Rune? oldItem, Rune? newItem) { - parent.DbContext.Rune.Remove(oldItem.ToEntity()); - parent.DbContext.Rune.Add(newItem.ToEntity()); + var toUpdate = parent.DbContext.Rune.Find(oldItem.Name); + toUpdate.Description = newItem.Description; + toUpdate.Family = newItem.Family; + parent.DbContext.SaveChanges(); return newItem; } } diff --git a/Sources/ManagersEF/EFManager.Skins.cs b/Sources/ManagersEF/EFManager.Skins.cs index 1810631..7bd981a 100644 --- a/Sources/ManagersEF/EFManager.Skins.cs +++ b/Sources/ManagersEF/EFManager.Skins.cs @@ -1,6 +1,8 @@ using APILOL.Mapper; +using Microsoft.EntityFrameworkCore; using Model; using System.Data.SqlTypes; +using System.Xml.Linq; namespace ManagersEF { @@ -15,38 +17,45 @@ namespace ManagersEF public async Task AddItem(Skin? item) { - await parent.DbContext.Skin.AddAsync(item.ToEntity()); + await parent.DbContext.Skin.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); return item; } public async Task DeleteItem(Skin? item) { - parent.DbContext.Skin.Remove(item.ToEntity()); - return true; + var toDelete = parent.DbContext.Skin.Find(item.Name); + if (toDelete != null) + { + parent.DbContext.Skin.Remove(toDelete); + parent.DbContext.SaveChanges(); + return true; + } + return false; } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.Skin.GetItemsWithFilterAndOrdering( + return parent.DbContext.Skin.Include("Champion").GetItemsWithFilterAndOrdering( s => true, index, count, - orderingPropertyName, descending).Select(s => s.ToModel()); + orderingPropertyName, descending).Select(s => s?.ToModel()); } public async Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.Skin.GetItemsWithFilterAndOrdering( - s => s.Champion.Name.Equals(champion.Name), + return parent.DbContext.Skin.Include("Champion").GetItemsWithFilterAndOrdering( + s => s.ChampionSkin.Name.Equals(champion?.Name), index, count, - orderingPropertyName, descending).Select(s => s.ToModel()); + orderingPropertyName, descending).Select(s => s?.ToModel()); } public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.Skin.GetItemsWithFilterAndOrdering( + return parent.DbContext.Skin.Include("Champion").GetItemsWithFilterAndOrdering( s => s.Name.Contains(substring), index, count, - orderingPropertyName, descending).Select(s => s.ToModel()); + orderingPropertyName, descending).Select(s => s?.ToModel()); } public async Task GetNbItems() @@ -56,8 +65,12 @@ namespace ManagersEF public async Task GetNbItemsByChampion(Champion? champion) { - return parent.DbContext.Skin.Where(s => s.Champion.Name.Equals(champion.Name)) - .Count(); + if (champion != null) + { + return parent.DbContext.Skin.Where(s => s.ChampionSkin.Name.Equals(champion.Name)) + .Count(); + } + return 0; } public async Task GetNbItemsByName(string substring) @@ -68,8 +81,8 @@ namespace ManagersEF public async Task UpdateItem(Skin? oldItem, Skin? newItem) { - parent.DbContext.Skin.Remove(oldItem.ToEntity()); - parent.DbContext.Skin.Add(newItem.ToEntity()); + var toUpdate = parent.DbContext.Skin.Find(oldItem.Name); + toUpdate.ChampionSkin = parent.DbContext.Champion.Find(newItem.Champion.Name); return newItem; } }