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; } }