You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
4.0 KiB
107 lines
4.0 KiB
using Microsoft.AspNetCore.Identity;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Entities;
|
|
using Entities.SQLudeoDB;
|
|
using Model.Business;
|
|
using System.Diagnostics;
|
|
using DbContextLib;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Linq.Expressions;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
|
|
namespace ModelToEntity
|
|
{
|
|
internal static class Extension
|
|
{
|
|
internal 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);
|
|
}
|
|
internal 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);
|
|
}
|
|
internal 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;
|
|
context.SaveChanges();
|
|
|
|
return Task.FromResult<T?>(entity);
|
|
}
|
|
internal static Task<IEnumerable<T?>> GetItemsWithFilter<T>(this DbContext context,
|
|
int index, int count, string? orderingPropertyName = null, object? valueProperty = null) where T : class
|
|
{
|
|
IQueryable<T> query = context.Set<T>();
|
|
|
|
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<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);
|
|
}
|
|
var items = query.Skip(index).Take(count).ToList();
|
|
return Task.FromResult<IEnumerable<T?>>(items);
|
|
}
|
|
}
|
|
} |