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 538a123..3692517 100644 Binary files a/tp1/TestStub/tp.Books.db and b/tp1/TestStub/tp.Books.db differ diff --git a/tp1/UnitTests/UnitTest1.cs b/tp1/UnitTests/UnitTest1.cs index d775f8c..2bd9991 100644 --- a/tp1/UnitTests/UnitTest1.cs +++ b/tp1/UnitTests/UnitTest1.cs @@ -11,6 +11,55 @@ namespace UnitTests { public class UnitTest1 { + [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 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;