diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db b/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db index 81dae89..c7e68ec 100644 Binary files a/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db and b/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db differ diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-shm b/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-shm index 8bb59d4..ab9f051 100644 Binary files a/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-shm and b/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-shm differ diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-wal b/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-wal index ea04235..cc80bcb 100644 Binary files a/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-wal and b/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-wal differ diff --git a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs index 9155269..4b358cd 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs @@ -84,7 +84,7 @@ namespace Business { return parent.DbContext.champions.GetItemsWithFilterAndOrdering( - c => c.runepages.Any(rp => rp.Equals(runePage.ToEntity())), + c => c.runepages.Any(rp => rp.Equals(runePage.ToEntity(parent.DbContext))), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -130,7 +130,7 @@ namespace Business public async Task GetNbItemsByRunePage(RunePage? runePage) { - return parent.DbContext.champions.Where(c => c.runepages.Any(rp => rp.Equals(runePage.ToEntity()))).Count(); + return parent.DbContext.champions.Where(c => c.runepages.Any(rp => rp.Equals(runePage.ToEntity(parent.DbContext)))).Count(); } diff --git a/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs b/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs index 0601236..1830f2c 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs @@ -1,4 +1,5 @@ using EntityMapper; +using Microsoft.EntityFrameworkCore; using Model; namespace Business @@ -14,13 +15,19 @@ namespace Business public async Task AddItem(RunePage? item) { - await parent.DbContext.runepages.AddAsync(item.ToEntity()); + try + { + await parent.DbContext.runepages.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); + } + catch (OperationCanceledException) { } + catch (DbUpdateException) { } return item; } public async Task DeleteItem(RunePage? item) { - parent.DbContext.runepages.Remove(item.ToEntity()); + parent.DbContext.runepages.Remove(item.ToEntity(parent.DbContext)); return true; } @@ -29,7 +36,7 @@ namespace Business return parent.DbContext.runepages.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) @@ -37,7 +44,7 @@ namespace Business return parent.DbContext.runepages.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) @@ -45,15 +52,15 @@ namespace Business return parent.DbContext.runepages.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.runepages.GetItemsWithFilterAndOrdering( - rp => rp.runes.Any(r => r.Name.Equals(rune.Name)), + return parent.DbContext.runepages.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() @@ -75,13 +82,13 @@ namespace Business public async Task GetNbItemsByRune(Model.Rune? rune) { - return parent.DbContext.runepages.Where(rp => rp.runes.Any(r => r.Name.Equals(rune.Name))).Count(); + return parent.DbContext.runepages.Where(rp => rp.entries.Any(r => r.RuneName.Equals(rune.Name))).Count(); } public async Task UpdateItem(RunePage? oldItem, RunePage? newItem) { - parent.DbContext.runepages.Remove(oldItem.ToEntity()); - parent.DbContext.runepages.Add(newItem.ToEntity()); + parent.DbContext.runepages.Remove(oldItem.ToEntity(parent.DbContext)); + parent.DbContext.runepages.Add(newItem.ToEntity(parent.DbContext)); return newItem; } } diff --git a/EntityFramework_LoL/Sources/Entities/LolDbContext.cs b/EntityFramework_LoL/Sources/Entities/LolDbContext.cs index 8772474..571b107 100644 --- a/EntityFramework_LoL/Sources/Entities/LolDbContext.cs +++ b/EntityFramework_LoL/Sources/Entities/LolDbContext.cs @@ -16,6 +16,7 @@ namespace Entities public DbSet runes { get; set; } public DbSet runepages { get; set; } public DbSet largeimages { get; set; } + public DbSet runepagerunes { get; set; } public LolDbContext(DbContextOptions configuration) : base(configuration){} protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) @@ -31,16 +32,15 @@ namespace Entities modelBuilder.Entity().Property(e => e.Id).ValueGeneratedOnAdd(); modelBuilder.Entity().HasKey(c => new { c.Name, c.ChampionForeignKey }); + modelBuilder.Entity().HasKey(c => new { c.RunePageName, c.RuneName }); - modelBuilder.Entity().Property(e => e.Id).ValueGeneratedOnAdd(); - modelBuilder.Entity() - .HasMany(x => x.runes) - .WithMany(x => x.runepages) - .UsingEntity(); + //modelBuilder.Entity().Property(e => e.Id).ValueGeneratedOnAdd(); modelBuilder.Entity() .HasMany(x => x.runepages) .WithMany(x => x.champions); } } + + internal record NewRecord(string Name, string Item); } diff --git a/EntityFramework_LoL/Sources/Entities/LolDbContextWithStub.cs b/EntityFramework_LoL/Sources/Entities/LolDbContextWithStub.cs index 204ad8b..cec9087 100644 --- a/EntityFramework_LoL/Sources/Entities/LolDbContextWithStub.cs +++ b/EntityFramework_LoL/Sources/Entities/LolDbContextWithStub.cs @@ -96,7 +96,7 @@ namespace Entities { new() { - Id = Guid.NewGuid(), + //Id = Guid.NewGuid(), Name="Runepage_1" } }); diff --git a/EntityFramework_LoL/Sources/Entities/RuneEntity.cs b/EntityFramework_LoL/Sources/Entities/RuneEntity.cs index 9b1d077..a456235 100644 --- a/EntityFramework_LoL/Sources/Entities/RuneEntity.cs +++ b/EntityFramework_LoL/Sources/Entities/RuneEntity.cs @@ -21,7 +21,7 @@ namespace Entities [Required] public RuneFamily RuneFamily { get; set; } - public ICollection? runepages { get; set; } + public ICollection? runepages { get; set; } public Guid? ImageId { get; set; } diff --git a/EntityFramework_LoL/Sources/Entities/RunePageEntity.cs b/EntityFramework_LoL/Sources/Entities/RunePageEntity.cs index 1c248ab..b4e4f99 100644 --- a/EntityFramework_LoL/Sources/Entities/RunePageEntity.cs +++ b/EntityFramework_LoL/Sources/Entities/RunePageEntity.cs @@ -11,14 +11,10 @@ namespace Entities public class RunePageEntity { [Key] - public Guid Id { get; set; } - [MaxLength(256)] public string Name { get; set; } - public ICollection runes { get; set; } + public ICollection entries { get; set; } public ICollection champions { get; set; } - - } } diff --git a/EntityFramework_LoL/Sources/Entities/RunePageRuneEntity.cs b/EntityFramework_LoL/Sources/Entities/RunePageRuneEntity.cs index a1ad83f..10f01d8 100644 --- a/EntityFramework_LoL/Sources/Entities/RunePageRuneEntity.cs +++ b/EntityFramework_LoL/Sources/Entities/RunePageRuneEntity.cs @@ -1,14 +1,20 @@ using Shared; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace Entities { public class RunePageRuneEntity { - public Category Category { get; set; } + public Category category { get; set; } + + [ForeignKey("RuneName")] + public RuneEntity rune { get; set; } + public string RuneName { get; set; } + + [ForeignKey("RunePageName")] + public RunePageEntity runepage { get; set; } + public string RunePageName { get; set; } + } } diff --git a/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs index 861d718..e00b0e3 100644 --- a/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs +++ b/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs @@ -9,7 +9,7 @@ namespace EntityMapper public static ChampionEntity ToEntity(this Champion item, LolDbContext context) { - ChampionEntity championEntity = context.champions.Find(item.Name); + ChampionEntity? championEntity = context.champions.Find(item.Name); if (championEntity == null) { championEntity = new() diff --git a/EntityFramework_LoL/Sources/EntityMappers/RuneMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/RuneMapper.cs index 472f94c..5dcf3d8 100644 --- a/EntityFramework_LoL/Sources/EntityMappers/RuneMapper.cs +++ b/EntityFramework_LoL/Sources/EntityMappers/RuneMapper.cs @@ -8,7 +8,7 @@ namespace EntityMapper { public static RuneEntity ToEntity(this Rune item, LolDbContext context) { - RuneEntity runeEntity = context.runes.Find(item.Name); + RuneEntity? runeEntity = context.runes.Find(item.Name); if (runeEntity == null) { return new() { diff --git a/EntityFramework_LoL/Sources/EntityMappers/RunePageMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/RunePageMapper.cs index 8044010..a9e46c0 100644 --- a/EntityFramework_LoL/Sources/EntityMappers/RunePageMapper.cs +++ b/EntityFramework_LoL/Sources/EntityMappers/RunePageMapper.cs @@ -1,18 +1,47 @@ using Entities; using Model; +using Shared; namespace EntityMapper { public static class RunePageMapper { - public static RunePageEntity ToEntity(this RunePage item) + public static RunePageEntity ToEntity(this RunePage item, LolDbContext context) { - throw new NotImplementedException(); + RunePageEntity? runePageEntity = context.runepages.Find(item.Name); + if (runePageEntity == null) + { + runePageEntity = new() + { + Name = item.Name, + }; + + runePageEntity.entries = new List(); + foreach (var r in item.Runes) + { + runePageEntity.entries.Add(new RunePageRuneEntity() + { + category = r.Key, + rune = r.Value.ToEntity(context), + }); + } + + } + return runePageEntity; } - public static RunePage ToModel(this RunePageEntity entity) + public static RunePage ToModel(this RunePageEntity entity, LolDbContext context) { - throw new NotImplementedException(); + RunePage runePage = new(entity.Name); + if(entity.entries!= null) + { + foreach(var r in entity.entries) + { + var rune = context.runes.Find(r.RuneName); + if(rune!=null) runePage[r.category] = rune.ToModel(); + }; + } + return runePage; } } diff --git a/EntityFramework_LoL/Sources/TestEF/TestRunePage.cs b/EntityFramework_LoL/Sources/TestEF/TestRunePage.cs new file mode 100644 index 0000000..5487e49 --- /dev/null +++ b/EntityFramework_LoL/Sources/TestEF/TestRunePage.cs @@ -0,0 +1,95 @@ +using Business; +using Entities; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Model; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestEF +{ + public class TestRunePage + { + [Fact] + public async void Test_Add() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new LolDbContext(options)) + { + var manager = new DbData(context).RunePagesMgr; + var runeManager = new DbData(context).RunesMgr; + + context.Database.EnsureCreated(); + + Model.Rune rune1 = new("Sanglante", RuneFamily.Domination); + Model.Rune rune2 = new("Oeil de l'esprit", RuneFamily.Precision); + Model.Rune rune3 = new("Concrétisation", RuneFamily.Unknown); + Model.Rune rune4 = new("The moon", RuneFamily.Unknown); + Model.Rune rune5 = new("Radiance", RuneFamily.Domination); + Model.Rune rune6 = new("Bullseye", RuneFamily.Precision); + + RunePage runepage1 = new("Damages"); + runepage1[Category.Major] = rune1; + runepage1[Category.Minor1] = rune2; + runepage1[Category.Minor2] = rune5; + + RunePage runepage2 = new("Hawk"); + + runepage2[Category.Major] = rune6; + runepage2[Category.Minor1] = rune2; + + RunePage runepage3 = new("Juggler"); + runepage3[Category.Major] = rune5; + runepage3[Category.Minor1] = rune3; + runepage3[Category.Minor2] = rune2; + + await runeManager.AddItem(rune1); + await runeManager.AddItem(rune2); + await runeManager.AddItem(rune3); + await runeManager.AddItem(rune4); + await runeManager.AddItem(rune5); + await runeManager.AddItem(rune6); + + await manager.AddItem(runepage1); + await manager.AddItem(runepage2); + await manager.AddItem(runepage3); + } + + //uses another instance of the context to do the tests + using (var context = new LolDbContext(options)) + { + var manager = new DbData(context).RunePagesMgr; + + context.Database.EnsureCreated(); + + Model.Rune rune5 = new("Radiance", RuneFamily.Domination); + Model.Rune rune1 = new("Sanglante", RuneFamily.Domination); + + var nbItems = await manager.GetNbItems(); + Assert.Equal(3, nbItems); + + var items = await manager.GetItemsByName("Ha", 0, nbItems); + Assert.Equal("Hawk", items.First().Name); + + Assert.Equal(2, await manager.GetNbItemsByName("a")); + + Assert.Equal(1, await manager.GetNbItemsByRune(rune5)); + + items = await manager.GetItemsByRune(rune1, 0, nbItems); + Assert.Equal("Damages", items.First().Name); + } + } + + } +}