From 08e08687da43fcc8a69637a0b37a5575b69603ed Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Fri, 23 Feb 2024 09:54:16 +0100 Subject: [PATCH 1/8] =?UTF-8?q?ajout=20d=C3=A9but=20de=20la=20g=C3=A9nicit?= =?UTF-8?q?=C3=A9=20mais=20il=20y=20a=20des=20erreurs,=20pas=20encore=20co?= =?UTF-8?q?rig=C3=A9es=20!!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/API/Controllers/UserController.cs | 24 +++--- API_SQLuedo/API/Program.cs | 2 +- API_SQLuedo/ModelToEntity/DbDataManager.cs | 9 ++- API_SQLuedo/ModelToEntity/Extension.cs | 19 ++++- API_SQLuedo/Services/IDataService.cs | 21 +----- API_SQLuedo/Services/IInquiryDataService.cs | 21 ++++++ API_SQLuedo/Services/ILessonDataService.cs | 20 +++++ API_SQLuedo/Services/Services.csproj | 1 + API_SQLuedo/Services/UserDataService.cs | 73 +++---------------- API_SQLuedo/Shared/IGenericService.cs | 7 ++ API_SQLuedo/Shared/IUserDataService.cs | 22 ++++++ API_SQLuedo/Shared/Shared.csproj | 9 +++ 12 files changed, 129 insertions(+), 99 deletions(-) create mode 100644 API_SQLuedo/Services/IInquiryDataService.cs create mode 100644 API_SQLuedo/Services/ILessonDataService.cs create mode 100644 API_SQLuedo/Shared/IGenericService.cs create mode 100644 API_SQLuedo/Shared/IUserDataService.cs create mode 100644 API_SQLuedo/Shared/Shared.csproj diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UserController.cs index 450c64a..3e17320 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UserController.cs @@ -13,27 +13,27 @@ namespace API.Controllers [ApiController] public class UserController : Controller { - private IDataService _userDataService; + private IDataService _dataService; private readonly ILogger _logger; - public UserController(IDataService userDataService, ILogger logger) + public UserController(IDataService dataService, ILogger logger) { - _userDataService = userDataService; + _dataService = dataService; _logger = logger; } [HttpGet("users/{page}/{number}")] public async Task GetUsers(int page, int number) { - var nbUser = (await _userDataService.GetUsers(page, number)).ToList().Count(); + var nbUser = (await _dataService.UserService.GetUsers(page, number)).ToList().Count(); if(nbUser == 0) { _logger.LogError("[ERREUR] Aucun utilisateur trouvé."); return StatusCode(204); } _logger.LogInformation("[INFORMATION] {nb} Utilisateur(s) trouvé(s)", nbUser); - return Ok(_userDataService.GetUsers(page, number)); + return Ok(_dataService.UserService.GetUsers(page, number)); } [HttpGet("user/id/{id}")] @@ -42,7 +42,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] Utilisateur avec l'id {id} a été trouvé.", id); - return Ok(_userDataService.GetUserById(id)); + return Ok(_dataService.UserService.GetUserById(id)); } catch (ArgumentException) { _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id); @@ -56,7 +56,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] Utilisateur avec l'username {username} a été trouvé.", username); - return Ok(_userDataService.GetUserByUsername(username)); + return Ok(_dataService.UserService.GetUserByUsername(username)); }catch (ArgumentException) { _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'username {username}.", username); @@ -68,11 +68,11 @@ namespace API.Controllers [HttpDelete] public async Task DeleteUser(int id) { - var success = await _userDataService.DeleteUser(id); + var success = await _dataService.UserService.DeleteUser(id); if(success) { _logger.LogInformation("[INFORMATION] L'utilisateur avec l'id {id} a été supprimé.", id); - return Ok(_userDataService.DeleteUser(id)); + return Ok(_dataService.UserService.DeleteUser(id)); } else { _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id); @@ -88,9 +88,9 @@ namespace API.Controllers { return BadRequest(); } - // return Ok(_userDataService.CreateUser(username, password, email, isAdmin)); + _logger.LogInformation("[INFORMATION] Un utilisateur a été créé : username - {username}, password - {password}, email - {email}, isAdmin - {isAdmin}", dto.Username, dto.Password, dto.Email, dto.IsAdmin); - return Created(nameof(GetUsers), _userDataService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin)); + return Created(nameof(GetUsers), _dataService.UserService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin)); } [HttpPut] @@ -109,7 +109,7 @@ namespace API.Controllers if(userDTO != null) { _logger.LogInformation("[INFORMATION] La mise à jour de l'utilisateur avec l'id {id} a été effectuée", id); - return Ok(_userDataService.UpdateUser(id, userDTO)); + return Ok(_dataService.UserService.UpdateUser(id, userDTO)); } _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id); return NotFound(); diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index eb3ee3c..579f60f 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -15,7 +15,7 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddDbContext(); builder.Services.AddDbContext(options => options.UseInMemoryDatabase("appDb")); diff --git a/API_SQLuedo/ModelToEntity/DbDataManager.cs b/API_SQLuedo/ModelToEntity/DbDataManager.cs index 7ae486b..9e87320 100644 --- a/API_SQLuedo/ModelToEntity/DbDataManager.cs +++ b/API_SQLuedo/ModelToEntity/DbDataManager.cs @@ -9,7 +9,7 @@ using Model.Mappers; namespace ModelToEntity { - public class DbDataManager : IDataServiceEF + public class DbDataManager : IUserDataService { public async Task GetUserById(int id) { @@ -93,7 +93,7 @@ namespace ModelToEntity IsAdmin = isAdmin }; context.Users.Add(newUserEntity.FromDTOToModel().FromModelToEntity()); - context.SaveChangesAsync(); + context.SaveChanges(); return await Task.FromResult(newUserEntity.FromDTOToModel()); } @@ -157,6 +157,11 @@ namespace ModelToEntity { throw new NotImplementedException(); } + + public Task AddItem(T? item) + { + throw new NotImplementedException(); + } } } diff --git a/API_SQLuedo/ModelToEntity/Extension.cs b/API_SQLuedo/ModelToEntity/Extension.cs index b039fe4..b63e770 100644 --- a/API_SQLuedo/ModelToEntity/Extension.cs +++ b/API_SQLuedo/ModelToEntity/Extension.cs @@ -7,10 +7,25 @@ using System.Threading.Tasks; using Entities; using Entities.SQLudeoDB; using Model.Business; +using System.Diagnostics; +using DbContextLib; +using Microsoft.EntityFrameworkCore; namespace ModelToEntity { - static class Extension + internal static class Extension { + internal static async Task AddItemAsync(this DbContext context, T? item) where T : class + { + if (item == null || await context.Set().ContainsAsync(item)) + { + return default(T); + } + + context.Set().Add(item); + await context.SaveChangesAsync(); + + return item; + } } -} +} \ No newline at end of file diff --git a/API_SQLuedo/Services/IDataService.cs b/API_SQLuedo/Services/IDataService.cs index c296774..462d8d5 100644 --- a/API_SQLuedo/Services/IDataService.cs +++ b/API_SQLuedo/Services/IDataService.cs @@ -6,23 +6,8 @@ namespace Services { public interface IDataService { - public Task> GetUsers(int page, int number); - public Task GetUserById(int id); - public Task GetUserByUsername(string username); - public Task DeleteUser(int id); - public Task UpdateUser(int id, UserDTO user); - public Task CreateUser(string username, string password, string email, bool isAdmin); - public IEnumerable GetInquiries(int page, int number); - public InquiryDTO GetInquiryById(int id); - public InquiryDTO GetInquiryByTitle(string title); - public bool DeleteInquiry(int id); - public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry); - public UserDTO CreateInquiry(string title, string description, bool isUser, InquiryTableEntity database, SolutionEntity inquiryTable); - public IEnumerable GetLessons(int page, int number); - public InquiryDTO GetLessonById(int id); - public InquiryDTO GetLessonByTitle(string title); - public bool DeleteLesson(int id); - public InquiryDTO UpdateLesson(int id, LessonDTO lesson); - public UserDTO CreateLesson(string title, string lastPublisher, DateOnly lastEdit); + IUserDataService UserService { get; } + IInquiryDataService InquiryDataService { get; } + ILessonDataService LessonDataService { get; } } } diff --git a/API_SQLuedo/Services/IInquiryDataService.cs b/API_SQLuedo/Services/IInquiryDataService.cs new file mode 100644 index 0000000..0e71be5 --- /dev/null +++ b/API_SQLuedo/Services/IInquiryDataService.cs @@ -0,0 +1,21 @@ +using Entities.SQLudeoDB; +using Model.DTO; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Services +{ + public interface IInquiryDataService : IGenericService + { + public IEnumerable GetInquiries(int page, int number); + public InquiryDTO GetInquiryById(int id); + public InquiryDTO GetInquiryByTitle(string title); + public bool DeleteInquiry(int id); + public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry); + public UserDTO CreateInquiry(string title, string description, bool isUser, InquiryTableEntity database, SolutionEntity inquiryTable); + } +} diff --git a/API_SQLuedo/Services/ILessonDataService.cs b/API_SQLuedo/Services/ILessonDataService.cs new file mode 100644 index 0000000..7321547 --- /dev/null +++ b/API_SQLuedo/Services/ILessonDataService.cs @@ -0,0 +1,20 @@ +using Model.DTO; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Services +{ + public interface ILessonDataService : IGenericService + { + public IEnumerable GetLessons(int page, int number); + public InquiryDTO GetLessonById(int id); + public InquiryDTO GetLessonByTitle(string title); + public bool DeleteLesson(int id); + public InquiryDTO UpdateLesson(int id, LessonDTO lesson); + public UserDTO CreateLesson(string title, string lastPublisher, DateOnly lastEdit); + } +} diff --git a/API_SQLuedo/Services/Services.csproj b/API_SQLuedo/Services/Services.csproj index dd690cb..4f252a2 100644 --- a/API_SQLuedo/Services/Services.csproj +++ b/API_SQLuedo/Services/Services.csproj @@ -24,6 +24,7 @@ + diff --git a/API_SQLuedo/Services/UserDataService.cs b/API_SQLuedo/Services/UserDataService.cs index 4bffa90..141b251 100644 --- a/API_SQLuedo/Services/UserDataService.cs +++ b/API_SQLuedo/Services/UserDataService.cs @@ -13,28 +13,27 @@ using ModelToEntity; namespace Services { - public class UserDataService : IDataService + public class UserDataService : IUserDataService { - private UserDbContext DbContext { get; set; } private readonly IDataServiceEF dataServiceEF; public UserDataService(IDataServiceEF dataServiceEF) { this.dataServiceEF = dataServiceEF; } - public async Task GetUserById(int id) - { - var user = await dataServiceEF.GetUserById(id); + public async Task GetUserById(int id) + { + var user = await dataServiceEF.GetUserById(id); return user.FromModelToDTO(); } - public async Task GetUserByUsername(string username) + public async Task GetUserByUsername(string username) { var user = await dataServiceEF.GetUserByUsername(username); return user.FromModelToDTO(); } - public async Task> GetUsers(int page, int number) + public async Task> GetUsers(int page, int number) { var users = await dataServiceEF.GetUsers(page, number); return users.Select(u => u.FromModelToDTO()); @@ -46,72 +45,18 @@ namespace Services return respons; } - public async Task UpdateUser(int id, UserDTO user) + public async Task UpdateUser(int id, UserDTO user) { var updatingUser = await dataServiceEF.UpdateUser(id, user); return updatingUser.FromModelToDTO(); } - public async Task CreateUser(string username, string password, string email, bool isAdmin) + public async Task CreateUser(string username, string password, string email, bool isAdmin) { var newUserEntity = await dataServiceEF.CreateUser(username, password, email, isAdmin); return newUserEntity.FromModelToDTO(); - } - - public IEnumerable GetInquiries(int page, int number) - { - throw new NotImplementedException(); - } - - public InquiryDTO GetInquiryById(int id) - { - throw new NotImplementedException(); - } - - public InquiryDTO GetInquiryByTitle(string title) - { - throw new NotImplementedException(); - } - public UserDTO CreateInquiry(string title, string description, bool isUser, InquiryTableEntity database, SolutionEntity inquiryTable) - { - throw new NotImplementedException(); - } - - public bool DeleteInquiry(int id) - { - throw new NotImplementedException(); - } - - public InquiryDTO UpdateInquiry(int id, InquiryDTO inquiry) - { - throw new NotImplementedException(); - } - - public IEnumerable GetLessons(int page, int number) - { - throw new NotImplementedException(); - } - - public InquiryDTO GetLessonById(int id) - { - throw new NotImplementedException(); - } - - public InquiryDTO GetLessonByTitle(string title) - { - throw new NotImplementedException(); - } - - public bool DeleteLesson(int id) - { - throw new NotImplementedException(); - } - - public InquiryDTO UpdateLesson(int id, LessonDTO lesson) - { - throw new NotImplementedException(); } - public UserDTO CreateLesson(string title, string lastPublisher, DateOnly lastEdit) + public async Task AddItem(T? item) { throw new NotImplementedException(); } diff --git a/API_SQLuedo/Shared/IGenericService.cs b/API_SQLuedo/Shared/IGenericService.cs new file mode 100644 index 0000000..f34b3f1 --- /dev/null +++ b/API_SQLuedo/Shared/IGenericService.cs @@ -0,0 +1,7 @@ +namespace Shared +{ + public interface IGenericService + { + public Task AddItem(T? item); + } +} diff --git a/API_SQLuedo/Shared/IUserDataService.cs b/API_SQLuedo/Shared/IUserDataService.cs new file mode 100644 index 0000000..fc5bca7 --- /dev/null +++ b/API_SQLuedo/Shared/IUserDataService.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Services; +using Model.DTO; +using Shared; + +namespace Services +{ + public interface IUserDataService : IGenericService + { + public Task> GetUsers(int page, int number); + public Task GetUserById(int id); + public Task GetUserByUsername(string username); + public Task DeleteUser(int id); + public Task UpdateUser(int id, UserDTO user); + public Task CreateUser(string username, string password, string email, bool isAdmin); + + } +} diff --git a/API_SQLuedo/Shared/Shared.csproj b/API_SQLuedo/Shared/Shared.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/API_SQLuedo/Shared/Shared.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + From d3154b9552ab2ab71a7205c62cce196f3332e002 Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Sat, 24 Feb 2024 10:06:04 +0100 Subject: [PATCH 2/8] =?UTF-8?q?r=C3=A9solution=20d'=C3=A9rreur=20sur=20l'i?= =?UTF-8?q?mpl=C3=A9mentation=20des=20diff=C3=A9rentes=20intefaces?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../API/Controllers/InquiryController.cs | 22 +++++++++---------- .../API/Controllers/LessonController.cs | 22 +++++++++---------- API_SQLuedo/API/Controllers/UserController.cs | 4 ++-- API_SQLuedo/API/Program.cs | 6 +++-- API_SQLuedo/API_SQLuedo.sln | 8 ++++++- .../ModelToEntity/ModelToEntity.csproj | 1 + API_SQLuedo/Services/DataBase.cs | 20 +++++++++++++++++ API_SQLuedo/Services/LessonDataService.cs | 12 ++++++++++ .../{Services => Shared}/IDataService.cs | 4 ++-- .../IInquiryDataService.cs | 0 .../ILessonDataService.cs | 0 API_SQLuedo/Shared/Shared.csproj | 4 ++++ 12 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 API_SQLuedo/Services/DataBase.cs create mode 100644 API_SQLuedo/Services/LessonDataService.cs rename API_SQLuedo/{Services => Shared}/IDataService.cs (71%) rename API_SQLuedo/{Services => Shared}/IInquiryDataService.cs (100%) rename API_SQLuedo/{Services => Shared}/ILessonDataService.cs (100%) diff --git a/API_SQLuedo/API/Controllers/InquiryController.cs b/API_SQLuedo/API/Controllers/InquiryController.cs index c6326ad..76efd2f 100644 --- a/API_SQLuedo/API/Controllers/InquiryController.cs +++ b/API_SQLuedo/API/Controllers/InquiryController.cs @@ -10,26 +10,26 @@ namespace API.Controllers [ApiController] public class InquiryController : Controller { - private IDataService _inquiryDataService; + private IDataService _dataService; private readonly ILogger _logger; - public InquiryController(IDataService inquiryDataService) + public InquiryController(IDataService dataService) { - _inquiryDataService = inquiryDataService; + _dataService = _dataService; } [HttpGet("inquiries/{page}/{number}")] public IActionResult GetInquiries(int page, int number) { - var nbInquiry = _inquiryDataService.GetInquiries(page, number).Count(); + var nbInquiry = _dataService.InquiryDataService.GetInquiries(page, number).Count(); if (nbInquiry == 0) { _logger.LogError("[ERREUR] Aucune enquête trouvée."); return StatusCode(204); } _logger.LogInformation("[INFORMATION] {nb} Enquête(s) trouvée(s)", nbInquiry); - return Ok(_inquiryDataService.GetInquiries(page, number)); + return Ok(_dataService.InquiryDataService.GetInquiries(page, number)); } [HttpGet("inquiry/id/{id}")] @@ -38,7 +38,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] L'enquête avec l'id {id} a été trouvé.", id); - return Ok(_inquiryDataService.GetInquiryById(id)); + return Ok(_dataService.InquiryDataService.GetInquiryById(id)); } catch (ArgumentException) { @@ -53,7 +53,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] L'enquête avec le titre {title} a été trouvé.", title); - return Ok(_inquiryDataService.GetInquiryByTitle(title)); + return Ok(_dataService.InquiryDataService.GetInquiryByTitle(title)); } catch (ArgumentException) { @@ -65,11 +65,11 @@ namespace API.Controllers [HttpDelete] public IActionResult DeleteInquiry(int id) { - var success = _inquiryDataService.DeleteInquiry(id); + var success = _dataService.InquiryDataService.DeleteInquiry(id); if (success) { _logger.LogInformation("[INFORMATION] L'enquête avec l'id {id} a été supprimé.", id); - return Ok(_inquiryDataService.DeleteInquiry(id)); + return Ok(_dataService.InquiryDataService.DeleteInquiry(id)); } else { @@ -87,7 +87,7 @@ namespace API.Controllers return BadRequest(); } _logger.LogInformation("[INFORMATION] Une enquête a été créé : title - {title}, description - {description}, isUser - {isUser}, database - {database}, inquiryTable - {inquiryTable}", dto.Title, dto.Description, dto.IsUser, dto.Database, dto.InquiryTable); - return Created(nameof(GetInquiries), _inquiryDataService.CreateInquiry(dto.Title, dto.Description, dto.IsUser, dto.Database, dto.InquiryTable)); + return Created(nameof(GetInquiries), _dataService.InquiryDataService.CreateInquiry(dto.Title, dto.Description, dto.IsUser, dto.Database, dto.InquiryTable)); } [HttpPut] @@ -106,7 +106,7 @@ namespace API.Controllers if (inquiryDTO != null) { _logger.LogInformation("[INFORMATION] La mise à jour de l'enquête avec l'id {id} a été effectuée", id); - return Ok(_inquiryDataService.UpdateInquiry(id, inquiryDTO)); + return Ok(_dataService.InquiryDataService.UpdateInquiry(id, inquiryDTO)); } _logger.LogError("[ERREUR] Aucune enquête trouvée avec l'id {id}.", id); return NotFound(); diff --git a/API_SQLuedo/API/Controllers/LessonController.cs b/API_SQLuedo/API/Controllers/LessonController.cs index 912196e..71490ed 100644 --- a/API_SQLuedo/API/Controllers/LessonController.cs +++ b/API_SQLuedo/API/Controllers/LessonController.cs @@ -10,26 +10,26 @@ namespace API.Controllers [ApiController] public class LessonController : Controller { - private IDataService _lessonDataService; + private IDataService _dataService; private readonly ILogger _logger; - public LessonController(IDataService lessonDataService) + public LessonController(IDataService dataService) { - _lessonDataService = lessonDataService; + _dataService = dataService; } [HttpGet("lessons/{page}/{number}")] public IActionResult GetLessons(int page, int number) { - var nbLesson = _lessonDataService.GetInquiries(page, number).Count(); + var nbLesson = _dataService.LessonDataService.GetLessons(page, number).Count(); if (nbLesson == 0) { _logger.LogError("[ERREUR] Aucune leçon trouvée."); return StatusCode(204); } _logger.LogInformation("[INFORMATION] {nb} Leçon(s) trouvée(s)", nbLesson); - return Ok(_lessonDataService.GetLessons(page, number)); + return Ok(_dataService.LessonDataService.GetLessons(page, number)); } [HttpGet("lesson/id/{id}")] @@ -38,7 +38,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] La leçon avec l'id {id} a été trouvé.", id); - return Ok(_lessonDataService.GetLessonById(id)); + return Ok(_dataService.LessonDataService.GetLessonById(id)); } catch (ArgumentException) { @@ -53,7 +53,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] La leçon avec le titre {title} a été trouvé.", title); - return Ok(_lessonDataService.GetLessonByTitle(title)); + return Ok(_dataService.LessonDataService.GetLessonByTitle(title)); } catch (ArgumentException) { @@ -65,11 +65,11 @@ namespace API.Controllers [HttpDelete] public IActionResult DeleteLesson(int id) { - var success = _lessonDataService.DeleteLesson(id); + var success = _dataService.LessonDataService.DeleteLesson(id); if (success) { _logger.LogInformation("[INFORMATION] La leçon avec l'id {id} a été supprimé.", id); - return Ok(_lessonDataService.DeleteLesson(id)); + return Ok(_dataService.LessonDataService.DeleteLesson(id)); } else { @@ -87,7 +87,7 @@ namespace API.Controllers return BadRequest(); } _logger.LogInformation("[INFORMATION] Une leçon a été créé : title - {title}, lastPublisher - {publisher}, lastEdit - {lastEdit}", dto.Title, dto.LastPublisher, dto.LastEdit); - return Created(nameof(GetLessons), _lessonDataService.CreateLesson(dto.Title, dto.LastPublisher, dto.LastEdit)); + return Created(nameof(GetLessons), _dataService.LessonDataService.CreateLesson(dto.Title, dto.LastPublisher, dto.LastEdit)); } [HttpPut] @@ -106,7 +106,7 @@ namespace API.Controllers if (lessonDTO != null) { _logger.LogInformation("[INFORMATION] La mise à jour de la leçon avec l'id {id} a été effectuée", id); - return Ok(_lessonDataService.UpdateLesson(id, lessonDTO)); + return Ok(_dataService.LessonDataService.UpdateLesson(id, lessonDTO)); } _logger.LogError("[ERREUR] Aucune leçon trouvée avec l'id {id}.", id); return NotFound(); diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UserController.cs index 3e17320..e261d4e 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UserController.cs @@ -13,11 +13,11 @@ namespace API.Controllers [ApiController] public class UserController : Controller { - private IDataService _dataService; + private IDataService _dataService; private readonly ILogger _logger; - public UserController(IDataService dataService, ILogger logger) + public UserController(IDataService dataService, ILogger logger) { _dataService = dataService; _logger = logger; diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index 579f60f..cf07659 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -3,6 +3,8 @@ using DbContextLib; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; +using Model.DTO; +using Model.Business; using ModelToEntity; using Services; using System.Data.Common; @@ -15,8 +17,8 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped, DataBase>(); +builder.Services.AddScoped, DbDataManager>(); builder.Services.AddDbContext(); builder.Services.AddDbContext(options => options.UseInMemoryDatabase("appDb")); builder.Services.AddIdentityApiEndpoints().AddEntityFrameworkStores(); diff --git a/API_SQLuedo/API_SQLuedo.sln b/API_SQLuedo/API_SQLuedo.sln index f9b50ec..0d2bfac 100644 --- a/API_SQLuedo/API_SQLuedo.sln +++ b/API_SQLuedo/API_SQLuedo.sln @@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbContextLib", "DbContextLi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Services", "Services\Services.csproj", "{9BD3DCBA-AFD8-47FA-B07C-613B53E63968}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelToEntity", "ModelToEntity\ModelToEntity.csproj", "{8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModelToEntity", "ModelToEntity\ModelToEntity.csproj", "{8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{B28FD539-6FE4-4B13-9C1F-D88F4771CC69}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -57,6 +59,10 @@ Global {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Debug|Any CPU.Build.0 = Debug|Any CPU {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Release|Any CPU.ActiveCfg = Release|Any CPU {8F53CC62-94B3-4F9D-ABF1-F7307A6F27C0}.Release|Any CPU.Build.0 = Release|Any CPU + {B28FD539-6FE4-4B13-9C1F-D88F4771CC69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B28FD539-6FE4-4B13-9C1F-D88F4771CC69}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B28FD539-6FE4-4B13-9C1F-D88F4771CC69}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B28FD539-6FE4-4B13-9C1F-D88F4771CC69}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/API_SQLuedo/ModelToEntity/ModelToEntity.csproj b/API_SQLuedo/ModelToEntity/ModelToEntity.csproj index 4cd7b1b..472379f 100644 --- a/API_SQLuedo/ModelToEntity/ModelToEntity.csproj +++ b/API_SQLuedo/ModelToEntity/ModelToEntity.csproj @@ -23,6 +23,7 @@ + diff --git a/API_SQLuedo/Services/DataBase.cs b/API_SQLuedo/Services/DataBase.cs new file mode 100644 index 0000000..63778c1 --- /dev/null +++ b/API_SQLuedo/Services/DataBase.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Model.Business; +using Model.DTO; +using Shared; + +namespace Services +{ + public class DataBase : IDataService + { + public IUserDataService UserService { get; } + + public IInquiryDataService InquiryDataService { get; } + + public ILessonDataService LessonDataService { get; } + } +} diff --git a/API_SQLuedo/Services/LessonDataService.cs b/API_SQLuedo/Services/LessonDataService.cs new file mode 100644 index 0000000..a21a5a7 --- /dev/null +++ b/API_SQLuedo/Services/LessonDataService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Services +{ + public class LessonDataService + { + } +} diff --git a/API_SQLuedo/Services/IDataService.cs b/API_SQLuedo/Shared/IDataService.cs similarity index 71% rename from API_SQLuedo/Services/IDataService.cs rename to API_SQLuedo/Shared/IDataService.cs index 462d8d5..6080a02 100644 --- a/API_SQLuedo/Services/IDataService.cs +++ b/API_SQLuedo/Shared/IDataService.cs @@ -4,9 +4,9 @@ using Model.DTO; namespace Services { - public interface IDataService + public interface IDataService { - IUserDataService UserService { get; } + IUserDataService UserService { get; } IInquiryDataService InquiryDataService { get; } ILessonDataService LessonDataService { get; } } diff --git a/API_SQLuedo/Services/IInquiryDataService.cs b/API_SQLuedo/Shared/IInquiryDataService.cs similarity index 100% rename from API_SQLuedo/Services/IInquiryDataService.cs rename to API_SQLuedo/Shared/IInquiryDataService.cs diff --git a/API_SQLuedo/Services/ILessonDataService.cs b/API_SQLuedo/Shared/ILessonDataService.cs similarity index 100% rename from API_SQLuedo/Services/ILessonDataService.cs rename to API_SQLuedo/Shared/ILessonDataService.cs diff --git a/API_SQLuedo/Shared/Shared.csproj b/API_SQLuedo/Shared/Shared.csproj index fa71b7a..b3751de 100644 --- a/API_SQLuedo/Shared/Shared.csproj +++ b/API_SQLuedo/Shared/Shared.csproj @@ -6,4 +6,8 @@ enable + + + + From df34ec238a5cda761c5bb95cc971284fd1289bc9 Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Sat, 24 Feb 2024 14:34:46 +0100 Subject: [PATCH 3/8] correction de bug quand on apelle les interface :bug: --- API_SQLuedo/API/Program.cs | 3 ++- API_SQLuedo/Services/{DataBase.cs => DataService.cs} | 6 +++++- API_SQLuedo/Services/UserDataService.cs | 4 ++-- API_SQLuedo/Shared/IUserDataService.cs | 1 + 4 files changed, 10 insertions(+), 4 deletions(-) rename API_SQLuedo/Services/{DataBase.cs => DataService.cs} (67%) diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index cf07659..e3828ff 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -17,7 +17,8 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddScoped, DataBase>(); +builder.Services.AddScoped, UserDataService>(); +builder.Services.AddScoped, DataService>(); builder.Services.AddScoped, DbDataManager>(); builder.Services.AddDbContext(); builder.Services.AddDbContext(options => options.UseInMemoryDatabase("appDb")); diff --git a/API_SQLuedo/Services/DataBase.cs b/API_SQLuedo/Services/DataService.cs similarity index 67% rename from API_SQLuedo/Services/DataBase.cs rename to API_SQLuedo/Services/DataService.cs index 63778c1..cb0e072 100644 --- a/API_SQLuedo/Services/DataBase.cs +++ b/API_SQLuedo/Services/DataService.cs @@ -9,9 +9,13 @@ using Shared; namespace Services { - public class DataBase : IDataService + public class DataService : IDataService { public IUserDataService UserService { get; } + public DataService(IUserDataService userDataService) + { + UserService = userDataService; + } public IInquiryDataService InquiryDataService { get; } diff --git a/API_SQLuedo/Services/UserDataService.cs b/API_SQLuedo/Services/UserDataService.cs index 141b251..f46d725 100644 --- a/API_SQLuedo/Services/UserDataService.cs +++ b/API_SQLuedo/Services/UserDataService.cs @@ -15,8 +15,8 @@ namespace Services { public class UserDataService : IUserDataService { - private readonly IDataServiceEF dataServiceEF; - public UserDataService(IDataServiceEF dataServiceEF) + private readonly IUserDataService dataServiceEF; + public UserDataService(IUserDataService dataServiceEF) { this.dataServiceEF = dataServiceEF; } diff --git a/API_SQLuedo/Shared/IUserDataService.cs b/API_SQLuedo/Shared/IUserDataService.cs index fc5bca7..16cfacc 100644 --- a/API_SQLuedo/Shared/IUserDataService.cs +++ b/API_SQLuedo/Shared/IUserDataService.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Services; using Model.DTO; using Shared; +using Model.Business; namespace Services { From 5f68add0fee5fc04905e972269619f986a52880e Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Sat, 24 Feb 2024 16:12:03 +0100 Subject: [PATCH 4/8] =?UTF-8?q?ajout=20d'un=20methode=20d'extension=20g?= =?UTF-8?q?=C3=A9n=C3=A9rique=20pour=20ajouter=20Item?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/API/Controllers/UserController.cs | 4 +++- API_SQLuedo/ModelToEntity/DbDataManager.cs | 8 ++++++-- API_SQLuedo/ModelToEntity/Extension.cs | 11 ++++++----- API_SQLuedo/Services/UserDataService.cs | 5 +++-- API_SQLuedo/Shared/IDataService.cs | 2 +- API_SQLuedo/Shared/IGenericService.cs | 4 ++-- API_SQLuedo/Shared/IUserDataService.cs | 2 +- 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UserController.cs index e261d4e..32acebe 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UserController.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Model.Business; using Model.DTO; +using Model.Mappers; using Services; namespace API.Controllers @@ -90,7 +91,8 @@ namespace API.Controllers } _logger.LogInformation("[INFORMATION] Un utilisateur a été créé : username - {username}, password - {password}, email - {email}, isAdmin - {isAdmin}", dto.Username, dto.Password, dto.Email, dto.IsAdmin); - return Created(nameof(GetUsers), _dataService.UserService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin)); + //return Created(nameof(GetUsers), _dataService.UserService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin)); + return Created(nameof(GetUsers), _dataService.UserService.AddItem(dto.FromDTOToModel().FromModelToEntity())); } [HttpPut] diff --git a/API_SQLuedo/ModelToEntity/DbDataManager.cs b/API_SQLuedo/ModelToEntity/DbDataManager.cs index 9e87320..2f144f4 100644 --- a/API_SQLuedo/ModelToEntity/DbDataManager.cs +++ b/API_SQLuedo/ModelToEntity/DbDataManager.cs @@ -158,9 +158,13 @@ namespace ModelToEntity throw new NotImplementedException(); } - public Task AddItem(T? item) + public async Task AddItem(T? item) where T : class { - throw new NotImplementedException(); + using (var context = new UserDbContext()) + { + return await context.CreateItemAsync(item); + } + } } diff --git a/API_SQLuedo/ModelToEntity/Extension.cs b/API_SQLuedo/ModelToEntity/Extension.cs index b63e770..63f3b3b 100644 --- a/API_SQLuedo/ModelToEntity/Extension.cs +++ b/API_SQLuedo/ModelToEntity/Extension.cs @@ -15,17 +15,18 @@ namespace ModelToEntity { internal static class Extension { - internal static async Task AddItemAsync(this DbContext context, T? item) where T : class + public static Task CreateItemAsync(this DbContext context, T? item) where T : class { - if (item == null || await context.Set().ContainsAsync(item)) + if (item == null || context.Set().Contains(item)) { - return default(T); + return Task.FromResult(default(T)); } context.Set().Add(item); - await context.SaveChangesAsync(); + context.SaveChangesAsync(); - return item; + return Task.FromResult(item); ; } + } } \ No newline at end of file diff --git a/API_SQLuedo/Services/UserDataService.cs b/API_SQLuedo/Services/UserDataService.cs index f46d725..a1263d8 100644 --- a/API_SQLuedo/Services/UserDataService.cs +++ b/API_SQLuedo/Services/UserDataService.cs @@ -56,9 +56,10 @@ namespace Services return newUserEntity.FromModelToDTO(); } - public async Task AddItem(T? item) + public async Task AddItem(T? item) where T : class { - throw new NotImplementedException(); + var newItem = dataServiceEF.AddItem(item); + return await newItem; } } } diff --git a/API_SQLuedo/Shared/IDataService.cs b/API_SQLuedo/Shared/IDataService.cs index 6080a02..7c3b82b 100644 --- a/API_SQLuedo/Shared/IDataService.cs +++ b/API_SQLuedo/Shared/IDataService.cs @@ -4,7 +4,7 @@ using Model.DTO; namespace Services { - public interface IDataService + public interface IDataService where T : class { IUserDataService UserService { get; } IInquiryDataService InquiryDataService { get; } diff --git a/API_SQLuedo/Shared/IGenericService.cs b/API_SQLuedo/Shared/IGenericService.cs index f34b3f1..fbcfb70 100644 --- a/API_SQLuedo/Shared/IGenericService.cs +++ b/API_SQLuedo/Shared/IGenericService.cs @@ -1,7 +1,7 @@ namespace Shared { - public interface IGenericService + public interface IGenericService where T : class { - public Task AddItem(T? item); + public Task AddItem(T? item) where T : class; } } diff --git a/API_SQLuedo/Shared/IUserDataService.cs b/API_SQLuedo/Shared/IUserDataService.cs index 16cfacc..bf2f2e2 100644 --- a/API_SQLuedo/Shared/IUserDataService.cs +++ b/API_SQLuedo/Shared/IUserDataService.cs @@ -10,7 +10,7 @@ using Model.Business; namespace Services { - public interface IUserDataService : IGenericService + public interface IUserDataService : IGenericService where T : class { public Task> GetUsers(int page, int number); public Task GetUserById(int id); From 7c2cc6f5ce03ee1eca2b66ea16a88306601685fd Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Sat, 24 Feb 2024 17:08:36 +0100 Subject: [PATCH 5/8] =?UTF-8?q?ajout=20d'un=20m=C3=A9thode=20d'extension?= =?UTF-8?q?=20g=C3=A9n=C3=A9rique=20pour=20la=20suppression=20d'un=20item?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/API/Controllers/UserController.cs | 3 ++- API_SQLuedo/ModelToEntity/DbDataManager.cs | 8 +++++++ API_SQLuedo/ModelToEntity/Extension.cs | 22 +++++++++++++++++-- API_SQLuedo/Services/UserDataService.cs | 6 +++++ API_SQLuedo/Shared/IGenericService.cs | 1 + 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UserController.cs index 32acebe..1d51961 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UserController.cs @@ -1,4 +1,5 @@ using DbContextLib; +using Entities.SQLudeoDB; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -69,7 +70,7 @@ namespace API.Controllers [HttpDelete] public async Task DeleteUser(int id) { - var success = await _dataService.UserService.DeleteUser(id); + var success = await _dataService.UserService.DeleteItem(id); if(success) { _logger.LogInformation("[INFORMATION] L'utilisateur avec l'id {id} a été supprimé.", id); diff --git a/API_SQLuedo/ModelToEntity/DbDataManager.cs b/API_SQLuedo/ModelToEntity/DbDataManager.cs index 2f144f4..6ec1af8 100644 --- a/API_SQLuedo/ModelToEntity/DbDataManager.cs +++ b/API_SQLuedo/ModelToEntity/DbDataManager.cs @@ -166,6 +166,14 @@ namespace ModelToEntity } } + + public async Task DeleteItem(int id) where T : class + { + using(var context = new UserDbContext()) + { + return await context.DeleteItemAsync(id); + } + } } } diff --git a/API_SQLuedo/ModelToEntity/Extension.cs b/API_SQLuedo/ModelToEntity/Extension.cs index 63f3b3b..d40c8c9 100644 --- a/API_SQLuedo/ModelToEntity/Extension.cs +++ b/API_SQLuedo/ModelToEntity/Extension.cs @@ -15,7 +15,7 @@ namespace ModelToEntity { internal static class Extension { - public static Task CreateItemAsync(this DbContext context, T? item) where T : class + internal static Task CreateItemAsync(this DbContext context, T? item) where T : class { if (item == null || context.Set().Contains(item)) { @@ -25,7 +25,25 @@ namespace ModelToEntity context.Set().Add(item); context.SaveChangesAsync(); - return Task.FromResult(item); ; + return Task.FromResult(item); + } + internal static async Task DeleteItemAsync(this DbContext context, int? id) where T : class + { + if (id == null) + { + return false; + } + + var entity = await context.Set().FindAsync(id); + if (entity == null) + { + return false; + } + + context.Set().Remove(entity); + await context.SaveChangesAsync(); + + return true; } } diff --git a/API_SQLuedo/Services/UserDataService.cs b/API_SQLuedo/Services/UserDataService.cs index a1263d8..624d9ef 100644 --- a/API_SQLuedo/Services/UserDataService.cs +++ b/API_SQLuedo/Services/UserDataService.cs @@ -61,5 +61,11 @@ namespace Services var newItem = dataServiceEF.AddItem(item); return await newItem; } + + public Task DeleteItem(int id) where T : class + { + var succes = dataServiceEF.DeleteItem(id); + return succes; + } } } diff --git a/API_SQLuedo/Shared/IGenericService.cs b/API_SQLuedo/Shared/IGenericService.cs index fbcfb70..2b24d84 100644 --- a/API_SQLuedo/Shared/IGenericService.cs +++ b/API_SQLuedo/Shared/IGenericService.cs @@ -3,5 +3,6 @@ public interface IGenericService where T : class { public Task AddItem(T? item) where T : class; + public Task DeleteItem(int id) where T : class; } } From de66affddc0ab225b8a5489d43ff8195df1f661a Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Sat, 24 Feb 2024 19:07:35 +0100 Subject: [PATCH 6/8] =?UTF-8?q?ajout=20d'UpdateItem=20g=C3=A9n=C3=A9rique,?= =?UTF-8?q?=20la=20mise=20=C3=A0=20jour=20se=20fait=20en=20base=20de=20don?= =?UTF-8?q?n=C3=A9e=20mais=20PUT=20renvoie=20un=20code=20de=20retour=20500?= =?UTF-8?q?=20car=20elle=20ne=20renvoie=20pas=20un=20object=20s=C3=A9riali?= =?UTF-8?q?zable=20:=20=C3=A0=20corriger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/API/Controllers/UserController.cs | 2 +- API_SQLuedo/ModelToEntity/DbDataManager.cs | 10 ++++++++ API_SQLuedo/ModelToEntity/Extension.cs | 24 +++++++++++++++++++ API_SQLuedo/Services/UserDataService.cs | 6 +++++ API_SQLuedo/Shared/IGenericService.cs | 2 ++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UserController.cs index 1d51961..c636ab0 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UserController.cs @@ -112,7 +112,7 @@ namespace API.Controllers if(userDTO != null) { _logger.LogInformation("[INFORMATION] La mise à jour de l'utilisateur avec l'id {id} a été effectuée", id); - return Ok(_dataService.UserService.UpdateUser(id, userDTO)); + return Ok(_dataService.UserService.UpdateItem(id, userDTO)); } _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id); return NotFound(); diff --git a/API_SQLuedo/ModelToEntity/DbDataManager.cs b/API_SQLuedo/ModelToEntity/DbDataManager.cs index 6ec1af8..3dcd2ed 100644 --- a/API_SQLuedo/ModelToEntity/DbDataManager.cs +++ b/API_SQLuedo/ModelToEntity/DbDataManager.cs @@ -6,6 +6,7 @@ using DbContextLib; using Model.Business; using Model; using Model.Mappers; +using System; namespace ModelToEntity { @@ -174,6 +175,15 @@ namespace ModelToEntity return await context.DeleteItemAsync(id); } } + public async Task UpdateItem(int? id, TDto? newItem) + where T : class + where TDto : class + { + using(var context = new UserDbContext()) + { + return await context.UpdateItemAsync(id, newItem); + } + } } } diff --git a/API_SQLuedo/ModelToEntity/Extension.cs b/API_SQLuedo/ModelToEntity/Extension.cs index d40c8c9..6a14122 100644 --- a/API_SQLuedo/ModelToEntity/Extension.cs +++ b/API_SQLuedo/ModelToEntity/Extension.cs @@ -45,6 +45,30 @@ namespace ModelToEntity return true; } + internal static Task UpdateItemAsync(this DbContext context, int? id, TDto dto) + where T : class + where TDto : class + { + var entity = context.Set().FindAsync(id); + if (entity == null) + { + throw new ArgumentException("Impossible de trouver l'entité", nameof(id)); + } + + var propertiesToUpdate = typeof(TDto).GetProperties() + .Where(p => p.CanRead && p.CanWrite); + + foreach (var property in propertiesToUpdate) + { + var value = property.GetValue(dto); + typeof(T).GetProperty(property.Name)?.SetValue(entity, value); + } + + context.Entry(entity).State = EntityState.Modified; + context.SaveChangesAsync(); + + return Task.FromResult(entity as T); + } } } \ No newline at end of file diff --git a/API_SQLuedo/Services/UserDataService.cs b/API_SQLuedo/Services/UserDataService.cs index 624d9ef..0566455 100644 --- a/API_SQLuedo/Services/UserDataService.cs +++ b/API_SQLuedo/Services/UserDataService.cs @@ -67,5 +67,11 @@ namespace Services var succes = dataServiceEF.DeleteItem(id); return succes; } + + public Task UpdateItem(int? id, TDto? newItem) where T : class where TDto : class + { + var item = dataServiceEF.UpdateItem(id, newItem); + return item; + } } } diff --git a/API_SQLuedo/Shared/IGenericService.cs b/API_SQLuedo/Shared/IGenericService.cs index 2b24d84..d5af118 100644 --- a/API_SQLuedo/Shared/IGenericService.cs +++ b/API_SQLuedo/Shared/IGenericService.cs @@ -4,5 +4,7 @@ { public Task AddItem(T? item) where T : class; public Task DeleteItem(int id) where T : class; + + public Task UpdateItem(int? id, TDto? newItem) where T : class where TDto : class; } } From a307f68fc3a5d026febb61821d38a57667ce492e Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Sat, 24 Feb 2024 19:51:55 +0100 Subject: [PATCH 7/8] =?UTF-8?q?correction=20du=20bug=20sur=20le=20renvoie?= =?UTF-8?q?=20d'un=20object=20non=20s=C3=A9rializable=20par=20la=20m=C3=A9?= =?UTF-8?q?thode=20UpdatItem=20qui=20provoquait=20une=20erreur=20500=20cot?= =?UTF-8?q?=C3=A9=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/ModelToEntity/Extension.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/API_SQLuedo/ModelToEntity/Extension.cs b/API_SQLuedo/ModelToEntity/Extension.cs index 6a14122..72ff9ba 100644 --- a/API_SQLuedo/ModelToEntity/Extension.cs +++ b/API_SQLuedo/ModelToEntity/Extension.cs @@ -49,7 +49,7 @@ namespace ModelToEntity where T : class where TDto : class { - var entity = context.Set().FindAsync(id); + var entity = context.Set().Find(id); if (entity == null) { throw new ArgumentException("Impossible de trouver l'entité", nameof(id)); @@ -65,9 +65,9 @@ namespace ModelToEntity } context.Entry(entity).State = EntityState.Modified; - context.SaveChangesAsync(); + context.SaveChanges(); - return Task.FromResult(entity as T); + return Task.FromResult(entity); } } From e476fd6c826f68c15669a3bcc4f392b651ad4f29 Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Sun, 25 Feb 2024 11:23:20 +0100 Subject: [PATCH 8/8] =?UTF-8?q?ajout=20d'un=20option=20dans=20UserDbContex?= =?UTF-8?q?t=20pour=20avoir=20plus=20de=20d=C3=A9taille=20sur=20les=20logs?= =?UTF-8?q?.=20Ajout=20de=20GetItems?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/API/Controllers/UserController.cs | 8 ++--- API_SQLuedo/API/Program.cs | 1 + API_SQLuedo/DbContextLib/UserDbContext.cs | 2 +- API_SQLuedo/Model/UserProperty.cs | 18 +++++++++++ API_SQLuedo/ModelToEntity/DbDataManager.cs | 13 ++++++-- API_SQLuedo/ModelToEntity/Extension.cs | 32 ++++++++++++++++++- API_SQLuedo/Services/UserDataService.cs | 6 ++++ API_SQLuedo/Shared/IGenericService.cs | 2 +- 8 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 API_SQLuedo/Model/UserProperty.cs diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UserController.cs index c636ab0..95d5067 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UserController.cs @@ -3,6 +3,7 @@ using Entities.SQLudeoDB; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Model; using Model.Business; using Model.DTO; using Model.Mappers; @@ -35,7 +36,7 @@ namespace API.Controllers return StatusCode(204); } _logger.LogInformation("[INFORMATION] {nb} Utilisateur(s) trouvé(s)", nbUser); - return Ok(_dataService.UserService.GetUsers(page, number)); + return Ok(_dataService.UserService.GetItems(page, number)); } [HttpGet("user/id/{id}")] @@ -44,7 +45,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] Utilisateur avec l'id {id} a été trouvé.", id); - return Ok(_dataService.UserService.GetUserById(id)); + return Ok(_dataService.UserService.GetItems(1, 1, UserProperty.Id.ToString(),id)); } catch (ArgumentException) { _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'id {id}.", id); @@ -58,7 +59,7 @@ namespace API.Controllers try { _logger.LogInformation("[INFORMATION] Utilisateur avec l'username {username} a été trouvé.", username); - return Ok(_dataService.UserService.GetUserByUsername(username)); + return Ok(_dataService.UserService.GetItems(1, 1,UserProperty.Username.ToString(), username)); }catch (ArgumentException) { _logger.LogError("[ERREUR] Aucun utilisateur trouvé avec l'username {username}.", username); @@ -92,7 +93,6 @@ namespace API.Controllers } _logger.LogInformation("[INFORMATION] Un utilisateur a été créé : username - {username}, password - {password}, email - {email}, isAdmin - {isAdmin}", dto.Username, dto.Password, dto.Email, dto.IsAdmin); - //return Created(nameof(GetUsers), _dataService.UserService.CreateUser(dto.Username, dto.Password, dto.Email, dto.IsAdmin)); return Created(nameof(GetUsers), _dataService.UserService.AddItem(dto.FromDTOToModel().FromModelToEntity())); } diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index e3828ff..c3d1a86 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -8,6 +8,7 @@ using Model.Business; using ModelToEntity; using Services; using System.Data.Common; +using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); diff --git a/API_SQLuedo/DbContextLib/UserDbContext.cs b/API_SQLuedo/DbContextLib/UserDbContext.cs index 4bf3c4e..a5acbf6 100644 --- a/API_SQLuedo/DbContextLib/UserDbContext.cs +++ b/API_SQLuedo/DbContextLib/UserDbContext.cs @@ -25,7 +25,7 @@ namespace DbContextLib { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseNpgsql("Host=localhost;Database=SQLuedo;Username=admin;Password=motdepasse"); + optionsBuilder.UseNpgsql("Host=localhost;Database=SQLuedo;Username=admin;Password=motdepasse").EnableSensitiveDataLogging(); } base.OnConfiguring(optionsBuilder); } diff --git a/API_SQLuedo/Model/UserProperty.cs b/API_SQLuedo/Model/UserProperty.cs new file mode 100644 index 0000000..75ecf9c --- /dev/null +++ b/API_SQLuedo/Model/UserProperty.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model +{ + public enum UserProperty + { + Id, + Username, + Password, + Email, + IsAdmin + + } +} diff --git a/API_SQLuedo/ModelToEntity/DbDataManager.cs b/API_SQLuedo/ModelToEntity/DbDataManager.cs index 3dcd2ed..c56dcf4 100644 --- a/API_SQLuedo/ModelToEntity/DbDataManager.cs +++ b/API_SQLuedo/ModelToEntity/DbDataManager.cs @@ -7,6 +7,8 @@ using Model.Business; using Model; using Model.Mappers; using System; +using Microsoft.AspNetCore.Identity; +using System.Runtime.InteropServices.ObjectiveC; namespace ModelToEntity { @@ -184,6 +186,13 @@ namespace ModelToEntity return await context.UpdateItemAsync(id, newItem); } } - } - + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, object? valueProperty = null) where T : class + { + using (var context = new UserDbContext()) + { + return await context.GetItemsWithFilter(index, count, orderingPropertyName, valueProperty); + } + } + } } diff --git a/API_SQLuedo/ModelToEntity/Extension.cs b/API_SQLuedo/ModelToEntity/Extension.cs index 72ff9ba..508e872 100644 --- a/API_SQLuedo/ModelToEntity/Extension.cs +++ b/API_SQLuedo/ModelToEntity/Extension.cs @@ -10,6 +10,8 @@ using Model.Business; using System.Diagnostics; using DbContextLib; using Microsoft.EntityFrameworkCore; +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore.Metadata.Internal; namespace ModelToEntity { @@ -17,7 +19,7 @@ namespace ModelToEntity { internal static Task CreateItemAsync(this DbContext context, T? item) where T : class { - if (item == null || context.Set().Contains(item)) + if (item == null || context.Set().Contains(item)) { return Task.FromResult(default(T)); } @@ -69,6 +71,34 @@ namespace ModelToEntity return Task.FromResult(entity); } + internal static Task> GetItemsWithFilter(this DbContext context, + int index, int count, string? orderingPropertyName = null, object valueProperty = null) where T : class + { + IQueryable query = context.Set(); + if (valueProperty != null && !string.IsNullOrEmpty(orderingPropertyName)) + { + var prop = typeof(T).GetProperty(orderingPropertyName); + if (prop != null) + { + var idProp = typeof(T).GetProperty(orderingPropertyName); + if (idProp != null) + { + var parameter = Expression.Parameter(typeof(T), "entity"); + var propertyAccess = Expression.Property(parameter, prop); + var constant = Expression.Constant(valueProperty); + var equality = Expression.Equal(propertyAccess, constant); + var lambda = Expression.Lambda>(equality, parameter); + var filteredEntity = context.Set().FirstOrDefault(lambda); + if (filteredEntity != null) + { + return Task.FromResult>(new List { filteredEntity }); + } + } + } + } + var items = query.Skip(index).Take(count).ToList(); + return Task.FromResult>(items); + } } } \ No newline at end of file diff --git a/API_SQLuedo/Services/UserDataService.cs b/API_SQLuedo/Services/UserDataService.cs index 0566455..e487d8d 100644 --- a/API_SQLuedo/Services/UserDataService.cs +++ b/API_SQLuedo/Services/UserDataService.cs @@ -73,5 +73,11 @@ namespace Services var item = dataServiceEF.UpdateItem(id, newItem); return item; } + + public Task> GetItems(int index, int count, string? orderingPropertyName = null, object? valueProperty = null) where T : class + { + var items = dataServiceEF.GetItems(index, count, orderingPropertyName, valueProperty); + return items; + } } } diff --git a/API_SQLuedo/Shared/IGenericService.cs b/API_SQLuedo/Shared/IGenericService.cs index d5af118..4e84eaf 100644 --- a/API_SQLuedo/Shared/IGenericService.cs +++ b/API_SQLuedo/Shared/IGenericService.cs @@ -4,7 +4,7 @@ { public Task AddItem(T? item) where T : class; public Task DeleteItem(int id) where T : class; - public Task UpdateItem(int? id, TDto? newItem) where T : class where TDto : class; + public Task> GetItems(int index, int count, string? orderingPropertyName = null, object? valueProperty = null) where T : class; } }