From 2ca4b8ba05fe90399b8cd358bde9d77609ca2f33 Mon Sep 17 00:00:00 2001 From: cllesme1 Date: Fri, 26 Jan 2024 22:25:29 +0100 Subject: [PATCH] fin du TP --- App/Program.cs | 72 +++++++++++++++++- .../20240122161115_testMigration.Designer.cs | 47 ------------ .../20240122161115_testMigration.cs | 36 --------- .../Migrations/LibraryContextModelSnapshot.cs | 44 ----------- ContextLib/Test.Librairy.db | Bin 20480 -> 0 bytes StubContext/StubContext.cs | 46 +++++++++++ 6 files changed, 116 insertions(+), 129 deletions(-) delete mode 100644 ContextLib/Migrations/20240122161115_testMigration.Designer.cs delete mode 100644 ContextLib/Migrations/20240122161115_testMigration.cs delete mode 100644 ContextLib/Migrations/LibraryContextModelSnapshot.cs delete mode 100644 ContextLib/Test.Librairy.db create mode 100644 StubContext/StubContext.cs diff --git a/App/Program.cs b/App/Program.cs index 83fa4f4..7bdef8d 100644 --- a/App/Program.cs +++ b/App/Program.cs @@ -1,2 +1,70 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using listBooksEF; +using Microsoft.Extensions.DependencyModel; +using Microsoft.Identity.Client; +using StubbedContextLib; +using System.Diagnostics; +using Xunit; + +List books = new List(); +StubbedContext myStub = new StubbedContext(); + +// Insertion de livres dans la base de données +using (var context = new LibraryContext()) +{ + Console.WriteLine("Insertion\n"); + + books = await myStub.booksStub(); + + // Ajoute chaque livre à la base de données de manière asynchrone + foreach (var book in books) + { + await context.Books.AddAsync(book); + } + + // Enregistre les modifications dans la base de données + await context.SaveChangesAsync(); +} + +// Récupération et affichage des livres de la base de données +using (var context = new LibraryContext()) +{ + Console.WriteLine("Récupération\n"); + + foreach (var book in context.Books) + { + Console.WriteLine($"{book.Title}, Auteur : {book.Author}"); + } +} + +// Suppression d'un livre avec l'ID 2 de la base de données +using (var context = new LibraryContext()) +{ + Console.WriteLine("Suppression\n"); + + var book = context.Books.FirstOrDefault(b => b.Id == 2); + if (book != null) + { + context.Books.Remove(book); + await context.SaveChangesAsync(); + } +} + +// Modification du titre d'un livre avec l'auteur "Martin" +using (var context = new LibraryContext()) +{ + Console.WriteLine("Modification\n"); + + var bookToUpdate = context.Books.FirstOrDefault(b => b.Author == "Martin"); + if (bookToUpdate != null) + { + bookToUpdate.Title = "Soir"; + await context.SaveChangesAsync(); + } +} + +// Assertion sur le nombre de livres dans la base de données +using (var context = new LibraryContext()) +{ + var cpt = context.Books.Count(); + Assert.Equal(cpt, 2); +} \ No newline at end of file diff --git a/ContextLib/Migrations/20240122161115_testMigration.Designer.cs b/ContextLib/Migrations/20240122161115_testMigration.Designer.cs deleted file mode 100644 index af8fda0..0000000 --- a/ContextLib/Migrations/20240122161115_testMigration.Designer.cs +++ /dev/null @@ -1,47 +0,0 @@ -// -using ContextLib; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace ContextLib.Migrations -{ - [DbContext(typeof(LibraryContext))] - [Migration("20240122161115_testMigration")] - partial class testMigration - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "8.0.1"); - - modelBuilder.Entity("Entities.BookEntity", b => - { - b.Property("id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Author") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Isbn") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Title") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("id"); - - b.ToTable("BooksSet"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/ContextLib/Migrations/20240122161115_testMigration.cs b/ContextLib/Migrations/20240122161115_testMigration.cs deleted file mode 100644 index efa583e..0000000 --- a/ContextLib/Migrations/20240122161115_testMigration.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace ContextLib.Migrations -{ - /// - public partial class testMigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "BooksSet", - columns: table => new - { - id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Title = table.Column(type: "TEXT", nullable: false), - Author = table.Column(type: "TEXT", nullable: false), - Isbn = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_BooksSet", x => x.id); - }); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "BooksSet"); - } - } -} diff --git a/ContextLib/Migrations/LibraryContextModelSnapshot.cs b/ContextLib/Migrations/LibraryContextModelSnapshot.cs deleted file mode 100644 index fb2c280..0000000 --- a/ContextLib/Migrations/LibraryContextModelSnapshot.cs +++ /dev/null @@ -1,44 +0,0 @@ -// -using ContextLib; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace ContextLib.Migrations -{ - [DbContext(typeof(LibraryContext))] - partial class LibraryContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "8.0.1"); - - modelBuilder.Entity("Entities.BookEntity", b => - { - b.Property("id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Author") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Isbn") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Title") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("id"); - - b.ToTable("BooksSet"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/ContextLib/Test.Librairy.db b/ContextLib/Test.Librairy.db deleted file mode 100644 index 6f7d24949e793a4828d65d5fb7e5056e6ca45aa6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI&F>ljA6bJCLleRRH37e;ilf}@gRTKwGI@YZYu$nl*xuQl0S#EO$i_})_iv(lg zo3Qg`7@3%v85p@laobo9d#nGGPxjr}FMdCJv7N!|P9z0APtuD}(x>DR(RH#K4Z?L!S7|Jqdy!&EqFXL` zwf!_1P0r*Sk!I>-3u#qpuD)u%YilHmN8;1`@`e+c%=$s;?*w+~a7Eo{52iPpp_*3z zNz{UX00bZa0SG_<0uX=z1Rwwb2teS@2|Up%%xpDV&zg3tWk0uVdpD3Glh-dU_8QHG zT~l>w^> booksStub() + { + List listBook = new List(); + + //Création des livres + BookEntity book1 = new BookEntity + { + Author = "Lechardeur", + Title = "Comment pécho 100% garanti", + Isbn = "696969a" + }; + + BookEntity book2 = new BookEntity + { + Author = "Vianney", + Title = "Gros", + Isbn = "0015150" + }; + + BookEntity book3 = new BookEntity + { + Author = "Khéna", + Title = "Khéna; un homme, un livre", + Isbn = "666666b" + }; + + //Ajout des livres + books.Add(book1); + books.Add(book2); + books.Add(book3); + + return listBook; + } + + } +}