From af5b49759c17aadf5108dfb0d717f5bf3ca70b20 Mon Sep 17 00:00:00 2001 From: Tom Rambeau Date: Thu, 15 Feb 2024 17:36:11 +0100 Subject: [PATCH] avancement part4 --- tp1/Entities/BookEntity.cs | 1 - tp1/Model/Book.cs | 47 ++++++++ tp1/Model/BookOrderCritera.cs | 16 +++ tp1/Model/IDataManager.cs | 21 ++++ tp1/Model/Model.csproj | 9 ++ tp1/Model2Entities/DbDataManager.cs | 26 ++++ tp1/Model2Entities/Extensions.cs | 20 ++++ tp1/Model2Entities/Model2Entities.csproj | 9 ++ ...igner.cs => 20240215154956_Mg.Designer.cs} | 4 +- ...40215144801_Mg.cs => 20240215154956_Mg.cs} | 24 ++-- .../Migrations/StubbedContextModelSnapshot.cs | 2 +- tp1/StubbedContextLib/StubbedContext.cs | 3 - tp1/TestModel2Entities/Program.cs | 3 + .../TestModel2Entities.csproj | 10 ++ tp1/TestStub/tp.Books.db | Bin 28672 -> 28672 bytes tp1/UnitTests/UnitTest1.cs | 113 +++++++++--------- 16 files changed, 233 insertions(+), 75 deletions(-) create mode 100644 tp1/Model/Book.cs create mode 100644 tp1/Model/BookOrderCritera.cs create mode 100644 tp1/Model/IDataManager.cs create mode 100644 tp1/Model/Model.csproj create mode 100644 tp1/Model2Entities/DbDataManager.cs create mode 100644 tp1/Model2Entities/Extensions.cs create mode 100644 tp1/Model2Entities/Model2Entities.csproj rename tp1/StubbedContextLib/Migrations/{20240215144801_Mg.Designer.cs => 20240215154956_Mg.Designer.cs} (97%) rename tp1/StubbedContextLib/Migrations/{20240215144801_Mg.cs => 20240215154956_Mg.cs} (89%) create mode 100644 tp1/TestModel2Entities/Program.cs create mode 100644 tp1/TestModel2Entities/TestModel2Entities.csproj diff --git a/tp1/Entities/BookEntity.cs b/tp1/Entities/BookEntity.cs index 25062c3..b107f2a 100644 --- a/tp1/Entities/BookEntity.cs +++ b/tp1/Entities/BookEntity.cs @@ -7,7 +7,6 @@ using System.Threading.Tasks; namespace Entities { - [Table("TableBook")] public class BookEntity { public long Id { get; set; } diff --git a/tp1/Model/Book.cs b/tp1/Model/Book.cs new file mode 100644 index 0000000..16ef9d3 --- /dev/null +++ b/tp1/Model/Book.cs @@ -0,0 +1,47 @@ +using System.Reflection; + +namespace Model +{ + public class Book + { + + private long id; + public long Id + { + get { return id; } + set { id = value;} + } + + private string title; + public string Title + { + get { return title; } + set { title = value; } + } + + private string author; + public string Author + { + get { return author; } + set { author = value; } + } + + private string isbn; + public string Isbn { + get { return isbn; } + set { isbn = value; } + } + public Book(long id, string title, string author, string isbn) + { + Id = id; + Title = title; + Author = author; + Isbn = isbn; + } + + } + + + +} + \ No newline at end of file diff --git a/tp1/Model/BookOrderCritera.cs b/tp1/Model/BookOrderCritera.cs new file mode 100644 index 0000000..fdc2dbe --- /dev/null +++ b/tp1/Model/BookOrderCritera.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model +{ + public enum BookOrderCriteria + { + None, + ByTitle, + ByAuthor, + ByIsbn + } +} diff --git a/tp1/Model/IDataManager.cs b/tp1/Model/IDataManager.cs new file mode 100644 index 0000000..eeed534 --- /dev/null +++ b/tp1/Model/IDataManager.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model +{ + public interface IDataManager + { + //public IEnumerable GetBooks(int index, int count, BookOrderCriteria orderCriterium); + //public IEnumerable GetBooksByTitle(string title, int index, int count, BookOrderCriteria orderCriterium); + //public IEnumerable GetBooksByAuthor(string author, int index, int count, BookOrderCriteria orderCriterium); + //public IEnumerable GetBooksByIsbn(string isbn, int index, int count, BookOrderCriteria orderCriterium); + public Book GetBookById(long id); + + //public void CreateBook(string title, string author, string isbn); + //public void UpdateBook(long id, Book book); + //public void DeleteBook(long id); + } +} diff --git a/tp1/Model/Model.csproj b/tp1/Model/Model.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/tp1/Model/Model.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/tp1/Model2Entities/DbDataManager.cs b/tp1/Model2Entities/DbDataManager.cs new file mode 100644 index 0000000..cf4d1ea --- /dev/null +++ b/tp1/Model2Entities/DbDataManager.cs @@ -0,0 +1,26 @@ +using DbContextLib; +using Entities; +using Model; +namespace Model2Entities +{ + public class DbDataManager : IDataManager + { + public Book GetBookById(long id) + { + List books = new List(); + using (var context = new LibraryContext()) + { + foreach (var i in context.BooksSet) + { + if (i.Id == id) + { + return i.ConvertToModel(); + } + } + return null; + } + } + + + } +} diff --git a/tp1/Model2Entities/Extensions.cs b/tp1/Model2Entities/Extensions.cs new file mode 100644 index 0000000..9ec1105 --- /dev/null +++ b/tp1/Model2Entities/Extensions.cs @@ -0,0 +1,20 @@ +using Entities; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace Model2Entities +{ + public static class Extensions + { + public static Book ConvertToModel(this BookEntity book) + { + return new Book(book.Id, book.Title, book.Author, book.Isbn); + } + + } +} diff --git a/tp1/Model2Entities/Model2Entities.csproj b/tp1/Model2Entities/Model2Entities.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/tp1/Model2Entities/Model2Entities.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/tp1/StubbedContextLib/Migrations/20240215144801_Mg.Designer.cs b/tp1/StubbedContextLib/Migrations/20240215154956_Mg.Designer.cs similarity index 97% rename from tp1/StubbedContextLib/Migrations/20240215144801_Mg.Designer.cs rename to tp1/StubbedContextLib/Migrations/20240215154956_Mg.Designer.cs index 91a00cc..0b53312 100644 --- a/tp1/StubbedContextLib/Migrations/20240215144801_Mg.Designer.cs +++ b/tp1/StubbedContextLib/Migrations/20240215154956_Mg.Designer.cs @@ -11,7 +11,7 @@ using StubbedContextLib; namespace StubbedContextLib.Migrations { [DbContext(typeof(StubbedContext))] - [Migration("20240215144801_Mg")] + [Migration("20240215154956_Mg")] partial class Mg { /// @@ -48,7 +48,7 @@ namespace StubbedContextLib.Migrations b.HasIndex("OwnerId"); - b.ToTable("TableBook"); + b.ToTable("BooksSet"); b.HasData( new diff --git a/tp1/StubbedContextLib/Migrations/20240215144801_Mg.cs b/tp1/StubbedContextLib/Migrations/20240215154956_Mg.cs similarity index 89% rename from tp1/StubbedContextLib/Migrations/20240215144801_Mg.cs rename to tp1/StubbedContextLib/Migrations/20240215154956_Mg.cs index 1cbb84f..c975e69 100644 --- a/tp1/StubbedContextLib/Migrations/20240215144801_Mg.cs +++ b/tp1/StubbedContextLib/Migrations/20240215154956_Mg.cs @@ -27,7 +27,7 @@ namespace StubbedContextLib.Migrations }); migrationBuilder.CreateTable( - name: "TableBook", + name: "BooksSet", columns: table => new { Id = table.Column(type: "INTEGER", nullable: false) @@ -40,21 +40,16 @@ namespace StubbedContextLib.Migrations }, constraints: table => { - table.PrimaryKey("PK_TableBook", x => x.Id); + table.PrimaryKey("PK_BooksSet", x => x.Id); table.ForeignKey( - name: "FK_TableBook_PersonSet_OwnerId", + name: "FK_BooksSet_PersonSet_OwnerId", column: x => x.OwnerId, principalTable: "PersonSet", principalColumn: "Id"); }); migrationBuilder.InsertData( - table: "PersonSet", - columns: new[] { "Id", "FirstName", "LastName" }, - values: new object[] { 1, "coco", "test" }); - - migrationBuilder.InsertData( - table: "TableBook", + table: "BooksSet", columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" }, values: new object[,] { @@ -62,9 +57,14 @@ namespace StubbedContextLib.Migrations { 2L, "test2", "test2", null, 1, "test2" } }); + migrationBuilder.InsertData( + table: "PersonSet", + columns: new[] { "Id", "FirstName", "LastName" }, + values: new object[] { 1, "coco", "test" }); + migrationBuilder.CreateIndex( - name: "IX_TableBook_OwnerId", - table: "TableBook", + name: "IX_BooksSet_OwnerId", + table: "BooksSet", column: "OwnerId"); } @@ -72,7 +72,7 @@ namespace StubbedContextLib.Migrations protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( - name: "TableBook"); + name: "BooksSet"); migrationBuilder.DropTable( name: "PersonSet"); diff --git a/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs b/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs index 061de25..f41c0ff 100644 --- a/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs +++ b/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs @@ -45,7 +45,7 @@ namespace StubbedContextLib.Migrations b.HasIndex("OwnerId"); - b.ToTable("TableBook"); + b.ToTable("BooksSet"); b.HasData( new diff --git a/tp1/StubbedContextLib/StubbedContext.cs b/tp1/StubbedContextLib/StubbedContext.cs index 3012431..cc3fdff 100644 --- a/tp1/StubbedContextLib/StubbedContext.cs +++ b/tp1/StubbedContextLib/StubbedContext.cs @@ -12,7 +12,6 @@ namespace StubbedContextLib { public class StubbedContext : LibraryContext { - public StubbedContext() { } @@ -32,8 +31,6 @@ namespace StubbedContextLib } - - public StubbedContext(DbContextOptions options) : base(options) diff --git a/tp1/TestModel2Entities/Program.cs b/tp1/TestModel2Entities/Program.cs new file mode 100644 index 0000000..3ed0e5d --- /dev/null +++ b/tp1/TestModel2Entities/Program.cs @@ -0,0 +1,3 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); + diff --git a/tp1/TestModel2Entities/TestModel2Entities.csproj b/tp1/TestModel2Entities/TestModel2Entities.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/tp1/TestModel2Entities/TestModel2Entities.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/tp1/TestStub/tp.Books.db b/tp1/TestStub/tp.Books.db index 538a1230c96f22060f33321e60de0362c8ed3bb1..36925173549b1f11bd687acba334abdc2f9908c5 100644 GIT binary patch delta 279 zcmZp8z}WDBaY7c~Nd~@4{A>AGc{lUubD!KSC=ke9pTfp2ZY;~#$eNj#l3L*z5$}|r zpIscBS`zPHo|jtWnF8apI|sQshPWzt`nkA9C@5i3siffVr=SE=ucV-%1T{iQvzd{V zU0ha{u{E?LF)1e%rp75>3St=>)bJ2TCm&a+8I!H~3^`ybC(q(J0_SW_=j{<k(Kj delta 291 zcmZp8z}WDBaY7c~0S3NH{A>AGc{lTDaUa;&=)ql|!Nx9bD$m%+nVFZ8THzTHACj1q zlj@Y8pB?XCo|jtWnF1GJcMfuO3~^QP^mB2IP*B3AR!PC%PeBQ;LrFnH32KUxW-}Wr zySThOV{3E?$O?!e6`)0M() + .UseSqlite() + .Options; + + //prepares the database with one instance of the context + using (var context = new StubbedContext(options)) + { + + context.PersonSet.Include(pers => pers.Books).ToList(); + var allPers = context.PersonSet; + foreach (var person in allPers) + { + context.PersonSet.Remove(person); + } + + Assert.Equal(0, context.PersonSet.Count()); + context.SaveChanges(); + } + } + + [Fact] + public void Delete_TestBook() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder().UseSqlite().Options; + + //prepares the database with one instance of the context + + using (var context = new StubbedContext(options)) + { + context.PersonSet.Include(pers => pers.Books).ToList(); + var allBooks = context.BooksSet; + foreach (var book in allBooks) + { + context.BooksSet.Remove(book); + } + + Assert.Equal(0, context.BooksSet.Count()); + context.SaveChanges(); + } + } + + [Fact] public void Add_TestBooks() { @@ -25,9 +74,9 @@ namespace UnitTests //context.Database.OpenConnection(); context.Database.EnsureCreated(); context.PersonSet.Include(pers => pers.Books).ToList(); - BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =3 }; - BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 }; - BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 }; + BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =8 }; + BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 10 }; + BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 9 }; context.BooksSet.Add(the100); context.BooksSet.Add(princeOfPersia); context.BooksSet.Add(PercyJackson); @@ -84,7 +133,7 @@ namespace UnitTests var options = new DbContextOptionsBuilder().UseSqlite().Options; //prepares the database with one instance of the context - + //prepares the database with one instance of the context using (var context = new StubbedContext(options)) { @@ -119,7 +168,6 @@ namespace UnitTests //prepares the database with one instance of the context - //prepares the database with one instance of the context using (var context = new StubbedContext(options)) { @@ -143,55 +191,10 @@ namespace UnitTests } } - [Fact] - public void Delete_TestPerson() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - var options = new DbContextOptionsBuilder().UseSqlite().Options; - - //prepares the database with one instance of the context - using (var context = new StubbedContext(options)) - { - - context.PersonSet.Include(pers => pers.Books).ToList(); - var nb = context.PersonSet.Count(); - if (nb > 0) - { - context.PersonSet.Remove(context.PersonSet.FirstOrDefault()); - } - - Assert.Equal(context.PersonSet.Count(), nb - 1); - context.SaveChanges(); - } - } - - [Fact] - public void Delete_TestBook() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - var options = new DbContextOptionsBuilder().UseSqlite().Options; - - //prepares the database with one instance of the context - - //prepares the database with one instance of the context - using (var context = new StubbedContext(options)) - { - context.PersonSet.Include(pers => pers.Books).ToList(); - var nb = context.BooksSet.Count(); - if (nb > 0) - { - context.BooksSet.Remove(context.BooksSet.FirstOrDefault()); - } - Assert.Equal(context.BooksSet.Count(), nb - 1); - context.SaveChanges(); - } - } - + [Fact] - public void Delete_TestEmprunt() + public void Update_TestEmprunt() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); @@ -199,7 +202,6 @@ namespace UnitTests //prepares the database with one instance of the context - //prepares the database with one instance of the context using (var context = new StubbedContext(options)) { var books = context.BooksSet; @@ -223,7 +225,7 @@ namespace UnitTests } [Fact] - public void Delete_TestRendu() + public void Update_TestRendu() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); @@ -231,8 +233,7 @@ namespace UnitTests //prepares the database with one instance of the context - //prepares the database with one instance of the context - using (var context = new StubbedContext(options)) + using (var context = new StubbedContext(options)) { var books = context.BooksSet;