From de66affddc0ab225b8a5489d43ff8195df1f661a Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Sat, 24 Feb 2024 19:07:35 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20d'UpdateItem=20g=C3=A9n=C3=A9rique,=20l?= =?UTF-8?q?a=20mise=20=C3=A0=20jour=20se=20fait=20en=20base=20de=20donn?= =?UTF-8?q?=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; } }