From 3e6bdbb85a828d89dc00d27b168278c9cd04e716 Mon Sep 17 00:00:00 2001 From: tonyfages Date: Fri, 8 Mar 2024 17:33:18 +0100 Subject: [PATCH] Ca marche pas --- Verax_API_EF/API/API.csproj | 4 + .../API/Controllers/ArticleController.cs | 8 +- Verax_API_EF/API/Program.cs | 11 +-- .../API_DbDataManager/DbManagerArticle.cs | 17 ++-- .../API_DbDataManager/DbManagerFormulaire.cs | 10 ++- .../API_DbDataManager/DbManagerUser.cs | 10 ++- .../Entity_FrameWork.Article.db | Bin 45056 -> 0 bytes .../Entity_FrameWork.Article.db-shm | Bin 32768 -> 0 bytes .../Entity_FrameWork.Article.db-wal | 0 Verax_API_EF/API_DbDataManager/Extensions.cs | 13 +-- Verax_API_EF/API_Mapping/ArticleMapper.cs | 4 +- Verax_API_EF/API_Services/IArticleService.cs | 10 +-- Verax_API_EF/DbContextLib/LibraryContext.cs | 78 ++++++++++++++++++ .../Model/{Article.cs => ArticleEntity.cs} | 2 +- Verax_API_EF/StubbedContextLib/StubTest.cs | 27 ++++++ .../StubbedContextLib.csproj | 1 + 16 files changed, 163 insertions(+), 32 deletions(-) delete mode 100644 Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db delete mode 100644 Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db-shm delete mode 100644 Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db-wal rename Verax_API_EF/Model/{Article.cs => ArticleEntity.cs} (92%) create mode 100644 Verax_API_EF/StubbedContextLib/StubTest.cs 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 becbb82ace3aec72d172dff2174e0573bf444e47..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 45056 zcmeI)-*4MQ00(gYNa`k~^CT23)Q4R?F&eFFn{=Z?2&q}?jELq3Ns6`y6s5_nv6k4K z?NIu_L&pSv0sq1f5BwFpBk{o79*|%{fDj;%K!OL(j`P#b)JaTQq_36KKkj_z``n!! z*FP%n?CB0AcTB5OcgQApo)ZLan-GrU1omBE-~J%6iAaBjjro!3X_ErCcIT%>`EM>R zUE|JuBY&~@aIyMAI`(Mc*ZFPfAzO$60uX=z1RyXg0%lxN-dN&K6S~o)ALMS;vh46# zp-yezrj}+nx^t3i`pXt}%37wXkz8R{yG7I~kyTPG5Y=BrB`fM^N^0^{iAhRvoj-lE zpJT>y^hTQwqmAVq2#p7}A`oRPf!_86;sm$sqnGC;Woe23IOfz3+SD(BKZ^OeRx>yD zw9(?l2^-X0Q)Mhw?WR^Hg<_Qy_V@P4PO(s_mNRTQQA=<4WmQrt=kl5IZSuBun`HK@ z#aw|Up4ST1)m~!N7VS`#RJB_yAwMxcXt(arQum;(H|maV8sU-lbmMM#NXe$%rWqbK z^badcFaOaJWd|klxF_3;O0vbWmb+Q-iaFT|NLkBjWv#HIRfyL>*y?VLu&k4A``Oj@ zG}h8~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ameWork.Article.db-shm b/Verax_API_EF/API_DbDataManager/Entity_FrameWork.Article.db-shm deleted file mode 100644 index fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeIuAr62r3 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 @@ +