diff --git a/API_SQLuedo/DbDataManager/Extensions.cs b/API_SQLuedo/DbDataManager/Extensions.cs deleted file mode 100644 index 5f01cbf..0000000 --- a/API_SQLuedo/DbDataManager/Extensions.cs +++ /dev/null @@ -1,96 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -using System.Threading.Tasks; - -namespace ModelToEntities -{ - internal static class Extensions - { - internal static Task CreateItemAsync(this DbContext context, T? item) where T : class - { - if (item == null || context.Set().Contains(item)) - { - return Task.FromResult(null); - } - - context.Set().Add(item); - context.SaveChangesAsync(); - - return Task.FromResult(item); - } - internal static Task DeleteItemAsync(this DbContext context, int? id) where T : class - { - if (id == null) - { - return Task.FromResult(false); - } - - var entity = context.Set().Find(id); - if (entity == null) - { - return Task.FromResult(false); - } - - context.Set().Remove(entity); - context.SaveChanges(); - - return Task.FromResult(true); - } - internal static Task UpdateItemAsync(this DbContext context, int? id, TDto dto) - where T : class - where TDto : class - { - var entity = context.Set().Find(id); - if (entity == null) - { - throw new ArgumentException("Impossible de trouver l'entité", nameof(id)); - } - var primaryKey = context.Model.FindEntityType(typeof(T)).FindPrimaryKey(); - var primaryKeyPropertyName = primaryKey.Properties.Select(x => x.Name).Single(); - - var propertiesToUpdate = typeof(TDto).GetProperties() - .Where(p => p.CanRead && p.CanWrite && p.Name != primaryKeyPropertyName); - - foreach (var property in propertiesToUpdate) - { - var value = property.GetValue(dto); - typeof(T).GetProperty(property.Name)?.SetValue(entity, value); - } - - context.Entry(entity).State = EntityState.Modified; // Permet d'indiquer en Db que l'entité a été modifiée. - context.SaveChanges(); - - return Task.FromResult(entity); - } - internal static Task> GetItemsWithFilter(this DbContext context, - int page, int count, object orderCriteria, string? filter = null, object? valueProperty = null) where T : class - { - IQueryable query = context.Set(); - - if (valueProperty != null && !string.IsNullOrEmpty(filter)) - { - var idProp = typeof(T).GetProperty(filter); - if (idProp != null) - { - var parameter = Expression.Parameter(typeof(T), "entity"); - var propertyAccess = Expression.Property(parameter, idProp); - 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 }); - } - } - return Task.FromResult>(null); - } - var items = query.Skip((page - 1) * count).Take(count).ToList().Select(u => u); - return Task.FromResult>(items); - } - } -} diff --git a/API_SQLuedo/DbDataManager/Service/UserDataService.cs b/API_SQLuedo/DbDataManager/Service/UserDataService.cs index d9d9ae6..6af4805 100644 --- a/API_SQLuedo/DbDataManager/Service/UserDataService.cs +++ b/API_SQLuedo/DbDataManager/Service/UserDataService.cs @@ -2,8 +2,8 @@ using Entities; using Microsoft.EntityFrameworkCore; using Model.OrderCriteria; -using ModelToEntities; using Shared; +using Shared.ModelToEntities; namespace DbDataManager.Service; diff --git a/API_SQLuedo/Shared/Extensions.cs b/API_SQLuedo/Shared/Extensions.cs new file mode 100644 index 0000000..d99011e --- /dev/null +++ b/API_SQLuedo/Shared/Extensions.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Shared +{ + using DbContextLib; + using Microsoft.EntityFrameworkCore; + using System; + using System.Collections.Generic; + using System.Linq; + using System.Linq.Expressions; + using System.Text; + using System.Threading.Tasks; + + namespace ModelToEntities + { + public static class Extensions + { + public static Task CreateItemAsync(this DbContext context, T? item) where T : class + { + if (item == null || context.Set().Contains(item)) + { + return Task.FromResult(null); + } + + context.Set().Add(item); + context.SaveChangesAsync(); + + return Task.FromResult(item); + } + public static Task DeleteItemAsync(this DbContext context, int? id) where T : class + { + if (id == null) + { + return Task.FromResult(false); + } + + var entity = context.Set().Find(id); + if (entity == null) + { + return Task.FromResult(false); + } + + context.Set().Remove(entity); + context.SaveChanges(); + + return Task.FromResult(true); + } + public static Task UpdateItemAsync(this DbContext context, int? id, TDto dto) + where T : class + where TDto : class + { + var entity = context.Set().Find(id); + if (entity == null) + { + throw new ArgumentException("Impossible de trouver l'entité", nameof(id)); + } + var primaryKey = context.Model.FindEntityType(typeof(T)).FindPrimaryKey(); + var primaryKeyPropertyName = primaryKey.Properties.Select(x => x.Name).Single(); + + var propertiesToUpdate = typeof(TDto).GetProperties() + .Where(p => p.CanRead && p.CanWrite && p.Name != primaryKeyPropertyName); + + foreach (var property in propertiesToUpdate) + { + var value = property.GetValue(dto); + typeof(T).GetProperty(property.Name)?.SetValue(entity, value); + } + + context.Entry(entity).State = EntityState.Modified; // Permet d'indiquer en Db que l'entité a été modifiée. + context.SaveChanges(); + + return Task.FromResult(entity); + } + public static Task> GetItemsWithFilter(this DbContext context, + int page, int count, object orderCriteria, string? filter = null, object? valueProperty = null) where T : class + { + IQueryable query = context.Set().Skip((page - 1) * count).Take(count); + + if (valueProperty != null && !string.IsNullOrEmpty(filter)) + { + var idProp = typeof(T).GetProperty(filter); + if (idProp != null) + { + var parameter = Expression.Parameter(typeof(T), "entity"); + var propertyAccess = Expression.Property(parameter, idProp); + 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 }); + } + } + return Task.FromResult>(null); + } + if (orderCriteria != null && orderCriteria.ToString() != "None") + { + // Ordonnancement + var propertyName = orderCriteria.ToString(); + if (propertyName.StartsWith("By")) + { + propertyName = propertyName.Substring(2); + } + var parameterExpression = Expression.Parameter(typeof(T), "entity"); + var propertyExpression = Expression.Property(parameterExpression, propertyName); + var lambdaExpression = Expression.Lambda>(Expression.Convert(propertyExpression, typeof(object)), parameterExpression); + + // Utilisation de la méthode OrderBy + var orderedQuery = Expression.Call( + typeof(Queryable), + "OrderBy", + new Type[] { typeof(T), typeof(object) }, + query.Expression, + lambdaExpression + ); + + query = query.Provider.CreateQuery(orderedQuery); + } + //var orderingItem = query.OrderBy(s => s.Equals(orderCriteria)).ToList(); + var orderingItem = query.ToList(); + return Task.FromResult>(orderingItem); + } + } + } + +} diff --git a/API_SQLuedo/Shared/UserDataService.cs b/API_SQLuedo/Shared/UserDataService.cs index 21702d1..a5a740a 100644 --- a/API_SQLuedo/Shared/UserDataService.cs +++ b/API_SQLuedo/Shared/UserDataService.cs @@ -9,6 +9,7 @@ using Entities; using Shared.Mapper; using DbContextLib; using Model.OrderCriteria; +using Shared.ModelToEntities; namespace Shared { @@ -133,9 +134,10 @@ namespace Shared throw new NotImplementedException(); } - public Task> GetItems(int page, int count, object orderCriteria, string? filter = null, object? valueFilter = null) + public async Task> GetItems(int page, int count, object orderCriteria, string? filter = null, object? valueFilter = null) { - throw new NotImplementedException(); + var usersEntities = await DbContext.GetItemsWithFilter(page, count, orderCriteria, filter, valueFilter); + return usersEntities.Select(e => e).ToList().Select(e => e.FromEntityToDTO()); } } }