diff --git a/Verax_API_EF/API/API.csproj b/Verax_API_EF/API/API.csproj index 8632b39..01ea7d9 100644 --- a/Verax_API_EF/API/API.csproj +++ b/Verax_API_EF/API/API.csproj @@ -12,6 +12,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all diff --git a/Verax_API_EF/API/Controllers/ArticleController.cs b/Verax_API_EF/API/Controllers/ArticleController.cs index fb64675..5388d4d 100644 --- a/Verax_API_EF/API/Controllers/ArticleController.cs +++ b/Verax_API_EF/API/Controllers/ArticleController.cs @@ -29,7 +29,7 @@ namespace API.Controllers } [HttpGet("/article/{id}")] - public async Task GetArticleById(int id) + public async Task GetArticleById(int id) { var result = await _articleService.GetArticleById(id); if (result == null) @@ -42,19 +42,19 @@ namespace API.Controllers [HttpPost("/article")] - public async Task CreateArticle(long id, string title, string description, string author, string date, int lectureTime) + public async Task CreateArticle(long id, string title, string description, string author, string date, int lectureTime) { return await _articleService.CreateArticle(id, title, description, author, date, lectureTime); } [HttpDelete("/article/{id}")] - public async Task DeleteArticle(long id) + public async Task DeleteArticle(long id) { return await _articleService.DeleteArticle(id); } [HttpPut("/article/{id}")] - public async Task UpdateArticle(long id, Article? a) + public async Task UpdateArticle(long id, ArticleEntity? a) { return await _articleService.UpdateArticle(id, a); } diff --git a/Verax_API_EF/API/Program.cs b/Verax_API_EF/API/Program.cs index 4b93a95..8ed48e2 100644 --- a/Verax_API_EF/API/Program.cs +++ b/Verax_API_EF/API/Program.cs @@ -2,6 +2,7 @@ using API_Services; using DbContextLib; using DbDataManager; using Microsoft.EntityFrameworkCore; +using StubbedContextLib; var builder = WebApplication.CreateBuilder(args); @@ -14,8 +15,10 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); - -builder.Services.AddDbContext(); +builder.Services.AddDbContext(options => +{ + options.UseSqlite("Data Source=Entity_FrameWork.Article.db"); +}); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -31,7 +34,5 @@ if (app.Environment.IsDevelopment()) app.UseHttpsRedirection(); - - - app.Run(); + diff --git a/Verax_API_EF/API_DbDataManager/DbManagerArticle.cs b/Verax_API_EF/API_DbDataManager/DbManagerArticle.cs index ca98ae5..d0c6d0d 100644 --- a/Verax_API_EF/API_DbDataManager/DbManagerArticle.cs +++ b/Verax_API_EF/API_DbDataManager/DbManagerArticle.cs @@ -2,6 +2,7 @@ using API_Services; using DbContextLib; using Entities; using Model; +using ArticleEntity = Model.ArticleEntity; namespace DbDataManager; @@ -11,12 +12,13 @@ public class DbManagerArticle : IArticleService public DbManagerArticle(LibraryContext context) { - _context = new LibraryContext(); + _context = context; } + - public async Task CreateArticle(long id, string title, string description, string author, string date, int lectureTime) + public async Task CreateArticle(long id, string title, string description, string author, string date, int lectureTime) { - var entity = new ArticleEntity() + var entity = new Entities.ArticleEntity() { Id = id, Title = title, @@ -31,7 +33,7 @@ public class DbManagerArticle : IArticleService return entity.ToModel(); } - public async Task DeleteArticle(long id) + public async Task DeleteArticle(long id) { var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id); Console.WriteLine(entity); @@ -41,7 +43,7 @@ public class DbManagerArticle : IArticleService return entity.ToModel(); } - public async Task UpdateArticle(long id, Article? a) + public async Task UpdateArticle(long id, ArticleEntity? a) { var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id); if (entity == null) return false; @@ -54,15 +56,16 @@ public class DbManagerArticle : IArticleService return true; } - public Task GetArticleById(int id) + public Task GetArticleById(int id) { var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id); return Task.FromResult(entity.ToModel()); } - public async Task> GetAllArticles() + public async Task> GetAllArticles() { Console.WriteLine("GetAllArticles"); return await Task.FromResult(_context.ArticleSet.Select(a => a.ToModel()).AsEnumerable()); + } } \ No newline at end of file diff --git a/Verax_API_EF/API_DbDataManager/DbManagerFormulaire.cs b/Verax_API_EF/API_DbDataManager/DbManagerFormulaire.cs index 52ca273..b459078 100644 --- a/Verax_API_EF/API_DbDataManager/DbManagerFormulaire.cs +++ b/Verax_API_EF/API_DbDataManager/DbManagerFormulaire.cs @@ -5,8 +5,16 @@ using Model; namespace DbDataManager; -public class DbManagerFormulaire(LibraryContext _context) : IFormulaireService +public class DbManagerFormulaire : IFormulaireService { + + private readonly LibraryContext _context; + + public DbManagerFormulaire(LibraryContext context) + { + _context = context; + } + public async Task> GetAllForm() { return await Task.FromResult(_context.FormSet.Select(f => f.ToModel()).AsEnumerable()); diff --git a/Verax_API_EF/API_DbDataManager/DbManagerUser.cs b/Verax_API_EF/API_DbDataManager/DbManagerUser.cs index b35bde6..e7ce2c4 100644 --- a/Verax_API_EF/API_DbDataManager/DbManagerUser.cs +++ b/Verax_API_EF/API_DbDataManager/DbManagerUser.cs @@ -5,8 +5,16 @@ using Model; namespace DbDataManager; -public class DbManagerUser(LibraryContext _context): IUserService +public class DbManagerUser: IUserService { + + private readonly LibraryContext _context; + + public DbManagerUser(LibraryContext context) + { + _context = context; + } + public async Task Create(User user) { var entity = new UserEntity() diff --git a/Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db b/Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db deleted file mode 100644 index becbb82..0000000 Binary files a/Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db and /dev/null differ diff --git a/Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db-shm b/Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db-shm deleted file mode 100644 index fe9ac28..0000000 Binary files a/Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db-shm and /dev/null differ diff --git a/Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db-wal b/Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db-wal deleted file mode 100644 index e69de29..0000000 diff --git a/Verax_API_EF/API_DbDataManager/Extensions.cs b/Verax_API_EF/API_DbDataManager/Extensions.cs index 2e9335e..6431bec 100644 --- a/Verax_API_EF/API_DbDataManager/Extensions.cs +++ b/Verax_API_EF/API_DbDataManager/Extensions.cs @@ -1,19 +1,20 @@ using Entities; using Model; +using ArticleEntity = Model.ArticleEntity; namespace DbDataManager; public static class Extensions { - public static ArticleEntity ToEntity(this Article article) - => new ArticleEntity + public static Entities.ArticleEntity ToEntity(this ArticleEntity articleEntity) + => new Entities.ArticleEntity { - Id = article.Id, Author = article.Author, Description = article.Description, Title = article.Title, - DatePublished = article.DatePublished, LectureTime = article.LectureTime + Id = articleEntity.Id, Author = articleEntity.Author, Description = articleEntity.Description, Title = articleEntity.Title, + DatePublished = articleEntity.DatePublished, LectureTime = articleEntity.LectureTime }; - public static Article ToModel(this ArticleEntity article) - => new Article + public static ArticleEntity ToModel(this Entities.ArticleEntity article) + => new ArticleEntity { Id = article.Id, Author = article.Author, Description = article.Description, Title = article.Title, DatePublished = article.DatePublished, LectureTime = article.LectureTime diff --git a/Verax_API_EF/API_Mapping/ArticleMapper.cs b/Verax_API_EF/API_Mapping/ArticleMapper.cs index 9767dd8..dfaf91c 100644 --- a/Verax_API_EF/API_Mapping/ArticleMapper.cs +++ b/Verax_API_EF/API_Mapping/ArticleMapper.cs @@ -5,7 +5,7 @@ namespace API_Mapping; public static class ArticleMapper { - public static ArticleDTO ToDTO(this Article a) => new() + public static ArticleDTO ToDTO(this ArticleEntity a) => new() { Id = a.Id, Title = a.Title, @@ -15,7 +15,7 @@ public static class ArticleMapper Author = a.Author }; - public static Article ToModel(this ArticleDTO a) => new() + public static ArticleEntity ToModel(this ArticleDTO a) => new() { Id = a.Id, Title = a.Title, diff --git a/Verax_API_EF/API_Services/IArticleService.cs b/Verax_API_EF/API_Services/IArticleService.cs index 2bbeac2..3ee9e0f 100644 --- a/Verax_API_EF/API_Services/IArticleService.cs +++ b/Verax_API_EF/API_Services/IArticleService.cs @@ -4,16 +4,16 @@ namespace API_Services { public interface IArticleService { - Task CreateArticle(long id, string title, string description, string author, string date, + Task CreateArticle(long id, string title, string description, string author, string date, int lectureTime); - Task DeleteArticle(long id); + Task DeleteArticle(long id); - Task UpdateArticle(long id, Article? a); + Task UpdateArticle(long id, ArticleEntity? a); - Task GetArticleById(int id); + Task GetArticleById(int id); - Task> GetAllArticles(); + Task> GetAllArticles(); } } diff --git a/Verax_API_EF/DbContextLib/LibraryContext.cs b/Verax_API_EF/DbContextLib/LibraryContext.cs index 146b845..ef1fc90 100644 --- a/Verax_API_EF/DbContextLib/LibraryContext.cs +++ b/Verax_API_EF/DbContextLib/LibraryContext.cs @@ -35,5 +35,83 @@ public class LibraryContext : DbContext .HasMany(a => a.Users) .WithMany(a => a.Articles) .UsingEntity(); + + modelBuilder.Entity().HasData( + new ArticleEntity + { + Id = 1, + Title = "Breaking News Elisabeth 2 Died", + Description = "The queen of England died today at the age of 95", + DatePublished = "2022-02-06", + LectureTime = 2, + Author = "Tom Smith" + + }, + new ArticleEntity + { + Id = 2, + Title = "The new iPhone 15", + Description = "The new iPhone 15 is out and it's the best phone ever", + DatePublished = "2022-02-06", + LectureTime = 3, + Author = "Tom Smith" + + }, + new ArticleEntity + { + Id = 3, + Title = "M&M's new recipe", + Description = "M&M's new recipe is out and it's the best chocolate ever", + DatePublished = "2022-02-06", + LectureTime = 1, + Author = "M&M's Red" + } + ); + + modelBuilder.Entity().HasData( + new UserEntity + { + Id = 1, Nom = "Fages", Prenom = "Tony", Pseudo = "TonyF", Mail = "tony@gmail.com", Mdp = "1234", Role = "Admin" + }, + new UserEntity + { + Id = 2, Nom = "Smith", Prenom = "Tom", Pseudo = "TomS", Mail = "tom@mail.com", Mdp = "1234", + Role = "User" + }, + new UserEntity + { + Id = 3, Nom = "M&M's", Prenom = "Red", Pseudo = "RedM", Mail = "M&M#mail.com", Mdp = "1234", Role = "Modérator" + } + ); + + modelBuilder.Entity().HasData( + new ArticleUserEntity + { + ArticleEntityId = 1, + UserEntityId = 1 + }, + new ArticleUserEntity + { + ArticleEntityId = 2, + UserEntityId = 2 + }, + new ArticleUserEntity + { + ArticleEntityId = 3, + UserEntityId = 3 + }, + new ArticleUserEntity + { + ArticleEntityId = 3, + UserEntityId = 1 + }, + new ArticleUserEntity + { + ArticleEntityId = 2, + UserEntityId = 3 + } + ); + + } } \ No newline at end of file diff --git a/Verax_API_EF/Model/Article.cs b/Verax_API_EF/Model/ArticleEntity.cs similarity index 92% rename from Verax_API_EF/Model/Article.cs rename to Verax_API_EF/Model/ArticleEntity.cs index 497b56c..5bedd63 100644 --- a/Verax_API_EF/Model/Article.cs +++ b/Verax_API_EF/Model/ArticleEntity.cs @@ -1,6 +1,6 @@ namespace Model; -public class Article +public class ArticleEntity { public long Id { get; set; } public string Title { get; set; } = string.Empty; diff --git a/Verax_API_EF/StubbedContextLib/StubTest.cs b/Verax_API_EF/StubbedContextLib/StubTest.cs new file mode 100644 index 0000000..9648580 --- /dev/null +++ b/Verax_API_EF/StubbedContextLib/StubTest.cs @@ -0,0 +1,27 @@ +using Entities; +using Model; +using ArticleEntity = Model.ArticleEntity; + +namespace StubbedContextLib; + +public class StubTest +{ + private List _article; + + public List StubArticle() + { + _article = new List + { + new ArticleEntity + { + Id = 1, + Title = "Test", + Description = "Test", + Author = "Test", + DatePublished = "Test", + LectureTime = 1 + } + }; + return _article; + } +} \ No newline at end of file diff --git a/Verax_API_EF/StubbedContextLib/StubbedContextLib.csproj b/Verax_API_EF/StubbedContextLib/StubbedContextLib.csproj index c39101a..6c03da9 100644 --- a/Verax_API_EF/StubbedContextLib/StubbedContextLib.csproj +++ b/Verax_API_EF/StubbedContextLib/StubbedContextLib.csproj @@ -8,6 +8,7 @@ +