From e476fd6c826f68c15669a3bcc4f392b651ad4f29 Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Sun, 25 Feb 2024 11:23:20 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20d'un=20option=20dans=20UserDbContext=20?= =?UTF-8?q?pour=20avoir=20plus=20de=20d=C3=A9taille=20sur=20les=20logs.=20?= =?UTF-8?q?Ajout=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; } }