From 6e11809f60a8531631b6065f3d9961a3ef55e808 Mon Sep 17 00:00:00 2001 From: clchieu Date: Tue, 27 Feb 2024 23:27:15 +0100 Subject: [PATCH] =?UTF-8?q?:construction:=20Les=20injections=20de=20d?= =?UTF-8?q?=C3=A9pendance=20ne=20sont=20surement=20pas=20bonnes,=20mais=20?= =?UTF-8?q?l'archi=20est=20faite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../API/Controllers/InquiriesController.cs | 33 ++++++++++++------- API_SQLuedo/API/Controllers/UserController.cs | 8 +++-- API_SQLuedo/API/Program.cs | 6 ++-- .../API/Service/InquiryDataServiceAPI.cs | 13 ++++++++ API_SQLuedo/API/Service/UserDataServiceAPI.cs | 20 +++++++++++ .../Service/InquiryDataService.cs | 11 ++++--- API_SQLuedo/TestConsoleAPI/Program.cs | 3 +- 7 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 API_SQLuedo/API/Service/InquiryDataServiceAPI.cs create mode 100644 API_SQLuedo/API/Service/UserDataServiceAPI.cs diff --git a/API_SQLuedo/API/Controllers/InquiriesController.cs b/API_SQLuedo/API/Controllers/InquiriesController.cs index 31b9626..9e4eacd 100644 --- a/API_SQLuedo/API/Controllers/InquiriesController.cs +++ b/API_SQLuedo/API/Controllers/InquiriesController.cs @@ -8,23 +8,32 @@ namespace API.Controllers [Route("api/[controller]")] [Authorize] [ApiController] - public class InquiriesController(ILogger logger, IInquiryService inquiryService) - : ControllerBase, IInquiryService + public class InquiriesController() + : ControllerBase { + private readonly IInquiryService _inquiryService; + private readonly ILogger _logger; + + public InquiriesController(IInquiryService inquiryService, ILogger logger) : this() + { + _inquiryService = inquiryService; + _logger = logger; + } + [HttpGet("inquiries/{page}/{number}")] [ProducesResponseType(typeof(InquiryDTO), 200)] [ProducesResponseType(typeof(string), 204)] public IActionResult GetInquiries(int page, int number) { - var nbInquiry = inquiryService.GetInquiries(page, number).Count(); + var nbInquiry = _inquiryService.GetInquiries(page, number).Count(); if (nbInquiry == 0) { - logger.LogError("[ERREUR] Aucune enquête trouvé."); + _logger.LogError("[ERREUR] Aucune enquête trouvé."); return StatusCode(204); } - logger.LogInformation("[INFORMATION] {nb} Enquête(s) trouvée(s)", nbInquiry); - return Ok(inquiryService.GetInquiries(page, number)); + _logger.LogInformation("[INFORMATION] {nb} Enquête(s) trouvée(s)", nbInquiry); + return Ok(_inquiryService.GetInquiries(page, number)); } [HttpGet("inquiry/id/{id}")] @@ -34,12 +43,12 @@ namespace API.Controllers { try { - logger.LogInformation("[INFORMATION] Enquête avec l'id {id} a été trouvé.", id); - return Ok(inquiryService.GetInquiryById(id)); + _logger.LogInformation("[INFORMATION] Enquête avec l'id {id} a été trouvé.", id); + return Ok(_inquiryService.GetInquiryById(id)); } catch (ArgumentException) { - logger.LogError("[ERREUR] Aucune enquête trouvée avec l'id {id}.", id); + _logger.LogError("[ERREUR] Aucune enquête trouvée avec l'id {id}.", id); return NotFound(); } } @@ -51,12 +60,12 @@ namespace API.Controllers { try { - logger.LogInformation("[INFORMATION] Enquête avec le titre {title} a été trouvé.", title); - return Ok(inquiryService.GetInquiryByTitle(title)); + _logger.LogInformation("[INFORMATION] Enquête avec le titre {title} a été trouvé.", title); + return Ok(_inquiryService.GetInquiryByTitle(title)); } catch (ArgumentException) { - logger.LogError("[ERREUR] Aucune enquête trouvée avec le titre {title}.", title); + _logger.LogError("[ERREUR] Aucune enquête trouvée avec le titre {title}.", title); return NotFound(); } } diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UserController.cs index a60b6e7..612d0ff 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UserController.cs @@ -1,6 +1,8 @@ -using Dto; +using API.Service; +using Dto; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using ModelToEntities.Service; using Shared; namespace API.Controllers @@ -9,7 +11,7 @@ namespace API.Controllers [Authorize] [ApiVersion("1.0")] [ApiController] - public class UserController(ILogger logger, IUserService userService) : ControllerBase, IUserService + public class UserController(ILogger logger, IUserService userService) : ControllerBase { [HttpGet("users/{page}/{number}")] [ProducesResponseType(typeof(UserDTO), 200)] @@ -101,7 +103,7 @@ namespace API.Controllers [ProducesResponseType(typeof(UserDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] - public IActionResult UpdateUser(int id, [FromBody] UserDTO userDTO) + public IActionResult UpdateUser(int id, [FromBody] UserDTO userDto) { if (id != userDto.Id) { diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index 9eeab75..2f26e29 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -1,11 +1,11 @@ using API; +using API.Service; using DbContextLib; using Dto; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; -using ModelToEntities.Business; using ModelToEntities.Service; using Shared; @@ -18,7 +18,9 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddScoped, UserDataService>(); -builder.Services.AddScoped, InquiryDataService>(); +builder.Services.AddScoped, UserDataServiceApi>(); +builder.Services.AddScoped, InquiryDataService>(); +builder.Services.AddScoped, InquiryDataServiceApi>(); builder.Services.AddDbContext(); builder.Services.AddDbContext(options => options.UseInMemoryDatabase("appDb")); builder.Services.AddIdentityApiEndpoints().AddEntityFrameworkStores(); diff --git a/API_SQLuedo/API/Service/InquiryDataServiceAPI.cs b/API_SQLuedo/API/Service/InquiryDataServiceAPI.cs new file mode 100644 index 0000000..90eb3e7 --- /dev/null +++ b/API_SQLuedo/API/Service/InquiryDataServiceAPI.cs @@ -0,0 +1,13 @@ +using Dto; +using Shared; + +namespace API.Service; + +public class InquiryDataServiceApi(IInquiryService inquiryService) : IInquiryService +{ + public IEnumerable GetInquiries(int page, int number) => inquiryService.GetInquiries(page, number); + + public InquiryDTO GetInquiryById(int id) => inquiryService.GetInquiryById(id); + + public InquiryDTO GetInquiryByTitle(string title) => inquiryService.GetInquiryByTitle(title); +} \ No newline at end of file diff --git a/API_SQLuedo/API/Service/UserDataServiceAPI.cs b/API_SQLuedo/API/Service/UserDataServiceAPI.cs new file mode 100644 index 0000000..66013dd --- /dev/null +++ b/API_SQLuedo/API/Service/UserDataServiceAPI.cs @@ -0,0 +1,20 @@ +using Dto; +using Shared; + +namespace API.Service; + +public class UserDataServiceApi(IUserService userService) : IUserService +{ + public IEnumerable GetUsers(int page, int number) => userService.GetUsers(page, number); + + public UserDTO GetUserById(int id) => userService.GetUserById(id); + + public UserDTO GetUserByUsername(string username) => userService.GetUserByUsername(username); + + public bool DeleteUser(int id) => userService.DeleteUser(id); + + public UserDTO UpdateUser(int id, UserDTO user) => userService.UpdateUser(id, user); + + public UserDTO CreateUser(string username, string password, string email, bool isAdmin) => + userService.CreateUser(username, password, email, isAdmin); +} \ No newline at end of file diff --git a/API_SQLuedo/ModelToEntities/Service/InquiryDataService.cs b/API_SQLuedo/ModelToEntities/Service/InquiryDataService.cs index 78c5a65..4b76513 100644 --- a/API_SQLuedo/ModelToEntities/Service/InquiryDataService.cs +++ b/API_SQLuedo/ModelToEntities/Service/InquiryDataService.cs @@ -1,21 +1,22 @@ -using ModelToEntities.Business; +using Dto; +using ModelToEntities.Business; using Shared; namespace ModelToEntities.Service; -public class InquiryDataService : IInquiryService +public class InquiryDataService : IInquiryService { - public IEnumerable GetInquiries(int page, int number) + public IEnumerable GetInquiries(int page, int number) { throw new NotImplementedException(); } - public Inquiry GetInquiryById(int id) + public InquiryDTO GetInquiryById(int id) { throw new NotImplementedException(); } - public Inquiry GetInquiryByTitle(string title) + public InquiryDTO GetInquiryByTitle(string title) { throw new NotImplementedException(); } diff --git a/API_SQLuedo/TestConsoleAPI/Program.cs b/API_SQLuedo/TestConsoleAPI/Program.cs index 10c1d99..50fd3a5 100644 --- a/API_SQLuedo/TestConsoleAPI/Program.cs +++ b/API_SQLuedo/TestConsoleAPI/Program.cs @@ -1,6 +1,7 @@ // See https://aka.ms/new-console-template for more information using API.Controllers; +using API.Service; using DbContextLib; using Dto; using Microsoft.AspNetCore.Mvc; @@ -20,7 +21,7 @@ ILogger logger = factory.CreateLogger(); using (var context = new UserDbContext(options)) { - var controller = new UserController(logger); + var controller = new UserController(logger, new UserDataServiceApi(new UserDataService(context))); void PrintUsers() {