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