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 6f7d249..0000000 Binary files a/ContextLib/Test.Librairy.db and /dev/null differ diff --git a/StubContext/StubContext.cs b/StubContext/StubContext.cs new file mode 100644 index 0000000..03c4775 --- /dev/null +++ b/StubContext/StubContext.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StubContext +{ + public class StubContext : LibraryContext + { + public async Task> 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; + } + + } +}