diff --git a/Sources/DbDataManager/DbDataManager.cs b/Sources/DbDataManager/DbDataManager.cs index 189eeb3..60ba046 100644 --- a/Sources/DbDataManager/DbDataManager.cs +++ b/Sources/DbDataManager/DbDataManager.cs @@ -3,7 +3,7 @@ namespace DbDataManager; public class DbDataManager : IDataManager { - public IPlayersManager PlayersMgr => throw new NotImplementedException(); + public IPlayersManager PlayersMgr => new PlayersManager(); public IGamesManager GamesMgr => throw new NotImplementedException(); diff --git a/Sources/DbDataManager/DbDataManager.csproj b/Sources/DbDataManager/DbDataManager.csproj index f05027c..bd1898f 100644 --- a/Sources/DbDataManager/DbDataManager.csproj +++ b/Sources/DbDataManager/DbDataManager.csproj @@ -8,5 +8,12 @@ + + + + + + + diff --git a/Sources/DbDataManager/Extensions.cs b/Sources/DbDataManager/Extensions.cs new file mode 100644 index 0000000..894eedc --- /dev/null +++ b/Sources/DbDataManager/Extensions.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DbDataManager +{ + static class Extensions + { + internal static Task> GetItemsWithFilterAndOrdering(this IEnumerable collection, + Func filter, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + IEnumerable temp = collection; + temp = temp.Where(item => filter(item)); + if (orderingPropertyName != null) + { + var prop = typeof(T).GetProperty(orderingPropertyName!); + if (prop != null) + { + temp = descending ? temp.OrderByDescending(item => prop.GetValue(item)) + : temp.OrderBy(item => prop.GetValue(item)); + } + } + return Task.FromResult>(temp.Skip(index * count).Take(count)); + } + + internal static Task GetNbItemsWithFilter(this IEnumerable collection, Func filter) + { + return Task.FromResult(collection.Count(item => filter(item))); + } + + internal static Task AddItem(this IList collection, T? item) + { + if (item == null || collection.Contains(item)) + { + return Task.FromResult(default(T)); + } + collection.Add(item); + return Task.FromResult(item); + } + + internal static Task DeleteItem(this IList collection, T? item) + { + if (item == null) + { + return Task.FromResult(false); + } + bool result = collection.Remove(item!); + return Task.FromResult(result); + } + + internal static Task UpdateItem(this IList collection, T? oldItem, T? newItem) + { + if (oldItem == null || newItem == null) return Task.FromResult(default(T)); + + if (!collection.Contains(oldItem)) + { + return Task.FromResult(default(T)); + } + + collection.Remove(oldItem!); + collection.Add(newItem!); + return Task.FromResult(newItem); + } + } +} diff --git a/Sources/DbDataManager/Mapper/PlayerMapper.cs b/Sources/DbDataManager/Mapper/PlayerMapper.cs new file mode 100644 index 0000000..82ddac4 --- /dev/null +++ b/Sources/DbDataManager/Mapper/PlayerMapper.cs @@ -0,0 +1,30 @@ +using System; +using EntityFrameWorkLib; +using Model; + +namespace DbDataManager.Mapper +{ + public static class PlayerMapper + { + public static Player ToModel(this PlayerEntity playerEntity) + { + Player player = new Player(playerEntity.Pseudo, playerEntity.stats.ToModel(), playerEntity.PlayerId); + return player; + } + + public static PlayerEntity ToEntity(this Player playerModel) + { + PlayerEntity player = new PlayerEntity + { + PlayerId = playerModel.Id, + Pseudo = playerModel.Pseudo, + stats = playerModel.Stats.ToEntity(), + + }; + player.stats.PlayerId = playerModel.Id; + return player; + } + + } +} + diff --git a/Sources/DbDataManager/Mapper/StatsMapper.cs b/Sources/DbDataManager/Mapper/StatsMapper.cs new file mode 100644 index 0000000..a2dc6af --- /dev/null +++ b/Sources/DbDataManager/Mapper/StatsMapper.cs @@ -0,0 +1,40 @@ +using System; +using EntityFrameWorkLib; +using Model; + +namespace DbDataManager.Mapper +{ + public static class StatsMapper + { + public static Stats ToModel(this StatsEntity statsEntity) + { + var stats = new Stats + { + Id = statsEntity.Id, + NbWin = statsEntity.NbWin, + NbPlayed = statsEntity.NbPlayed, + MaxZone = statsEntity.MaxZone, + MaxChain = statsEntity.MaxChain, + MaxPoints = statsEntity.MaxPoints + }; + return stats; + } + + public static StatsEntity ToEntity(this Stats statsModel) + { + var stats = new StatsEntity + { + Id = statsModel.Id, + MaxChain = statsModel.MaxChain, + MaxPoints = statsModel.MaxPoints, + MaxZone = statsModel.MaxZone, + NbPlayed = statsModel.NbPlayed, + NbWin = statsModel.NbWin, + + }; + return stats; + } + + } +} + diff --git a/Sources/DbDataManager/PlayersManager.cs b/Sources/DbDataManager/PlayersManager.cs new file mode 100644 index 0000000..07efefd --- /dev/null +++ b/Sources/DbDataManager/PlayersManager.cs @@ -0,0 +1,85 @@ +using System; +using EntityFrameWorkLib; +using Model; +using DbDataManager.Mapper; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using System.Data.SqlTypes; + +namespace DbDataManager +{ + public class PlayersManager : IPlayersManager + { + private TrekContext trekContext; + + public async Task AddItem(Player? item) + { + if (item == null) + { + return null; + + } + var player = await trekContext.AddAsync(item); + await trekContext.SaveChangesAsync(); + return player.Entity; + } + public async Task DeleteItem(Player? item) + { + if (item == null) + { + return false; + } + var deleteItem = trekContext.Remove(item); + await trekContext.SaveChangesAsync(); + return true; + } + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + { + + IEnumerable players = trekContext.Players.Skip(index * count) + .Take(count) + .OrderBy(players => orderingPropertyName) + .Select(players => players.ToModel()); + + return players; + } + public Task> GetItemsById(int id) + { + return trekContext.Players.Select(player => player.ToModel()).GetItemsWithFilterAndOrdering(player => filterById(player, id),0,1); + } + private Func filterById = (player, id) => player.Id.Equals(id); + + + public Task> GetItemsByPseudo(string charPseudo) + { + return trekContext.Players.Select(player => player.ToModel()).GetItemsWithFilterAndOrdering(player => filterByPseudo(player, charPseudo), 0, trekContext.Players.Count()); + } + private Func filterByPseudo = (player, substring) => player.Pseudo.Contains(substring, StringComparison.InvariantCultureIgnoreCase); + + + public Task GetNbItems() + { + return Task.FromResult(trekContext.Players.Count()); + } + + public Task GetNbItemsByPseudo(string charPseudo) + { + return trekContext.Players.Select(player => player.ToModel()).GetNbItemsWithFilter(player => filterByPseudo(player, charPseudo)); + } + + public Task UpdateItem(Player? oldItem, Player? newItem) + { + if (oldItem == null || newItem == null) return Task.FromResult(default(Player)); + + if (!trekContext.Players.Select(player => player.ToModel()).Contains(oldItem)) + { + return Task.FromResult(default(Player)); + } + + trekContext.Players.Remove(oldItem!.ToEntity()); + trekContext.Players.AddAsync(newItem!.ToEntity()); + return Task.FromResult(newItem); + } + } +} + diff --git a/Sources/Stub/Stub/StubData.cs b/Sources/Stub/Stub/StubData.cs index e492098..d3e32db 100644 --- a/Sources/Stub/Stub/StubData.cs +++ b/Sources/Stub/Stub/StubData.cs @@ -6,11 +6,6 @@ namespace Stub { public StubData() { - //ChampionsMgr = new ChampionsManager(this); - //SkinsMgr = new SkinsManager(this); - //RunesMgr = new RunesManager(this); - //RunePagesMgr = new RunePagesManager(this); - PlayersMgr = new PlayersManager(this); GamesMgr = new GamesManager(this); GamesModeMgr = new GamesModeManager(this); diff --git a/Sources/Trek12_API.sln b/Sources/Trek12_API.sln index 527b156..025e37a 100644 --- a/Sources/Trek12_API.sln +++ b/Sources/Trek12_API.sln @@ -23,7 +23,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test_Stub", "Tests\Test_Stub\Test_Stub.csproj", "{5A1D87FF-E82F-43FF-AC5C-59659B91D5B8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test_Model", "Tests\Test_Model\Test_Model.csproj", "{593766AA-8E2A-4B3D-A299-8E45F5C19C18}" +Project("{9344BDBB-3E7F-41FC-A0DD-8665D75EE146}") = "Test_Model", "Tests\Test_Model\Test_Model.csproj", "{CFA6848A-2A65-49CA-BE0B-3759F52B85E3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbDataManager", "DbDataManager\DbDataManager.csproj", "{57F6555B-462D-4ED1-89C9-495EC7111218}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -63,10 +65,14 @@ Global {5A1D87FF-E82F-43FF-AC5C-59659B91D5B8}.Debug|Any CPU.Build.0 = Debug|Any CPU {5A1D87FF-E82F-43FF-AC5C-59659B91D5B8}.Release|Any CPU.ActiveCfg = Release|Any CPU {5A1D87FF-E82F-43FF-AC5C-59659B91D5B8}.Release|Any CPU.Build.0 = Release|Any CPU - {593766AA-8E2A-4B3D-A299-8E45F5C19C18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {593766AA-8E2A-4B3D-A299-8E45F5C19C18}.Debug|Any CPU.Build.0 = Debug|Any CPU - {593766AA-8E2A-4B3D-A299-8E45F5C19C18}.Release|Any CPU.ActiveCfg = Release|Any CPU - {593766AA-8E2A-4B3D-A299-8E45F5C19C18}.Release|Any CPU.Build.0 = Release|Any CPU + {CFA6848A-2A65-49CA-BE0B-3759F52B85E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CFA6848A-2A65-49CA-BE0B-3759F52B85E3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CFA6848A-2A65-49CA-BE0B-3759F52B85E3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CFA6848A-2A65-49CA-BE0B-3759F52B85E3}.Release|Any CPU.Build.0 = Release|Any CPU + {57F6555B-462D-4ED1-89C9-495EC7111218}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {57F6555B-462D-4ED1-89C9-495EC7111218}.Debug|Any CPU.Build.0 = Debug|Any CPU + {57F6555B-462D-4ED1-89C9-495EC7111218}.Release|Any CPU.ActiveCfg = Release|Any CPU + {57F6555B-462D-4ED1-89C9-495EC7111218}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -76,7 +82,7 @@ Global {6EC6383D-10E8-44E1-8EDD-EF4DF460A9B1} = {C75DF644-C41F-4A08-8B69-C8554204AC72} {5E45E953-4FCC-42B6-9F22-15108D002D78} = {B1585816-FCBB-484F-A1AA-C7AEB501C39B} {5A1D87FF-E82F-43FF-AC5C-59659B91D5B8} = {C75DF644-C41F-4A08-8B69-C8554204AC72} - {593766AA-8E2A-4B3D-A299-8E45F5C19C18} = {C75DF644-C41F-4A08-8B69-C8554204AC72} + {CFA6848A-2A65-49CA-BE0B-3759F52B85E3} = {C75DF644-C41F-4A08-8B69-C8554204AC72} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4D47853B-D1A3-49A5-84BA-CD2DC65FD105} diff --git a/Sources/Trek12_Lib/Migrations/20230320170231_AllMigrations.Designer.cs b/Sources/Trek12_Lib/Migrations/20230411093944_MyMigration.Designer.cs similarity index 64% rename from Sources/Trek12_Lib/Migrations/20230320170231_AllMigrations.Designer.cs rename to Sources/Trek12_Lib/Migrations/20230411093944_MyMigration.Designer.cs index b99d4b8..2fbf2b8 100644 --- a/Sources/Trek12_Lib/Migrations/20230320170231_AllMigrations.Designer.cs +++ b/Sources/Trek12_Lib/Migrations/20230411093944_MyMigration.Designer.cs @@ -11,14 +11,14 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace EntityFrameWorkLib.Migrations { [DbContext(typeof(TrekContext))] - [Migration("20230320170231_AllMigrations")] - partial class AllMigrations + [Migration("20230411093944_MyMigration")] + partial class MyMigration { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + modelBuilder.HasAnnotation("ProductVersion", "7.0.4"); modelBuilder.Entity("EntityFrameWorkLib.CaseEntity", b => { @@ -26,11 +26,16 @@ namespace EntityFrameWorkLib.Migrations .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); + b.Property("GrilleId") + .HasColumnType("INTEGER"); + b.Property("Value") .HasColumnType("INTEGER"); b.HasKey("CaseId"); + b.HasIndex("GrilleId"); + b.ToTable("Case"); }); @@ -47,14 +52,18 @@ namespace EntityFrameWorkLib.Migrations .HasColumnType("TEXT"); b.Property("Name") - .IsRequired() .HasColumnType("TEXT"); b.Property("NbPlayers") .HasColumnType("INTEGER"); + b.Property("PlayerId") + .HasColumnType("INTEGER"); + b.HasKey("GameId"); + b.HasIndex("PlayerId"); + b.ToTable("Game"); }); @@ -87,52 +96,62 @@ namespace EntityFrameWorkLib.Migrations .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("MaxPoints") - .HasColumnType("INTEGER"); + b.Property("Pseudo") + .HasColumnType("TEXT"); - b.Property("MaxZone") - .HasColumnType("INTEGER"); + b.HasKey("PlayerId"); - b.Property("NbPlayed") + b.ToTable("Players"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.ScoreEntity", b => + { + b.Property("GameId") .HasColumnType("INTEGER"); - b.Property("NbPoints") + b.Property("PlayerId") .HasColumnType("INTEGER"); - b.Property("NbWin") + b.Property("NbPointsTotal") .HasColumnType("INTEGER"); - b.Property("Pseudo") - .IsRequired() - .HasColumnType("TEXT"); + b.HasKey("GameId", "PlayerId"); - b.HasKey("PlayerId"); + b.HasIndex("PlayerId"); - b.ToTable("Players"); + b.ToTable("Score"); }); - modelBuilder.Entity("EntityFrameWorkLib.ScoreEntity", b => + modelBuilder.Entity("EntityFrameWorkLib.StatsEntity", b => { - b.Property("ScoreId") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("GameId") + b.Property("MaxChain") .HasColumnType("INTEGER"); - b.Property("NbPoints") + b.Property("MaxPoints") .HasColumnType("INTEGER"); - b.Property("PlayerId") + b.Property("MaxZone") .HasColumnType("INTEGER"); - b.HasKey("ScoreId"); + b.Property("NbPlayed") + .HasColumnType("INTEGER"); - b.HasIndex("GameId"); + b.Property("NbWin") + .HasColumnType("INTEGER"); - b.HasIndex("PlayerId"); + b.Property("PlayerId") + .HasColumnType("INTEGER"); - b.ToTable("Score"); + b.HasKey("Id"); + + b.HasIndex("PlayerId") + .IsUnique(); + + b.ToTable("Stats"); }); modelBuilder.Entity("EntityFrameWorkLib.TurnEntity", b => @@ -152,10 +171,32 @@ namespace EntityFrameWorkLib.Migrations b.ToTable("Turn"); }); + modelBuilder.Entity("EntityFrameWorkLib.CaseEntity", b => + { + b.HasOne("EntityFrameWorkLib.GrilleEntity", "Grille") + .WithMany("Cases") + .HasForeignKey("GrilleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Grille"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.GameEntity", b => + { + b.HasOne("EntityFrameWorkLib.PlayerEntity", "Player") + .WithMany() + .HasForeignKey("PlayerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Player"); + }); + modelBuilder.Entity("EntityFrameWorkLib.ScoreEntity", b => { b.HasOne("EntityFrameWorkLib.GameEntity", "Game") - .WithMany() + .WithMany("Scores") .HasForeignKey("GameId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -170,6 +211,33 @@ namespace EntityFrameWorkLib.Migrations b.Navigation("Player"); }); + + modelBuilder.Entity("EntityFrameWorkLib.StatsEntity", b => + { + b.HasOne("EntityFrameWorkLib.PlayerEntity", "Player") + .WithOne("stats") + .HasForeignKey("EntityFrameWorkLib.StatsEntity", "PlayerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Player"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.GameEntity", b => + { + b.Navigation("Scores"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.GrilleEntity", b => + { + b.Navigation("Cases"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.PlayerEntity", b => + { + b.Navigation("stats") + .IsRequired(); + }); #pragma warning restore 612, 618 } } diff --git a/Sources/Trek12_Lib/Migrations/20230320170231_AllMigrations.cs b/Sources/Trek12_Lib/Migrations/20230411093944_MyMigration.cs similarity index 70% rename from Sources/Trek12_Lib/Migrations/20230320170231_AllMigrations.cs rename to Sources/Trek12_Lib/Migrations/20230411093944_MyMigration.cs index 5790c46..64c5ec2 100644 --- a/Sources/Trek12_Lib/Migrations/20230320170231_AllMigrations.cs +++ b/Sources/Trek12_Lib/Migrations/20230411093944_MyMigration.cs @@ -6,101 +6,132 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace EntityFrameWorkLib.Migrations { /// - public partial class AllMigrations : Migration + public partial class MyMigration : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( - name: "Case", + name: "Grille", columns: table => new { - CaseId = table.Column(type: "INTEGER", nullable: false) + GrilleId = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - Value = table.Column(type: "INTEGER", nullable: false) + NbChains = table.Column(type: "INTEGER", nullable: false), + NbZones = table.Column(type: "INTEGER", nullable: false), + MaxChain = table.Column(type: "INTEGER", nullable: false), + MaxZone = table.Column(type: "INTEGER", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Case", x => x.CaseId); + table.PrimaryKey("PK_Grille", x => x.GrilleId); }); migrationBuilder.CreateTable( - name: "Game", + name: "Players", columns: table => new { - GameId = table.Column(type: "INTEGER", nullable: false) + PlayerId = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - Duration = table.Column(type: "TEXT", nullable: false), - Date = table.Column(type: "TEXT", nullable: false), - NbPlayers = table.Column(type: "INTEGER", nullable: false), - Name = table.Column(type: "TEXT", nullable: false) + Pseudo = table.Column(type: "TEXT", nullable: true) }, constraints: table => { - table.PrimaryKey("PK_Game", x => x.GameId); + table.PrimaryKey("PK_Players", x => x.PlayerId); }); migrationBuilder.CreateTable( - name: "Grille", + name: "Turn", columns: table => new { - GrilleId = table.Column(type: "INTEGER", nullable: false) + TurnId = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - NbChains = table.Column(type: "INTEGER", nullable: false), - NbZones = table.Column(type: "INTEGER", nullable: false), - MaxChain = table.Column(type: "INTEGER", nullable: false), - MaxZone = table.Column(type: "INTEGER", nullable: false) + DiceValue1 = table.Column(type: "INTEGER", nullable: false), + DiceValue2 = table.Column(type: "INTEGER", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Grille", x => x.GrilleId); + table.PrimaryKey("PK_Turn", x => x.TurnId); }); migrationBuilder.CreateTable( - name: "Players", + name: "Case", columns: table => new { - PlayerId = table.Column(type: "INTEGER", nullable: false) + CaseId = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - Pseudo = table.Column(type: "TEXT", nullable: false), - NbWin = table.Column(type: "INTEGER", nullable: false), - NbPlayed = table.Column(type: "INTEGER", nullable: false), - MaxZone = table.Column(type: "INTEGER", nullable: false), - MaxPoints = table.Column(type: "INTEGER", nullable: false), - NbPoints = table.Column(type: "INTEGER", nullable: false) + GrilleId = table.Column(type: "INTEGER", nullable: false), + Value = table.Column(type: "INTEGER", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Players", x => x.PlayerId); + table.PrimaryKey("PK_Case", x => x.CaseId); + table.ForeignKey( + name: "FK_Case_Grille_GrilleId", + column: x => x.GrilleId, + principalTable: "Grille", + principalColumn: "GrilleId", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( - name: "Turn", + name: "Game", columns: table => new { - TurnId = table.Column(type: "INTEGER", nullable: false) + GameId = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - DiceValue1 = table.Column(type: "INTEGER", nullable: false), - DiceValue2 = table.Column(type: "INTEGER", nullable: false) + Duration = table.Column(type: "TEXT", nullable: false), + Date = table.Column(type: "TEXT", nullable: false), + NbPlayers = table.Column(type: "INTEGER", nullable: false), + PlayerId = table.Column(type: "INTEGER", nullable: false), + Name = table.Column(type: "TEXT", nullable: true) }, constraints: table => { - table.PrimaryKey("PK_Turn", x => x.TurnId); + table.PrimaryKey("PK_Game", x => x.GameId); + table.ForeignKey( + name: "FK_Game_Players_PlayerId", + column: x => x.PlayerId, + principalTable: "Players", + principalColumn: "PlayerId", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( - name: "Score", + name: "Stats", columns: table => new { - ScoreId = table.Column(type: "INTEGER", nullable: false) + Id = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - NbPoints = table.Column(type: "INTEGER", nullable: false), - GameId = table.Column(type: "INTEGER", nullable: false), + NbWin = table.Column(type: "INTEGER", nullable: false), + NbPlayed = table.Column(type: "INTEGER", nullable: false), + MaxChain = table.Column(type: "INTEGER", nullable: false), + MaxZone = table.Column(type: "INTEGER", nullable: false), + MaxPoints = table.Column(type: "INTEGER", nullable: false), PlayerId = table.Column(type: "INTEGER", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Score", x => x.ScoreId); + table.PrimaryKey("PK_Stats", x => x.Id); + table.ForeignKey( + name: "FK_Stats_Players_PlayerId", + column: x => x.PlayerId, + principalTable: "Players", + principalColumn: "PlayerId", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Score", + columns: table => new + { + GameId = table.Column(type: "INTEGER", nullable: false), + PlayerId = table.Column(type: "INTEGER", nullable: false), + NbPointsTotal = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Score", x => new { x.GameId, x.PlayerId }); table.ForeignKey( name: "FK_Score_Game_GameId", column: x => x.GameId, @@ -116,14 +147,25 @@ namespace EntityFrameWorkLib.Migrations }); migrationBuilder.CreateIndex( - name: "IX_Score_GameId", - table: "Score", - column: "GameId"); + name: "IX_Case_GrilleId", + table: "Case", + column: "GrilleId"); + + migrationBuilder.CreateIndex( + name: "IX_Game_PlayerId", + table: "Game", + column: "PlayerId"); migrationBuilder.CreateIndex( name: "IX_Score_PlayerId", table: "Score", column: "PlayerId"); + + migrationBuilder.CreateIndex( + name: "IX_Stats_PlayerId", + table: "Stats", + column: "PlayerId", + unique: true); } /// @@ -133,14 +175,17 @@ namespace EntityFrameWorkLib.Migrations name: "Case"); migrationBuilder.DropTable( - name: "Grille"); + name: "Score"); migrationBuilder.DropTable( - name: "Score"); + name: "Stats"); migrationBuilder.DropTable( name: "Turn"); + migrationBuilder.DropTable( + name: "Grille"); + migrationBuilder.DropTable( name: "Game"); diff --git a/Sources/Trek12_Lib/Migrations/TrekContextModelSnapshot.cs b/Sources/Trek12_Lib/Migrations/TrekContextModelSnapshot.cs index c577c75..8fe9309 100644 --- a/Sources/Trek12_Lib/Migrations/TrekContextModelSnapshot.cs +++ b/Sources/Trek12_Lib/Migrations/TrekContextModelSnapshot.cs @@ -15,7 +15,7 @@ namespace EntityFrameWorkLib.Migrations protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + modelBuilder.HasAnnotation("ProductVersion", "7.0.4"); modelBuilder.Entity("EntityFrameWorkLib.CaseEntity", b => { @@ -23,11 +23,16 @@ namespace EntityFrameWorkLib.Migrations .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); + b.Property("GrilleId") + .HasColumnType("INTEGER"); + b.Property("Value") .HasColumnType("INTEGER"); b.HasKey("CaseId"); + b.HasIndex("GrilleId"); + b.ToTable("Case"); }); @@ -44,14 +49,18 @@ namespace EntityFrameWorkLib.Migrations .HasColumnType("TEXT"); b.Property("Name") - .IsRequired() .HasColumnType("TEXT"); b.Property("NbPlayers") .HasColumnType("INTEGER"); + b.Property("PlayerId") + .HasColumnType("INTEGER"); + b.HasKey("GameId"); + b.HasIndex("PlayerId"); + b.ToTable("Game"); }); @@ -84,52 +93,62 @@ namespace EntityFrameWorkLib.Migrations .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("MaxPoints") - .HasColumnType("INTEGER"); + b.Property("Pseudo") + .HasColumnType("TEXT"); - b.Property("MaxZone") - .HasColumnType("INTEGER"); + b.HasKey("PlayerId"); - b.Property("NbPlayed") + b.ToTable("Players"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.ScoreEntity", b => + { + b.Property("GameId") .HasColumnType("INTEGER"); - b.Property("NbPoints") + b.Property("PlayerId") .HasColumnType("INTEGER"); - b.Property("NbWin") + b.Property("NbPointsTotal") .HasColumnType("INTEGER"); - b.Property("Pseudo") - .IsRequired() - .HasColumnType("TEXT"); + b.HasKey("GameId", "PlayerId"); - b.HasKey("PlayerId"); + b.HasIndex("PlayerId"); - b.ToTable("Players"); + b.ToTable("Score"); }); - modelBuilder.Entity("EntityFrameWorkLib.ScoreEntity", b => + modelBuilder.Entity("EntityFrameWorkLib.StatsEntity", b => { - b.Property("ScoreId") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("GameId") + b.Property("MaxChain") .HasColumnType("INTEGER"); - b.Property("NbPoints") + b.Property("MaxPoints") .HasColumnType("INTEGER"); - b.Property("PlayerId") + b.Property("MaxZone") .HasColumnType("INTEGER"); - b.HasKey("ScoreId"); + b.Property("NbPlayed") + .HasColumnType("INTEGER"); - b.HasIndex("GameId"); + b.Property("NbWin") + .HasColumnType("INTEGER"); - b.HasIndex("PlayerId"); + b.Property("PlayerId") + .HasColumnType("INTEGER"); - b.ToTable("Score"); + b.HasKey("Id"); + + b.HasIndex("PlayerId") + .IsUnique(); + + b.ToTable("Stats"); }); modelBuilder.Entity("EntityFrameWorkLib.TurnEntity", b => @@ -149,10 +168,32 @@ namespace EntityFrameWorkLib.Migrations b.ToTable("Turn"); }); + modelBuilder.Entity("EntityFrameWorkLib.CaseEntity", b => + { + b.HasOne("EntityFrameWorkLib.GrilleEntity", "Grille") + .WithMany("Cases") + .HasForeignKey("GrilleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Grille"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.GameEntity", b => + { + b.HasOne("EntityFrameWorkLib.PlayerEntity", "Player") + .WithMany() + .HasForeignKey("PlayerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Player"); + }); + modelBuilder.Entity("EntityFrameWorkLib.ScoreEntity", b => { b.HasOne("EntityFrameWorkLib.GameEntity", "Game") - .WithMany() + .WithMany("Scores") .HasForeignKey("GameId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -167,6 +208,33 @@ namespace EntityFrameWorkLib.Migrations b.Navigation("Player"); }); + + modelBuilder.Entity("EntityFrameWorkLib.StatsEntity", b => + { + b.HasOne("EntityFrameWorkLib.PlayerEntity", "Player") + .WithOne("stats") + .HasForeignKey("EntityFrameWorkLib.StatsEntity", "PlayerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Player"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.GameEntity", b => + { + b.Navigation("Scores"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.GrilleEntity", b => + { + b.Navigation("Cases"); + }); + + modelBuilder.Entity("EntityFrameWorkLib.PlayerEntity", b => + { + b.Navigation("stats") + .IsRequired(); + }); #pragma warning restore 612, 618 } } diff --git a/Sources/Trek12_Lib/PlayerEntity.cs b/Sources/Trek12_Lib/PlayerEntity.cs index 62cc19d..1b947c3 100644 --- a/Sources/Trek12_Lib/PlayerEntity.cs +++ b/Sources/Trek12_Lib/PlayerEntity.cs @@ -10,10 +10,7 @@ namespace EntityFrameWorkLib [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int PlayerId { get; set; } public string? Pseudo { get; set; } - public int NbWin { get; set; } - public int NbPlayed { get; set; } - public int MaxZone { get; set; } - public int MaxPoints { get; set; } + public StatsEntity stats { get; set; } } } diff --git a/Sources/Trek12_Lib/StatsEntity.cs b/Sources/Trek12_Lib/StatsEntity.cs new file mode 100644 index 0000000..569d4f7 --- /dev/null +++ b/Sources/Trek12_Lib/StatsEntity.cs @@ -0,0 +1,17 @@ +using System; +namespace EntityFrameWorkLib +{ + public class StatsEntity + { + public int Id { get; set; } + public int NbWin { get; set; } + public int NbPlayed { get; set; } + public int MaxChain { get; set; } + public int MaxZone { get; set; } + public int MaxPoints { get; set; } + + public int PlayerId { get; set; } + public PlayerEntity Player { get; set; } + } +} + diff --git a/Sources/Trek12_Lib/TrekContext.cs b/Sources/Trek12_Lib/TrekContext.cs index 33c88bc..4661400 100644 --- a/Sources/Trek12_Lib/TrekContext.cs +++ b/Sources/Trek12_Lib/TrekContext.cs @@ -20,6 +20,8 @@ namespace EntityFrameWorkLib public DbSet Score { get; set; } + public DbSet Stats { get; set; } + public TrekContext() { } public TrekContext(DbContextOptions options) : base(options) @@ -78,8 +80,13 @@ namespace EntityFrameWorkLib .WithOne(c => c.Grille) .HasForeignKey(c => c.GrilleId); + modelBuilder.Entity() //l'entité Player... + .HasOne(n => n.stats) //a une propriété obligatoire stats... + .WithOne(c => c.Player) //reliée à la propriété Player de Stats... + .HasForeignKey(c => c.PlayerId);//dont la propriété Id est une Foreign Key + //Configuration des clés primaires et étrangères pour la table Participate - + base.OnModelCreating(modelBuilder); } diff --git a/Sources/Trek12_Lib/projet.AllTables.db b/Sources/Trek12_Lib/projet.AllTables.db index 1dcbc42..cf51b30 100644 Binary files a/Sources/Trek12_Lib/projet.AllTables.db and b/Sources/Trek12_Lib/projet.AllTables.db differ diff --git a/Sources/Trek12_Lib/projet.AllTables.db-shm b/Sources/Trek12_Lib/projet.AllTables.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/Sources/Trek12_Lib/projet.AllTables.db-shm differ diff --git a/Sources/Trek12_Lib/projet.AllTables.db-wal b/Sources/Trek12_Lib/projet.AllTables.db-wal new file mode 100644 index 0000000..e69de29