From de27dfe029c0acd1a61ba0b3ff6f3f7e50c366fc Mon Sep 17 00:00:00 2001 From: Corentin R <76619184+Koroh63@users.noreply.github.com> Date: Wed, 22 Feb 2023 17:19:30 +0100 Subject: [PATCH] Ajout de la fluent API --- Sources/Api_UT/EntityTest.cs | 6 +- Sources/Api_UT/UnitTest1.cs | 10 +- Sources/ConsoleTestAPI/ConsoleTestAPI.csproj | 20 ++ Sources/ConsoleTestAPI/Program.cs | 194 ++++++++++++++++++ Sources/DTO/ChampionDTO.cs | 16 +- Sources/DTO/DTO.csproj | 4 + Sources/EntityFramework/ChampionEntity.cs | 24 ++- Sources/EntityFramework/LoLDbContext.cs | 23 +++ .../20230222160559_myMigration.Designer.cs | 50 +++++ .../Migrations/20230222160559_myMigration.cs | 36 ++++ .../Migrations/LoLDbContextModelSnapshot.cs | 47 +++++ Sources/EntityFramework/Program.cs | 19 +- Sources/EntityFramework/champion.db | Bin 20480 -> 20480 bytes Sources/EntityFramework/champion.db-shm | Bin 0 -> 32768 bytes Sources/EntityFramework/champion.db-wal | Bin 0 -> 24752 bytes Sources/LeagueOfLegends.sln | 7 + 16 files changed, 441 insertions(+), 15 deletions(-) create mode 100644 Sources/ConsoleTestAPI/ConsoleTestAPI.csproj create mode 100644 Sources/ConsoleTestAPI/Program.cs create mode 100644 Sources/EntityFramework/Migrations/20230222160559_myMigration.Designer.cs create mode 100644 Sources/EntityFramework/Migrations/20230222160559_myMigration.cs create mode 100644 Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs create mode 100644 Sources/EntityFramework/champion.db-shm create mode 100644 Sources/EntityFramework/champion.db-wal diff --git a/Sources/Api_UT/EntityTest.cs b/Sources/Api_UT/EntityTest.cs index 6de3a08..b2623fa 100644 --- a/Sources/Api_UT/EntityTest.cs +++ b/Sources/Api_UT/EntityTest.cs @@ -23,9 +23,9 @@ namespace Api_UT using (var context = new LoLDbContext(options)) { - ChampionEntity chewie = new ChampionEntity("Chewbacca"); - ChampionEntity yoda = new ChampionEntity("Yoda"); - ChampionEntity ewok = new ChampionEntity("Ewok"); + ChampionEntity chewie = new ChampionEntity("Chewbacca","bio","icon"); + ChampionEntity yoda = new ChampionEntity("Yoda", "bio", "icon"); + ChampionEntity ewok = new ChampionEntity("Ewok", "bio", "icon"); Console.WriteLine("Creates and inserts new Champion for tests"); diff --git a/Sources/Api_UT/UnitTest1.cs b/Sources/Api_UT/UnitTest1.cs index 3b74199..5fba47b 100644 --- a/Sources/Api_UT/UnitTest1.cs +++ b/Sources/Api_UT/UnitTest1.cs @@ -1,8 +1,12 @@ using API_LoL.Controllers; using DTO; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore.Query; using Model; using StubLib; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; namespace Api_UT { @@ -14,9 +18,9 @@ namespace Api_UT { List list = new List {new ChampionDTO("Akali","",""), new ChampionDTO("Aatrox", "", ""), new ChampionDTO("Ahri", "", ""), new ChampionDTO("Akshan", "", ""), new ChampionDTO("Bard", "", ""), new ChampionDTO("Alistar", "", "") }; ChampionsController api = new ChampionsController(new StubData()); - IActionResult a = await api.Get(); + IEnumerable a = ((OkObjectResult)await api.Get()).Value as IEnumerable; Assert.IsNotNull(a); - Assert.AreEqual(list,((OkObjectResult)a).Value); + Assert.IsTrue(list.SequenceEqual(a)); } [TestMethod] @@ -26,7 +30,7 @@ namespace Api_UT IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon")); Assert.IsNotNull(a); ChampionDTO champ = new ChampionDTO("nom", "bio", "icon"); - //Assert.AreEqual(champ,((CreatedAtActionResult)a).Value); + Assert.IsTrue(champ.equals((ChampionDTO)((CreatedAtActionResult)a).Value)); } } diff --git a/Sources/ConsoleTestAPI/ConsoleTestAPI.csproj b/Sources/ConsoleTestAPI/ConsoleTestAPI.csproj new file mode 100644 index 0000000..a2be5fa --- /dev/null +++ b/Sources/ConsoleTestAPI/ConsoleTestAPI.csproj @@ -0,0 +1,20 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/Sources/ConsoleTestAPI/Program.cs b/Sources/ConsoleTestAPI/Program.cs new file mode 100644 index 0000000..0f9440f --- /dev/null +++ b/Sources/ConsoleTestAPI/Program.cs @@ -0,0 +1,194 @@ +// See https://aka.ms/new-console-template for more information +using System.Net.Security; +using Model; +using System.Net.Http; +using System.Reflection.Metadata; +using Newtonsoft.Json; +using DTO; +using System.Text.Json; +using Microsoft.AspNetCore.Mvc; +using System.Net.Http.Json; + +class APIResponse +{ + public string status { get; set; } + public List champions { get; set; } +} + + +static class Program +{ + + static HttpClient client = new HttpClient(); + static async Task Main(string[] args) { + HttpClient client = new HttpClient(); + await DisplayMainMenu(); + } + + public static async Task DisplayMainMenu() + { + Dictionary choices = new Dictionary() + { + [1] = "1- Manage Champions", + [2] = "2- Manage Skins", + [3] = "3- Manage Runes", + [4] = "4- Manage Rune Pages", + [99] = "99- Quit" + }; + + while (true) + { + int input = DisplayAMenu(choices); + + switch (input) + { + case 1: + await DisplayChampionsMenu(); + break; + case 2: + break; + case 3: + break; + case 4: + break; + case 99: + Console.WriteLine("Bye bye!"); + return; + default: + break; + } + } + } + + private static int DisplayAMenu(Dictionary choices) + { + int input = -1; + while (true) + { + Console.WriteLine("What is your choice?"); + Console.WriteLine("--------------------"); + foreach (var choice in choices.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value)) + { + Console.WriteLine(choice); + } + if (!int.TryParse(Console.ReadLine(), out input) || input == -1) + { + Console.WriteLine("I do not understand what your choice is. Please try again."); + continue; + } + break; + } + Console.WriteLine($"You have chosen: {choices[input]}"); + Console.WriteLine(); + return input; + } + + public static async Task DisplayChampionsMenu() + { + Dictionary choices = new Dictionary() + { + [0] = "0- Get number of champions", + [1] = "1- Get champions", + [2] = "2- Find champions by name", + [3] = "3- Find champions by characteristic", + [4] = "4- Find champions by class", + [5] = "5- Find champions by skill", + [6] = "6- Add new champion", + [7] = "7- Delete a champion", + [8] = "8- Update a champion", + }; + + int input = DisplayAMenu(choices); + + switch (input) + { + case 0: + + case 1: + { + var response = await client.GetFromJsonAsync( + "https://localhost:7144/api/Champions"); + + + + Console.WriteLine(response.ToString()); + string? json = response.ToString(); + List f = System.Text.Json.JsonSerializer.Deserialize>(json: json); + foreach(var c in f) + { + Console.WriteLine(c.ToString()); + } + + } + break; + case 2: + { + + } + break; + case 3: + { + + } + break; + case 4: + { + + } + break; + case 5: + { + + } + break; + case 6: + { + + } + break; + case 7: + { + + } + break; + case 8: + { + + } + break; + default: + break; + } + + } + + public static void DisplayCreationChampionMenu(Champion champion) + { + Dictionary choices = new Dictionary() + { + [1] = "1- Add a skill", + [2] = "2- Add a skin", + [3] = "3- Add a characteristic", + [99] = "99- Finish" + }; + + while (true) + { + int input = DisplayAMenu(choices); + + switch (input) + { + case 1: + + case 2: + + case 3: + + case 99: + return; + default: + break; + } + } + } +} \ No newline at end of file diff --git a/Sources/DTO/ChampionDTO.cs b/Sources/DTO/ChampionDTO.cs index c6bb27c..5c0344b 100644 --- a/Sources/DTO/ChampionDTO.cs +++ b/Sources/DTO/ChampionDTO.cs @@ -1,4 +1,6 @@ -namespace DTO +using Model; + +namespace DTO { public class ChampionDTO { @@ -11,10 +13,20 @@ public string Name { get; set; } public string Bio { get; set; } - + //public ChampionClass Class { get; set; } public string Icon { get; set; } + + public bool equals(ChampionDTO other) + { + return other.Name==Name && other.Bio==Bio && other.Icon==Icon; + } + + public string toString() + { + return Name + Bio + Icon; + } } } \ No newline at end of file diff --git a/Sources/DTO/DTO.csproj b/Sources/DTO/DTO.csproj index 132c02c..ac52241 100644 --- a/Sources/DTO/DTO.csproj +++ b/Sources/DTO/DTO.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs index c4ce92c..ab87c87 100644 --- a/Sources/EntityFramework/ChampionEntity.cs +++ b/Sources/EntityFramework/ChampionEntity.cs @@ -14,11 +14,29 @@ namespace EntityFramework [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } - public string name { get; set; } + [Required] + [MaxLength(50)] + public string Name { get; set; } - public ChampionEntity(string name) { - this.name = name; + + [MaxLength(500)] + [Column("Bio", TypeName = "string")] + public string Bio { get; set; } + + [Required] + public string Icon { get; set; } + + + public ChampionEntity(string name,string bio,string icon) { + this.Name = name; + this.Bio = bio; + this.Icon = icon; + } + + public override string ToString() + { + return Name; } } } diff --git a/Sources/EntityFramework/LoLDbContext.cs b/Sources/EntityFramework/LoLDbContext.cs index 4b89834..9fa1567 100644 --- a/Sources/EntityFramework/LoLDbContext.cs +++ b/Sources/EntityFramework/LoLDbContext.cs @@ -26,5 +26,28 @@ namespace EntityFramework options.UseSqlite("Data Source=champion.db"); } } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasKey(entity => entity.Id); + modelBuilder.Entity().ToTable("Champion"); + + modelBuilder.Entity().Property(entity => entity.Id) + .ValueGeneratedOnAdd(); + + modelBuilder.Entity().Property(entity => entity.Name) + .IsRequired() + .HasMaxLength(50); + + modelBuilder.Entity().Property(entity => entity.Bio) + .HasMaxLength(500) + .HasColumnName("Bio") + .HasColumnType("string"); + + modelBuilder.Entity().Property(entity => entity.Icon) + .IsRequired(); + + + } } } diff --git a/Sources/EntityFramework/Migrations/20230222160559_myMigration.Designer.cs b/Sources/EntityFramework/Migrations/20230222160559_myMigration.Designer.cs new file mode 100644 index 0000000..75a6b54 --- /dev/null +++ b/Sources/EntityFramework/Migrations/20230222160559_myMigration.Designer.cs @@ -0,0 +1,50 @@ +// +using EntityFramework; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFramework.Migrations +{ + [DbContext(typeof(LoLDbContext))] + [Migration("20230222160559_myMigration")] + partial class myMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("EntityFramework.ChampionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Bio") + .IsRequired() + .HasMaxLength(500) + .HasColumnType("string") + .HasColumnName("Bio"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Champion", (string)null); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFramework/Migrations/20230222160559_myMigration.cs b/Sources/EntityFramework/Migrations/20230222160559_myMigration.cs new file mode 100644 index 0000000..fc395ae --- /dev/null +++ b/Sources/EntityFramework/Migrations/20230222160559_myMigration.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EntityFramework.Migrations +{ + /// + public partial class myMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Champion", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", maxLength: 50, nullable: false), + Bio = table.Column(type: "string", maxLength: 500, nullable: false), + Icon = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Champion", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Champion"); + } + } +} diff --git a/Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs b/Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs new file mode 100644 index 0000000..9c51e74 --- /dev/null +++ b/Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs @@ -0,0 +1,47 @@ +// +using EntityFramework; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFramework.Migrations +{ + [DbContext(typeof(LoLDbContext))] + partial class LoLDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("EntityFramework.ChampionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Bio") + .IsRequired() + .HasMaxLength(500) + .HasColumnType("string") + .HasColumnName("Bio"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Champion", (string)null); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFramework/Program.cs b/Sources/EntityFramework/Program.cs index 29ee9d8..d3d785d 100644 --- a/Sources/EntityFramework/Program.cs +++ b/Sources/EntityFramework/Program.cs @@ -1,10 +1,21 @@ // See https://aka.ms/new-console-template for more information using EntityFramework; -Console.WriteLine("Hello, World!"); - using( var context = new LoLDbContext()) { - context.Add(new ChampionEntity("test") ); + context.Add(new ChampionEntity("test","test","test") ); context.SaveChanges(); -} \ No newline at end of file + + ChampionEntity champ = context.Find(1); + + if( champ != null) + { + Console + .WriteLine(champ.ToString()); + + } + else + { + Console.WriteLine("Not Found"); + } +} diff --git a/Sources/EntityFramework/champion.db b/Sources/EntityFramework/champion.db index df42e8e6ad2fa6f0d00384f0f839b96c2de7b076..393af3dfdd7a254c53fb36ee71f2d2656da4ffba 100644 GIT binary patch delta 271 zcmZozz}T>WQ6@OhC$l6~AuYcsH?c&)m_dMniHX5ML4kpRfoYQDRAEexA9Wfu0c;(3On*j~MtL0rlMASCM6QWQ6@OhC$l6~AuYcsH?c&)m_dMnk&(ecL4kpRfpwycvnVTrUc3}9{|^Qh z-rEd(m-yH6>GIy*EGUr9J9!CjFsBkTySS_@W0UwKzE+mJ#N5=)2l!kVxtRHz82GR8 zui%_;Zn(nqSC;`$P9-{E?%H-nE59$@ZaIz&OZt0q!#{q6=q*XLM~!PQ-swqOv=o@ zoCuS!y949`M*i;%Km-AsB@CYN3$rsaGl-U?7MDQeKQr)u-YjVFlAo8Gky)1085%+W DZ}@PO diff --git a/Sources/EntityFramework/champion.db-shm b/Sources/EntityFramework/champion.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..a58b0f7697a2b9ffc4931c977583c281b6e8b593 GIT binary patch literal 32768 zcmeI)zX`%X6bIl-{8PlnGE3Nr3)rW#wNo6x32X#cZ~}1?3n#F00tXQE?ywNMbOyd3 zJnl%syURDgtM8MLQpPGm7!Tt*i1pk&n)&p$KW}HJ>+({qcB93yS|93`-^W%tsqgD6 zpU3&!wC~4C=l-tgoe&^EfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBo5xi$S!Sc=X*3(2O zQ@CGyxm%Rh$lreP<9L2EJi45grZ%5SE(!qz5I_I{1Q0*~0R#|0009JsLBP=|<+^T= zI}GAm#!5wZyf5%->9S5&&)a)}rZUZ87@QdpKmY**5I_I{1Q0*~0R#|0;8$Q$+nI6C z-`+jQ@9#-3krx`{?fB*srAb!jR!2CX zi@ZQ3KX-jr&(+#_frc`T0UMj+B7gt_2q1s}0tg_000IagfWSWjQxX@@aa_QX4-