From 0ccaeaeda0a349ab3e6dd10e7181a886c1887b6f Mon Sep 17 00:00:00 2001 From: tonyfages Date: Tue, 12 Mar 2024 21:11:02 +0100 Subject: [PATCH] try to implement unit of _logger; - public ArticleController(IArticleService articleService, ILogger logger) + public ArticleController(IDataManager dataManager, ILogger logger) { - this._articleService = articleService; + this._dataManager = dataManager; this._logger = logger; } @@ -26,7 +27,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetAllArticles), index, count, orderCriterium); try { - var result = (await _articleService.GetAllArticles(index, count, orderCriterium)).Select(a => a.ToDTO()); + var result = (await _dataManager.ArticleService.GetAllArticles(index, count, orderCriterium)).Select(a => a.ToDTO()); if (result == null) { return NotFound(); @@ -46,7 +47,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetArticleById), id); try { - var result = (await _articleService.GetArticleById(id)).ToDTO(); + var result = (await _dataManager.ArticleService.GetArticleById(id)).ToDTO(); if (result == null) { return NotFound($"Article ID {id} not found"); @@ -68,7 +69,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(CreateArticle), article); try { - var result = (await _articleService.CreateArticle(article)).ToDTO(); + var result = (await _dataManager.ArticleService.CreateArticle(article)).ToDTO(); if (result == null) { return BadRequest($"Article not created"); @@ -88,7 +89,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(DeleteArticle), id); try { - var result = await _articleService.DeleteArticle(id); + var result = await _dataManager.ArticleService.DeleteArticle(id); if (result == null) { return NotFound($"Article ID {id} not found"); @@ -108,7 +109,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(UpdateArticle), id, a); try { - var result = await _articleService.UpdateArticle(id, a); + var result = await _dataManager.ArticleService.UpdateArticle(id, a); if (result == false) { return NotFound($"Article ID {id} not found"); diff --git a/Verax_API_EF/Verax_API_EF/API/Controllers/FormulaireController.cs b/Verax_API_EF/Verax_API_EF/API/Controllers/FormulaireController.cs index 1e793a9..6b46b64 100644 --- a/Verax_API_EF/Verax_API_EF/API/Controllers/FormulaireController.cs +++ b/Verax_API_EF/Verax_API_EF/API/Controllers/FormulaireController.cs @@ -12,12 +12,13 @@ namespace API.Controllers [ApiController] public class FormulaireController : ControllerBase { - private readonly IFormulaireService _form; + //private readonly IFormulaireService _form; + private readonly IDataManager _dataManager; private readonly ILogger _logger; - public FormulaireController(IFormulaireService iform, ILogger logger) + public FormulaireController(IDataManager dataManager, ILogger logger) { - this._form = iform; + this._dataManager = dataManager; this._logger = logger; } @@ -27,7 +28,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetAllForm), index, count, orderCriteria); try { - var result = (await _form.GetAllForm(index, count, orderCriteria)).Select(f => f.ToDTO()); + var result = (await _dataManager.FormulaireService.GetAllForm(index, count, orderCriteria)).Select(f => f.ToDTO()); if (result == null) { return NotFound($"No form found"); @@ -48,7 +49,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetById), id); try { - var result = (await _form.GetById(id)).ToDTO(); + var result = (await _dataManager.FormulaireService.GetById(id)).ToDTO(); if (result == null) { return NotFound($"form ID {id} not found"); @@ -70,7 +71,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(CreateForm), formulaire); try { - var result = (await _form.CreateForm(formulaire)).ToDTO(); + var result = (await _dataManager.FormulaireService.CreateForm(formulaire)).ToDTO(); if (result == null) { return BadRequest($"Form Id {formulaire.Id} already exists"); @@ -90,7 +91,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(DeleteForm), id); try { - var result = await _form.DeleteForm(id); + var result = await _dataManager.FormulaireService.DeleteForm(id); if (result == false) { return NotFound($"Form Id {id} not found"); @@ -111,7 +112,7 @@ namespace API.Controllers try { - var result = await _form.UpdateForm(id, formulaire); + var result = await _dataManager.FormulaireService.UpdateForm(id, formulaire); if (result == false) { return NotFound($"form Id {id} not found"); diff --git a/Verax_API_EF/Verax_API_EF/API/Controllers/UserController.cs b/Verax_API_EF/Verax_API_EF/API/Controllers/UserController.cs index 547c1af..adb77db 100644 --- a/Verax_API_EF/Verax_API_EF/API/Controllers/UserController.cs +++ b/Verax_API_EF/Verax_API_EF/API/Controllers/UserController.cs @@ -12,12 +12,13 @@ namespace API.Controllers [ApiController] public class UserController : ControllerBase { - private readonly IUserService _us; + //private readonly IUserService _us; + private readonly IDataManager _dataManager; private readonly ILogger _logger; - public UserController(IUserService us, ILogger logger) + public UserController(IDataManager dataManager, ILogger logger) { - this._us = us; this._logger = logger; + this._dataManager = dataManager; } [HttpGet("/users")] @@ -26,7 +27,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetAll), index, count, orderCriteria); try { - var result = (await _us.GetAll(index, count, orderCriteria)).Select(u => u.ToDTO()); + var result = (await _dataManager.UserService.GetAll(index, count, orderCriteria)).Select(u => u.ToDTO()); if (result == null) { return NotFound($"No user found with the given parameters"); @@ -46,7 +47,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetByPseudo), pseudo); try { - var result = (await _us.GetByPseudo(pseudo)).ToDTO(); + var result = (await _dataManager.UserService.GetByPseudo(pseudo)).ToDTO(); if (result == null) { return NotFound($"Psuedo {pseudo} not found"); @@ -67,7 +68,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(Create), user); try { - var result = await _us.Create(user); + var result = await _dataManager.UserService.Create(user); if (result == false) { return BadRequest($"User {user.Pseudo} already exists"); @@ -89,7 +90,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(Update), user, pseudo); try { - var result = await _us.Update(user,pseudo); + var result = await _dataManager.UserService.Update(user,pseudo); if (result == false) { return NotFound(); @@ -110,7 +111,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(Delete), pseudo); try { - var result = await _us.Delete(pseudo); + var result = await _dataManager.UserService.Delete(pseudo); if (result == false) { return NotFound(); @@ -130,7 +131,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetAllArticleUsers), ""); try { - var result = (await _us.GetAllArticleUsers()).Select(u => u.ToDTO()); + var result = (await _dataManager.UserService.GetAllArticleUsers()).Select(u => u.ToDTO()); if (result == null) { return NoContent(); @@ -150,7 +151,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetArticleUser), pseudo); try { - var result = (await _us.GetArticleUser(pseudo)).Select(a => a.ToDTO()); + var result = (await _dataManager.UserService.GetArticleUser(pseudo)).Select(a => a.ToDTO()); if (result == null) { return NoContent(); @@ -171,7 +172,7 @@ namespace API.Controllers try { Console.WriteLine(articleUser); - var result = await _us.CreateArticleUser(articleUser); + var result = await _dataManager.UserService.CreateArticleUser(articleUser); if (result == null) { return BadRequest($"ArticleUser {articleUser.UserEntityPseudo} already exists"); @@ -191,7 +192,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(DeleteArticleUser), pseudo); try { - var result = await _us.DeleteArticleUser(pseudo); + var result = await _dataManager.UserService.DeleteArticleUser(pseudo); if (!result) { return BadRequest($"ArticleUser {pseudo} does not exist"); @@ -211,7 +212,7 @@ namespace API.Controllers _logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(UpdateArticleUser), articleUser); try { - var result = await _us.UpdateArticleUser(articleUser); + var result = await _dataManager.UserService.UpdateArticleUser(articleUser); if (!result) { return BadRequest($"ArticleUser {articleUser.UserEntityPseudo} does not exist"); diff --git a/Verax_API_EF/Verax_API_EF/API/Program.cs b/Verax_API_EF/Verax_API_EF/API/Program.cs index 03613b2..06fffd8 100644 --- a/Verax_API_EF/Verax_API_EF/API/Program.cs +++ b/Verax_API_EF/Verax_API_EF/API/Program.cs @@ -2,6 +2,7 @@ using API_Services; using DbContextLib; using DbDataManager; using Microsoft.EntityFrameworkCore; +using Model; using StubbedContextLib; var builder = WebApplication.CreateBuilder(args); @@ -20,9 +21,9 @@ builder.Services.AddDbContext(options => options.UseSqlite("Data Source=Entity_FrameWork.Article.db"); }); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); + + var app = builder.Build(); diff --git a/Verax_API_EF/Verax_API_EF/API_Services/IDataManager.cs b/Verax_API_EF/Verax_API_EF/API_Services/IDataManager.cs index 846f924..d5abbc8 100644 --- a/Verax_API_EF/Verax_API_EF/API_Services/IDataManager.cs +++ b/Verax_API_EF/Verax_API_EF/API_Services/IDataManager.cs @@ -3,8 +3,11 @@ using API_Services; public interface IDataManager { + + IArticleService ArticleService { get; } IUserService UserService { get; } IFormulaireService FormulaireService { get; } -} \ No newline at end of file +} +