Fin du TP1, moitier du TP2

part2
Clement LESME 1 year ago
parent 1cb0feb1d4
commit 0a41a1ccd0

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

@ -3,24 +3,24 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
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
{2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} = {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A}
EndProjectSection
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
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
{A3AD5ED6-188F-4F13-8B43-F649502A9DED} = {A3AD5ED6-188F-4F13-8B43-F649502A9DED}
EndProjectSection
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
{2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} = {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A}
EndProjectSection
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
{98FB6B00-686C-4B31-B000-876B08E5DAEA} = {98FB6B00-686C-4B31-B000-876B08E5DAEA}
EndProjectSection

@ -1,12 +1,54 @@
using listBooksEF;
using ContextLib;
using Entities;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Identity.Client;
using StubbedContextLib;
using System.Diagnostics;
using Xunit;
using StubbedContextLib;
/*
namespace App
{
class Program
{
static void Main(string[] args)
{
BookEntity book2 = new BookEntity
{
Id = 2,
Author = "Vianney",
Title = "Gros",
Isbn = "0015150"
};
BookEntity book3 = new BookEntity
{
Id = 3,
Author = "Khéna",
Title = "Khéna. Un homme, un livre",
Isbn = "666666b"
};
using (var context = new LibraryContext())
{
Console.WriteLine("insertion");
context.Add(book2);
context.Add(book3);
context.SaveChanges();
}
using (var context = new LibraryContext())
{
foreach (var n in context.BooksSet)
{
Console.WriteLine($"{n.Id} - {n.Author}");
}
context.SaveChanges();
}
}
}
}*/
List<BookEntity> books = new List<BookEntity>();
StubbedContext myStub = new StubbedContext();
StubContext myStub = new StubContext();
// Insertion de livres dans la base de données
using (var context = new LibraryContext())
@ -18,7 +60,7 @@ using (var context = new LibraryContext())
// Ajoute chaque livre à la base de données de manière asynchrone
foreach (var book in books)
{
await context.Books.AddAsync(book);
await context.BooksSet.AddAsync(book);
}
// Enregistre les modifications dans la base de données
@ -30,7 +72,7 @@ using (var context = new LibraryContext())
{
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}");
}
@ -41,10 +83,10 @@ using (var context = new LibraryContext())
{
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)
{
context.Books.Remove(book);
context.BooksSet.Remove(book);
await context.SaveChangesAsync();
}
}
@ -54,17 +96,10 @@ using (var context = new LibraryContext())
{
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)
{
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);
}

@ -5,12 +5,14 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entities;
using Microsoft.EntityFrameworkCore.Metadata;
namespace ContextLib
{
public class LibraryContext : DbContext
{
public DbSet<BookEntity> BooksSet { get; set; }
public DbSet<PersonEntity> PersonsSet { get; set; }
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 long id { get; set; }
public long Id { get; set; }
public string Title { get; set; }
public string Author { 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,12 @@
using System;
using ContextLib;
using Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StubContext
namespace StubbedContextLib
{
public class StubContext : LibraryContext
{
@ -15,6 +17,7 @@ namespace StubContext
//Création des livres
BookEntity book1 = new BookEntity
{
Id = 1,
Author = "Lechardeur",
Title = "Comment pécho 100% garanti",
Isbn = "696969a"
@ -22,6 +25,7 @@ namespace StubContext
BookEntity book2 = new BookEntity
{
Id = 2,
Author = "Vianney",
Title = "Gros",
Isbn = "0015150"
@ -29,15 +33,16 @@ namespace StubContext
BookEntity book3 = new BookEntity
{
Id = 3,
Author = "Khéna",
Title = "Khéna; un homme, un livre",
Isbn = "666666b"
};
//Ajout des livres
books.Add(book1);
books.Add(book2);
books.Add(book3);
listBook.Add(book1);
listBook.Add(book2);
listBook.Add(book3);
return listBook;
}

@ -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>
<ProjectReference Include="..\StubContext\StubContext.csproj" />
<ProjectReference Include="..\StubContext\StubbedContextLib.csproj" />
</ItemGroup>
</Project>

Loading…
Cancel
Save