|
|
|
@ -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<BookEntity> books = new List<BookEntity>();
|
|
|
|
|
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);
|
|
|
|
|
}
|