|
|
|
@ -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<T?> CreateItemAsync<T>(this DbContext context, T? item) where T : class
|
|
|
|
|
{
|
|
|
|
|
if (item == null || context.Set<T>().Contains(item))
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult<T?>(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Set<T>().Add(item);
|
|
|
|
|
context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return Task.FromResult<T?>(item);
|
|
|
|
|
}
|
|
|
|
|
public static Task<bool> DeleteItemAsync<T>(this DbContext context, int? id) where T : class
|
|
|
|
|
{
|
|
|
|
|
if (id == null)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var entity = context.Set<T>().Find(id);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Set<T>().Remove(entity);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
public static Task<T?> UpdateItemAsync<T, TDto>(this DbContext context, int? id, TDto dto)
|
|
|
|
|
where T : class
|
|
|
|
|
where TDto : class
|
|
|
|
|
{
|
|
|
|
|
var entity = context.Set<T>().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<T?>(entity);
|
|
|
|
|
}
|
|
|
|
|
public static Task<IEnumerable<T?>> GetItemsWithFilter<T>(this DbContext context,
|
|
|
|
|
int page, int count, object orderCriteria, string? filter = null, object? valueProperty = null) where T : class
|
|
|
|
|
{
|
|
|
|
|
IQueryable<T> query = context.Set<T>().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<Func<T, bool>>(equality, parameter);
|
|
|
|
|
var filteredEntity = context.Set<T>().FirstOrDefault(lambda);
|
|
|
|
|
if (filteredEntity != null)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult<IEnumerable<T?>>(new List<T?> { filteredEntity });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Task.FromResult<IEnumerable<T?>>(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<Func<T, object>>(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<T>(orderedQuery);
|
|
|
|
|
}
|
|
|
|
|
//var orderingItem = query.OrderBy(s => s.Equals(orderCriteria)).ToList();
|
|
|
|
|
var orderingItem = query.ToList();
|
|
|
|
|
return Task.FromResult<IEnumerable<T?>>(orderingItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|