Compare commits

...

4 Commits

@ -0,0 +1,89 @@
// <auto-generated />
using System;
using DbContextLib;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DbContextLib.Migrations
{
[DbContext(typeof(LibraryContext))]
[Migration("20240216040156_Mg")]
partial class Mg
{
/// <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<int?>("OwnerId")
.HasColumnType("INTEGER");
b.Property<int>("PersonId")
.HasColumnType("INTEGER");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("OwnerId");
b.ToTable("BooksSet");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("PersonSet");
});
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.HasOne("Entities.PersonEntity", "Owner")
.WithMany("Books")
.HasForeignKey("OwnerId");
b.Navigation("Owner");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Navigation("Books");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,65 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DbContextLib.Migrations
{
/// <inheritdoc />
public partial class Mg : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "PersonSet",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
FirstName = table.Column<string>(type: "TEXT", nullable: false),
LastName = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_PersonSet", x => x.Id);
});
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),
PersonId = table.Column<int>(type: "INTEGER", nullable: false),
OwnerId = table.Column<int>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_BooksSet", x => x.Id);
table.ForeignKey(
name: "FK_BooksSet_PersonSet_OwnerId",
column: x => x.OwnerId,
principalTable: "PersonSet",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_BooksSet_OwnerId",
table: "BooksSet",
column: "OwnerId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BooksSet");
migrationBuilder.DropTable(
name: "PersonSet");
}
}
}

@ -0,0 +1,86 @@
// <auto-generated />
using System;
using DbContextLib;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DbContextLib.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<int?>("OwnerId")
.HasColumnType("INTEGER");
b.Property<int>("PersonId")
.HasColumnType("INTEGER");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("OwnerId");
b.ToTable("BooksSet");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("PersonSet");
});
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.HasOne("Entities.PersonEntity", "Owner")
.WithMany("Books")
.HasForeignKey("OwnerId");
b.Navigation("Owner");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Navigation("Books");
});
#pragma warning restore 612, 618
}
}
}

@ -7,9 +7,9 @@ using System.Threading.Tasks;
namespace Entities namespace Entities
{ {
[Table("TableBook")]
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; }
@ -18,6 +18,6 @@ namespace Entities
public int PersonId { get; set; } public int PersonId { get; set; }
public PersonEntity? Owner { get; set; } public PersonEntity? Owner { get; set; }
} }
} }

@ -0,0 +1,53 @@
using System.Reflection;
using System.Text;
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;
}
public string Tostring(StringBuilder sb)
{
return Id.ToString() + " " + Title.ToString() + " " + Author.ToString() + " " + Isbn.ToString() ;
}
}
}

@ -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
}
}

@ -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<Book> GetBooks(int index, int count, BookOrderCriteria orderCriterium);
//public IEnumerable<Book> GetBooksByTitle(string title, int index, int count, BookOrderCriteria orderCriterium);
//public IEnumerable<Book> GetBooksByAuthor(string author, int index, int count, BookOrderCriteria orderCriterium);
//public IEnumerable<Book> 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);
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,140 @@
using DbContextLib;
using Entities;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Model;
namespace Model2Entities
{
public class DbDataManager : IDataManager
{
public void CreateBook(Book book) {
using (var context = new LibraryContext())
{
context.Database.EnsureCreated();
if (!context.BooksSet.Contains(book.ConvertToEntity())){
context.BooksSet.Add(book.ConvertToEntity());
}
context.SaveChanges();
}
}
public List<Book> GetAllBooks()
{
List<Book> books = new List<Book>();
using (var context = new LibraryContext())
{
foreach (var bookEntity in context.BooksSet) {
books.Add(bookEntity.ConvertToModel());
}
return books;
}
}
public Book GetBookById(long id)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Id == id)
{
return i.ConvertToModel();
}
}
return null;
}
}
public Book GetBookByAuthor(string author)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Author == author)
{
return i.ConvertToModel();
}
}
return null;
}
}
public Book GetBookByTitle(string title)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Title == title)
{
return i.ConvertToModel();
}
}
return null;
}
}
public Book GetBookByIsbn(string isbn)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Isbn == isbn)
{
return i.ConvertToModel();
}
}
return null;
}
}
public void UpdateAuthor(long id, string author)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Id == id)
{
i.Author = author;
}
}
context.SaveChanges();
}
}
public void DeleteBook(long id)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Id == id)
{
context.Remove(i);
}
}
context.SaveChanges();
}
}
public void DeleteAll()
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
context.Remove(i);
}
context.SaveChanges();
}
}
}
}

@ -0,0 +1,24 @@
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);
}
public static BookEntity ConvertToEntity(this Book book)
{
return new BookEntity() {Id= book.Id, Title = book.Title, Author = book.Author, Isbn = book.Isbn };
}
}
}

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -11,7 +11,7 @@ using StubbedContextLib;
namespace StubbedContextLib.Migrations namespace StubbedContextLib.Migrations
{ {
[DbContext(typeof(StubbedContext))] [DbContext(typeof(StubbedContext))]
[Migration("20240215144801_Mg")] [Migration("20240215195621_Mg")]
partial class Mg partial class Mg
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -48,7 +48,7 @@ namespace StubbedContextLib.Migrations
b.HasIndex("OwnerId"); b.HasIndex("OwnerId");
b.ToTable("TableBook"); b.ToTable("BooksSet");
b.HasData( b.HasData(
new new

@ -27,7 +27,7 @@ namespace StubbedContextLib.Migrations
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "TableBook", name: "BooksSet",
columns: table => new columns: table => new
{ {
Id = table.Column<long>(type: "INTEGER", nullable: false) Id = table.Column<long>(type: "INTEGER", nullable: false)
@ -40,21 +40,16 @@ namespace StubbedContextLib.Migrations
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_TableBook", x => x.Id); table.PrimaryKey("PK_BooksSet", x => x.Id);
table.ForeignKey( table.ForeignKey(
name: "FK_TableBook_PersonSet_OwnerId", name: "FK_BooksSet_PersonSet_OwnerId",
column: x => x.OwnerId, column: x => x.OwnerId,
principalTable: "PersonSet", principalTable: "PersonSet",
principalColumn: "Id"); principalColumn: "Id");
}); });
migrationBuilder.InsertData( migrationBuilder.InsertData(
table: "PersonSet", table: "BooksSet",
columns: new[] { "Id", "FirstName", "LastName" },
values: new object[] { 1, "coco", "test" });
migrationBuilder.InsertData(
table: "TableBook",
columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" }, columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" },
values: new object[,] values: new object[,]
{ {
@ -62,9 +57,14 @@ namespace StubbedContextLib.Migrations
{ 2L, "test2", "test2", null, 1, "test2" } { 2L, "test2", "test2", null, 1, "test2" }
}); });
migrationBuilder.InsertData(
table: "PersonSet",
columns: new[] { "Id", "FirstName", "LastName" },
values: new object[] { 1, "coco", "test" });
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_TableBook_OwnerId", name: "IX_BooksSet_OwnerId",
table: "TableBook", table: "BooksSet",
column: "OwnerId"); column: "OwnerId");
} }
@ -72,7 +72,7 @@ namespace StubbedContextLib.Migrations
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "TableBook"); name: "BooksSet");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "PersonSet"); name: "PersonSet");

@ -45,7 +45,7 @@ namespace StubbedContextLib.Migrations
b.HasIndex("OwnerId"); b.HasIndex("OwnerId");
b.ToTable("TableBook"); b.ToTable("BooksSet");
b.HasData( b.HasData(
new new

@ -12,7 +12,6 @@ namespace StubbedContextLib
{ {
public class StubbedContext : LibraryContext public class StubbedContext : LibraryContext
{ {
public StubbedContext() { public StubbedContext() {
} }
@ -32,8 +31,6 @@ namespace StubbedContextLib
} }
public StubbedContext(DbContextOptions<LibraryContext> options) public StubbedContext(DbContextOptions<LibraryContext> options)
: base(options) : base(options)

@ -0,0 +1,70 @@
using Model;
using Entities;
using Model2Entities;
using Microsoft.Extensions.DependencyModel;
using DbContextLib;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
DbDataManager dbDataManager = new DbDataManager();
dbDataManager.CreateBook(new Book(1, "mistake", "test1", "test1"));
dbDataManager.CreateBook(new Book(2, "the100", "test2", "test2"));
dbDataManager.CreateBook(new Book(3, "GOT", "lastTest", "lastTest"));
Console.WriteLine("test de récupération de tout les livres");
List<Book> books = dbDataManager.GetAllBooks();
foreach (var book in books)
{
Console.WriteLine("Titre du livre : " + book.Title);
}
Console.WriteLine("\ntest de récupération de livre par ID");
Book b1 = dbDataManager.GetBookById(3);
if (b1 != null)
{
Console.WriteLine("Le livre : " + b1.Title);
}
Console.WriteLine("\ntest de récupération de livre par Autheur");
Book b2 = dbDataManager.GetBookByIsbn("test2");
Console.WriteLine("Le livre : " + b2.Title);
Console.WriteLine("\ntest de récupération de livre par Titre");
Book b3 = dbDataManager.GetBookByTitle("the100");
Console.WriteLine("Le livre : " + b3.Title);
Console.WriteLine("\ntest de récupération de livre par ISBn");
Book b4 = dbDataManager.GetBookByAuthor("lastTest");
Console.WriteLine("Le livre : " + b4.Title);
Console.WriteLine("\n MAJ de l'auteur du livre th100");
dbDataManager.UpdateAuthor(2, "Kass Morgan");
Book the100 = dbDataManager.GetBookByAuthor("Kass Morgan");
Console.WriteLine(the100.Title + " " + the100.Author);
Console.WriteLine("\nsupresion du livre ou id = 1");
dbDataManager.DeleteBook(1);
Book test = dbDataManager.GetBookById(1);
if (test == null)
{
Console.WriteLine("livre supprimée");
}
Console.WriteLine("\nsuppression de tous les livres");
dbDataManager.DeleteAll();

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\Model2Entities\Model2Entities.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

Binary file not shown.

@ -13,6 +13,7 @@ namespace UnitTests
{ {
public class UnitTest1 public class UnitTest1
{ {
[Fact] [Fact]
public void Add_TestBooks() public void Add_TestBooks()
{ {
@ -39,7 +40,7 @@ namespace UnitTests
//uses another instance of the context to do the tests //uses another instance of the context to do the tests
//using (var context = new StubbedContext(options)) //using (var context = new StubbedContext(options))
using (var context = new StubbedContext()) using (var context = new StubbedContext(options))
{ {
context.Database.EnsureCreated(); context.Database.EnsureCreated();
Assert.Equal(3, context.BooksSet.Count()); Assert.Equal(3, context.BooksSet.Count());
@ -348,7 +349,5 @@ namespace UnitTests
} }
} }
} }
} }

@ -11,7 +11,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "Stubbe
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{5C673F26-2E43-4AA8-944E-1A3B309927CF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{5C673F26-2E43-4AA8-944E-1A3B309927CF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "UnitTests\UnitTests.csproj", "{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{69CD70C4-A73C-4C22-864D-B72204B7AD9D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model2Entities", "Model2Entities\Model2Entities.csproj", "{B5E6450F-A53F-4986-9740-A648E4E823EC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestModel2Entities", "TestModel2Entities\TestModel2Entities.csproj", "{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +45,18 @@ Global
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Debug|Any CPU.Build.0 = Debug|Any CPU {73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.ActiveCfg = Release|Any CPU {73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.Build.0 = Release|Any CPU {73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.Build.0 = Release|Any CPU
{69CD70C4-A73C-4C22-864D-B72204B7AD9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69CD70C4-A73C-4C22-864D-B72204B7AD9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69CD70C4-A73C-4C22-864D-B72204B7AD9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69CD70C4-A73C-4C22-864D-B72204B7AD9D}.Release|Any CPU.Build.0 = Release|Any CPU
{B5E6450F-A53F-4986-9740-A648E4E823EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5E6450F-A53F-4986-9740-A648E4E823EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5E6450F-A53F-4986-9740-A648E4E823EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5E6450F-A53F-4986-9740-A648E4E823EC}.Release|Any CPU.Build.0 = Release|Any CPU
{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Loading…
Cancel
Save