Merge pull request 'part2' (#1) from part2 into master

Reviewed-on: #1
master
Clement LESME 1 year ago
commit 428c5e7b37

@ -19,6 +19,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ContextLib\ContextLib.csproj" /> <ProjectReference Include="..\ContextLib\ContextLib.csproj" />
<ProjectReference Include="..\StubContext\StubbedContextLib.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -3,24 +3,24 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188 VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "App", "App.csproj", "{E8B1B649-B0B8-4DDF-BE5E-7EB74EE8E79A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "App.csproj", "{E8B1B649-B0B8-4DDF-BE5E-7EB74EE8E79A}"
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
{2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} = {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} = {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A}
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "..\Entities\Entities.csproj", "{A3AD5ED6-188F-4F13-8B43-F649502A9DED}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "..\Entities\Entities.csproj", "{A3AD5ED6-188F-4F13-8B43-F649502A9DED}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContextLib", "..\ContextLib\ContextLib.csproj", "{2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContextLib", "..\ContextLib\ContextLib.csproj", "{2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A}"
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
{A3AD5ED6-188F-4F13-8B43-F649502A9DED} = {A3AD5ED6-188F-4F13-8B43-F649502A9DED} {A3AD5ED6-188F-4F13-8B43-F649502A9DED} = {A3AD5ED6-188F-4F13-8B43-F649502A9DED}
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubContext", "..\StubContext\StubContext.csproj", "{98FB6B00-686C-4B31-B000-876B08E5DAEA}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "..\StubContext\StubbedContextLib.csproj", "{98FB6B00-686C-4B31-B000-876B08E5DAEA}"
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
{2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} = {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} = {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A}
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "..\Test\Test.csproj", "{E8159099-8CD9-49FF-A5D5-95B651756C19}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "..\Test\Test.csproj", "{E8159099-8CD9-49FF-A5D5-95B651756C19}"
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
{98FB6B00-686C-4B31-B000-876B08E5DAEA} = {98FB6B00-686C-4B31-B000-876B08E5DAEA} {98FB6B00-686C-4B31-B000-876B08E5DAEA} = {98FB6B00-686C-4B31-B000-876B08E5DAEA}
EndProjectSection EndProjectSection

@ -1,12 +1,12 @@
using listBooksEF; using ContextLib;
using Entities;
using Microsoft.Extensions.DependencyModel; using Microsoft.Extensions.DependencyModel;
using Microsoft.Identity.Client;
using StubbedContextLib;
using System.Diagnostics; using System.Diagnostics;
using Xunit; using StubbedContextLib;
List<BookEntity> books = new List<BookEntity>(); List<BookEntity> books = new List<BookEntity>();
StubbedContext myStub = new StubbedContext(); List<PersonEntity> persons = new List<PersonEntity>();
StubContext myStub = new StubContext();
// Insertion de livres dans la base de données // Insertion de livres dans la base de données
using (var context = new LibraryContext()) using (var context = new LibraryContext())
@ -15,22 +15,29 @@ using (var context = new LibraryContext())
books = await myStub.booksStub(); books = await myStub.booksStub();
// Ajoute chaque person à la base de données de manière asynchrone
foreach (var person in persons)
{
await context.PersonsSet.AddAsync(person);
}
// Ajoute chaque livre à la base de données de manière asynchrone // Ajoute chaque livre à la base de données de manière asynchrone
foreach (var book in books) foreach (var book in books)
{ {
await context.Books.AddAsync(book); await context.BooksSet.AddAsync(book);
} }
// Enregistre les modifications dans la base de données // Enregistre les modifications dans la base de données
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
/*
// Récupération et affichage des livres de la base de données // Récupération et affichage des livres de la base de données
using (var context = new LibraryContext()) using (var context = new LibraryContext())
{ {
Console.WriteLine("Récupération\n"); Console.WriteLine("Récupération\n");
foreach (var book in context.Books) foreach (var book in context.BooksSet)
{ {
Console.WriteLine($"{book.Title}, Auteur : {book.Author}"); Console.WriteLine($"{book.Title}, Auteur : {book.Author}");
} }
@ -41,10 +48,10 @@ using (var context = new LibraryContext())
{ {
Console.WriteLine("Suppression\n"); Console.WriteLine("Suppression\n");
var book = context.Books.FirstOrDefault(b => b.Id == 2); var book = context.BooksSet.FirstOrDefault(b => b.Id == 2);
if (book != null) if (book != null)
{ {
context.Books.Remove(book); context.BooksSet.Remove(book);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
} }
@ -54,17 +61,10 @@ using (var context = new LibraryContext())
{ {
Console.WriteLine("Modification\n"); Console.WriteLine("Modification\n");
var bookToUpdate = context.Books.FirstOrDefault(b => b.Author == "Martin"); var bookToUpdate = context.BooksSet.FirstOrDefault(b => b.Author == "Martin");
if (bookToUpdate != null) if (bookToUpdate != null)
{ {
bookToUpdate.Title = "Soir"; bookToUpdate.Title = "Soir";
await context.SaveChangesAsync(); 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);
}

@ -5,12 +5,14 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Entities; using Entities;
using Microsoft.EntityFrameworkCore.Metadata;
namespace ContextLib namespace ContextLib
{ {
public class LibraryContext : DbContext public class LibraryContext : DbContext
{ {
public DbSet<BookEntity> BooksSet { get; set; } public DbSet<BookEntity> BooksSet { get; set; }
public DbSet<PersonEntity> PersonsSet { get; set; }
public LibraryContext() { } public LibraryContext() { }

@ -1,47 +0,0 @@
// <auto-generated />
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("20240126215342_deuxiemeTestMigration")]
partial class deuxiemeTestMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<long>("id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("id");
b.ToTable("BooksSet");
});
#pragma warning restore 612, 618
}
}
}

@ -1,36 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ContextLib.Migrations
{
/// <inheritdoc />
public partial class deuxiemeTestMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "BooksSet",
columns: table => new
{
id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Title = table.Column<string>(type: "TEXT", nullable: false),
Author = table.Column<string>(type: "TEXT", nullable: false),
Isbn = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_BooksSet", x => x.id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BooksSet");
}
}
}

@ -1,47 +0,0 @@
// <auto-generated />
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("20240126215524_seuxiemeTestMigrationn")]
partial class seuxiemeTestMigrationn
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<long>("id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("id");
b.ToTable("BooksSet");
});
#pragma warning restore 612, 618
}
}
}

@ -1,22 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ContextLib.Migrations
{
/// <inheritdoc />
public partial class seuxiemeTestMigrationn : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

@ -1,44 +0,0 @@
// <auto-generated />
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<long>("id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("id");
b.ToTable("BooksSet");
});
#pragma warning restore 612, 618
}
}
}

@ -8,10 +8,17 @@ namespace Entities
{ {
public class BookEntity public class BookEntity
{ {
public long id { get; set; } public long Id { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Author { get; set; } public string Author { get; set; }
public string Isbn { get; set; } public string Isbn { get; set; }
public long? PersonEntityId { get; set; }
public PersonEntity? Owner { get; set; } = null!;
} }
} }

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entities
{
public class PersonEntity
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<BookEntity> Library { get; set; } = new List<BookEntity>();
}
}

@ -1,10 +1,13 @@
using System; using ContextLib;
using Entities;
using Microsoft.Extensions.DependencyModel;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StubContext namespace StubbedContextLib
{ {
public class StubContext : LibraryContext public class StubContext : LibraryContext
{ {
@ -15,13 +18,16 @@ namespace StubContext
//Création des livres //Création des livres
BookEntity book1 = new BookEntity BookEntity book1 = new BookEntity
{ {
Id = 1,
Author = "Lechardeur", Author = "Lechardeur",
Title = "Comment pécho 100% garanti", Title = "Comment pécho 100% garanti",
Isbn = "696969a" Isbn = "696969a",
PersonEntityId = 1,
}; };
BookEntity book2 = new BookEntity BookEntity book2 = new BookEntity
{ {
Id = 2,
Author = "Vianney", Author = "Vianney",
Title = "Gros", Title = "Gros",
Isbn = "0015150" Isbn = "0015150"
@ -29,18 +35,35 @@ namespace StubContext
BookEntity book3 = new BookEntity BookEntity book3 = new BookEntity
{ {
Id = 3,
Author = "Khéna", Author = "Khéna",
Title = "Khéna; un homme, un livre", Title = "Khéna; un homme, un livre",
Isbn = "666666b" Isbn = "666666b"
}; };
//Ajout des livres //Ajout des livres
books.Add(book1); listBook.Add(book1);
books.Add(book2); listBook.Add(book2);
books.Add(book3); listBook.Add(book3);
return listBook; return listBook;
} }
public async Task<List<PersonEntity>> personsStub()
{
List<PersonEntity> ListPerson = new List<PersonEntity>();
List<BookEntity> l = new List<BookEntity>();
PersonEntity person1 = new PersonEntity
{
Id = 1,
FirstName = "Khena",
LastName = "Bruneau",
};
ListPerson.Add(person1);
return ListPerson;
}
} }
} }

@ -0,0 +1,16 @@
using Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
public class Test
{
}
}

@ -16,7 +16,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\StubContext\StubContext.csproj" /> <ProjectReference Include="..\StubContext\StubbedContextLib.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

Loading…
Cancel
Save