From 4fe9d582759b95c8af1069dd821898f44c2ba966 Mon Sep 17 00:00:00 2001 From: clchieu Date: Fri, 15 Mar 2024 17:02:27 +0100 Subject: [PATCH] Modification du service de Paragraph --- .../API/Controllers/LessonsController.cs | 2 +- .../API/Controllers/ParagraphsController.cs | 2 +- .../API/Controllers/SuccessesController.cs | 2 +- API_SQLuedo/API/Program.cs | 6 +- .../API/Service/ParagraphDataServiceAPI.cs | 22 ++-- .../Service/ParagraphDataService.cs | 58 ++++++--- API_SQLuedo/Shared/ParagraphDataService.cs | 116 ------------------ API_SQLuedo/TestConsoleAPI/Program.cs | 4 +- 8 files changed, 59 insertions(+), 153 deletions(-) delete mode 100644 API_SQLuedo/Shared/ParagraphDataService.cs diff --git a/API_SQLuedo/API/Controllers/LessonsController.cs b/API_SQLuedo/API/Controllers/LessonsController.cs index eaaec1b..ba99163 100644 --- a/API_SQLuedo/API/Controllers/LessonsController.cs +++ b/API_SQLuedo/API/Controllers/LessonsController.cs @@ -13,7 +13,7 @@ namespace API.Controllers [ApiController] public class LessonsController : Controller { - private ILessonService _lessonDataService; + private readonly ILessonService _lessonDataService; private readonly ILogger _logger; diff --git a/API_SQLuedo/API/Controllers/ParagraphsController.cs b/API_SQLuedo/API/Controllers/ParagraphsController.cs index 9196e21..9be8fd9 100644 --- a/API_SQLuedo/API/Controllers/ParagraphsController.cs +++ b/API_SQLuedo/API/Controllers/ParagraphsController.cs @@ -13,7 +13,7 @@ namespace API.Controllers [ApiController] public class ParagraphsController : Controller { - private IParagraphService _paragraphDataService; + private readonly IParagraphService _paragraphDataService; private readonly ILogger _logger; diff --git a/API_SQLuedo/API/Controllers/SuccessesController.cs b/API_SQLuedo/API/Controllers/SuccessesController.cs index 09df742..a399979 100644 --- a/API_SQLuedo/API/Controllers/SuccessesController.cs +++ b/API_SQLuedo/API/Controllers/SuccessesController.cs @@ -13,7 +13,7 @@ namespace API.Controllers [ApiController] public class SuccessesController : Controller { - private ISuccessService _successDataService; + private readonly ISuccessService _successDataService; private readonly ILogger _logger; diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index 060b520..98766d0 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -6,9 +6,7 @@ using DbDataManager.Service; using Dto; using Entities; using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using Shared; @@ -27,8 +25,8 @@ builder.Services.AddScoped, Shared.UserDataService>(); builder.Services.AddScoped, InquiryDataService>(); builder.Services.AddScoped, InquiryDataServiceApi>(); -builder.Services.AddScoped, DbDataManager.Service.ParagraphDataService>(); -builder.Services.AddScoped, Shared.ParagraphDataService>(); +builder.Services.AddScoped, ParagraphDataService>(); +builder.Services.AddScoped, ParagraphDataServiceApi>(); builder.Services.AddScoped, DbDataManager.Service.SuccessDataService>(); builder.Services.AddScoped, Shared.SuccessDataService>(); diff --git a/API_SQLuedo/API/Service/ParagraphDataServiceAPI.cs b/API_SQLuedo/API/Service/ParagraphDataServiceAPI.cs index c29f89d..af09a6b 100644 --- a/API_SQLuedo/API/Service/ParagraphDataServiceAPI.cs +++ b/API_SQLuedo/API/Service/ParagraphDataServiceAPI.cs @@ -1,26 +1,28 @@ -using Entities; +using Dto; +using Entities; using Model.OrderCriteria; using Shared; +using Shared.Mapper; namespace API.Service; -public class ParagraphDataServiceApi(IParagraphService paragraphService) : IParagraphService +public class ParagraphDataServiceApi(IParagraphService paragraphService) : IParagraphService { - public IEnumerable GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria) + public IEnumerable GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria) { var paragraphsEntities = paragraphService.GetParagraphs(page, number, orderCriteria); - return paragraphsEntities.Select(e => e).ToList(); + return paragraphsEntities.Select(e => e.FromEntityToDTO()).ToList(); } - public ParagraphEntity GetParagraphById(int id) => paragraphService.GetParagraphById(id); + public ParagraphDTO GetParagraphById(int id) => paragraphService.GetParagraphById(id).FromEntityToDTO(); - public ParagraphEntity GetParagraphByTitle(string title) => paragraphService.GetParagraphByTitle(title); + public ParagraphDTO GetParagraphByTitle(string title) => paragraphService.GetParagraphByTitle(title).FromEntityToDTO(); public bool DeleteParagraph(int id) => paragraphService.DeleteParagraph(id); - public ParagraphEntity UpdateParagraph(int id, ParagraphEntity paragraph) => - paragraphService.UpdateParagraph(id, paragraph); + public ParagraphDTO UpdateParagraph(int id, ParagraphDTO paragraph) => + paragraphService.UpdateParagraph(id, paragraph.FromDTOToEntity()).FromEntityToDTO(); - public ParagraphEntity CreateParagraph(string title, string content, string info, string query, string comment, int lessonId) => - paragraphService.CreateParagraph(title, content, info, query, comment, lessonId); + public ParagraphDTO CreateParagraph(string title, string content, string info, string query, string comment, int lessonId) => + paragraphService.CreateParagraph(title, content, info, query, comment, lessonId).FromEntityToDTO(); } \ No newline at end of file diff --git a/API_SQLuedo/DbDataManager/Service/ParagraphDataService.cs b/API_SQLuedo/DbDataManager/Service/ParagraphDataService.cs index d561261..fa92a29 100644 --- a/API_SQLuedo/DbDataManager/Service/ParagraphDataService.cs +++ b/API_SQLuedo/DbDataManager/Service/ParagraphDataService.cs @@ -16,6 +16,36 @@ public class ParagraphDataService : IParagraphService context.Database.EnsureCreated(); } + public IEnumerable GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria) + { + IQueryable query = DbContext.Paragraphs.Skip((page - 1) * number).Take(number); + switch (orderCriteria) + { + case ParagraphOrderCriteria.None: + break; + case ParagraphOrderCriteria.ByTitle: + query = query.OrderBy(s => s.Title); + break; + case ParagraphOrderCriteria.ByContent: + query = query.OrderBy(s => s.Content); + break; + case ParagraphOrderCriteria.ByInfo: + query = query.OrderBy(s => s.Info); + break; + case ParagraphOrderCriteria.ByQuery: + query = query.OrderBy(s => s.Query); + break; + case ParagraphOrderCriteria.ByComment: + query = query.OrderBy(s => s.Comment); + break; + default: + break; + } + + var paragraphs = query.ToList(); + return paragraphs.Select(s => s); + } + public ParagraphEntity GetParagraphById(int id) { var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Id == id); @@ -38,15 +68,9 @@ public class ParagraphDataService : IParagraphService return paragraphEntity; } - public IEnumerable GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria) - { - return DbContext.Paragraphs.Skip((page - 1) * number).Take(number).ToList() - .Select(u => u); - } - public bool DeleteParagraph(int id) { - var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Id == id); + var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id); if (paragraphEntity == null) { return false; @@ -57,27 +81,25 @@ public class ParagraphDataService : IParagraphService return true; } - public ParagraphEntity UpdateParagraph(int id, ParagraphEntity paragraph) + public ParagraphEntity UpdateParagraph(int id, ParagraphEntity paragraphDTO) { - var updatingParagraph = DbContext.Paragraphs.FirstOrDefault(u => u.Id == id); + var updatingParagraph = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id); if (updatingParagraph == null) { throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id)); } - updatingParagraph.Title = paragraph.Title; - updatingParagraph.Content = paragraph.Content; - updatingParagraph.Info = paragraph.Info; - updatingParagraph.Query = paragraph.Query; - updatingParagraph.Comment = paragraph.Comment; - updatingParagraph.LessonId = paragraph.LessonId; - // Permet d'indiquer en Db que l'entité a été modifiée. - DbContext.Entry(updatingParagraph).State = EntityState.Modified; + updatingParagraph.Title = paragraphDTO.Title; + updatingParagraph.Content = paragraphDTO.Content; + updatingParagraph.Info = paragraphDTO.Info; + updatingParagraph.Query = paragraphDTO.Query; + updatingParagraph.Comment = paragraphDTO.Comment; DbContext.SaveChangesAsync(); return updatingParagraph; } - public ParagraphEntity CreateParagraph(string title, string content, string info, string query, string comment, int lessonId) + public ParagraphEntity CreateParagraph(string title, string content, string info, string query, string comment, + int lessonId) { var newParagraphEntity = new ParagraphEntity() { diff --git a/API_SQLuedo/Shared/ParagraphDataService.cs b/API_SQLuedo/Shared/ParagraphDataService.cs deleted file mode 100644 index 7954f78..0000000 --- a/API_SQLuedo/Shared/ParagraphDataService.cs +++ /dev/null @@ -1,116 +0,0 @@ -using Entities; -using Microsoft.EntityFrameworkCore; -using Dto; -using DbContextLib; -using Model.OrderCriteria; -using Shared; -using Shared.Mapper; - -namespace Shared; - -public class ParagraphDataService : IParagraphDataService -{ - private UserDbContext DbContext { get; set; } - public ParagraphDataService(UserDbContext context) - { - DbContext = context; - context.Database.EnsureCreated(); - } - - public IEnumerable GetParagraphs(int page, int number, ParagraphOrderCriteria orderCriteria) - { - IQueryable query = DbContext.Paragraphs.Skip((page - 1) * number).Take(number); - switch (orderCriteria) - { - case ParagraphOrderCriteria.None: - break; - case ParagraphOrderCriteria.ByTitle: - query = query.OrderBy(s => s.Title); - break; - case ParagraphOrderCriteria.ByContent: - query = query.OrderBy(s => s.Content); - break; - case ParagraphOrderCriteria.ByInfo: - query = query.OrderBy(s => s.Info); - break; - case ParagraphOrderCriteria.ByQuery: - query = query.OrderBy(s => s.Query); - break; - case ParagraphOrderCriteria.ByComment: - query = query.OrderBy(s => s.Comment); - break; - default: - break; - } - var paragraphs = query.ToList(); - return paragraphs.Select(s => s.FromEntityToDTO()); - } - - public ParagraphDTO GetParagraphById(int id) - { - var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Id == id); - if (paragraphEntity == null) - { - throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id)); - } - - return paragraphEntity.FromEntityToDTO(); - } - - public ParagraphDTO GetParagraphByTitle(string title) - { - var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(u => u.Title == title); - if (paragraphEntity == null) - { - throw new ArgumentException("Impossible de trouver le paragraphe", nameof(title)); - } - - return paragraphEntity.FromEntityToDTO(); - } - - public bool DeleteParagraph(int id) - { - var paragraphEntity = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id); - if (paragraphEntity == null) - { - return false; - } - - DbContext.Paragraphs.Remove(paragraphEntity); - DbContext.SaveChangesAsync(); - return true; - } - - public ParagraphDTO UpdateParagraph(int id, ParagraphDTO paragraphDTO) - { - var updatingParagraph = DbContext.Paragraphs.FirstOrDefault(p => p.Id == id); - if (updatingParagraph == null) - { - throw new ArgumentException("Impossible de trouver le paragraphe", nameof(id)); - } - - updatingParagraph.Title = paragraphDTO.Title; - updatingParagraph.Content = paragraphDTO.Content; - updatingParagraph.Info = paragraphDTO.Info; - updatingParagraph.Query = paragraphDTO.Query; - updatingParagraph.Comment = paragraphDTO.Comment; - DbContext.SaveChangesAsync(); - return updatingParagraph.FromEntityToDTO(); - } - - public ParagraphDTO CreateParagraph(string title, string content, string info, string query, string comment, int lessonId) - { - var newParagraphEntity = new ParagraphDTO() - { - Title = title, - Content = content, - Info = info, - Query = query, - Comment = comment, - LessonId = lessonId - }; - DbContext.Paragraphs.Add(newParagraphEntity.FromDTOToEntity()); - DbContext.SaveChangesAsync(); - return newParagraphEntity; - } -} \ No newline at end of file diff --git a/API_SQLuedo/TestConsoleAPI/Program.cs b/API_SQLuedo/TestConsoleAPI/Program.cs index 5c6486a..6921e72 100644 --- a/API_SQLuedo/TestConsoleAPI/Program.cs +++ b/API_SQLuedo/TestConsoleAPI/Program.cs @@ -10,8 +10,8 @@ using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Model.OrderCriteria; -using ParagraphDataService = Shared.ParagraphDataService; using Service_LessonDataService = DbDataManager.Service.LessonDataService; +using Service_ParagraphDataService = DbDataManager.Service.ParagraphDataService; using SuccessDataService = Shared.SuccessDataService; using UserDataService = Shared.UserDataService; @@ -33,7 +33,7 @@ using (var context = new UserDbContext(options)) var userController = new UsersController(userLogger, new UserDataService(context)); var inquiryController = new InquiriesController(new InquiryDataServiceApi(new InquiryDataService(context)), inquiryLogger); - var paragraphController = new ParagraphsController(new ParagraphDataService(context), paragraphLogger); + var paragraphController = new ParagraphsController(new ParagraphDataServiceApi(new Service_ParagraphDataService(context)), paragraphLogger); var lessonController = new LessonsController(new LessonDataServiceApi(new Service_LessonDataService(context)), lessonLogger); var successController = new SuccessesController(new SuccessDataService(context), successLogger);