|
|
|
@ -28,21 +28,52 @@ public static class Extensions
|
|
|
|
|
return await Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static async Task<T?> UpdateItem<T>(this IList<T> collection, T? oldItem, T? newItem) where T : class
|
|
|
|
|
{
|
|
|
|
|
// if(oldItem == null || newItem == null) return default(T);
|
|
|
|
|
|
|
|
|
|
// if(!collection.Contains(oldItem))
|
|
|
|
|
// ! 1
|
|
|
|
|
// internal static async Task<T?> UpdateItem<T>(this HeartTrackContext context, int? id, T? newItem) where T : class
|
|
|
|
|
// {
|
|
|
|
|
// return default(T);
|
|
|
|
|
// var existingT = await context.Set<T>().FindAsync(id);
|
|
|
|
|
// if (existingT != null)
|
|
|
|
|
// {
|
|
|
|
|
// context.Entry(existingT).CurrentValues.SetValues(newItem!);
|
|
|
|
|
// context.Update(existingT);
|
|
|
|
|
// // existingT = func(newItem!);
|
|
|
|
|
// await context.SaveChangesAsync();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return existingT;
|
|
|
|
|
// // if(oldItem == null || newItem == null) return default(T);
|
|
|
|
|
|
|
|
|
|
// // if(!collection.Contains(oldItem))
|
|
|
|
|
// // {
|
|
|
|
|
// // return default(T);
|
|
|
|
|
// // }
|
|
|
|
|
|
|
|
|
|
// // collection.Remove(oldItem!);
|
|
|
|
|
// // await collection.AddItem(newItem!);
|
|
|
|
|
// // return newItem;
|
|
|
|
|
// // return await Task.FromResult<T?>(default(T));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// collection.Remove(oldItem!);
|
|
|
|
|
// await collection.AddItem(newItem!);
|
|
|
|
|
// return newItem;
|
|
|
|
|
return await Task.FromResult<T?>(default(T));
|
|
|
|
|
// ! 2
|
|
|
|
|
public static async Task<U?> UpdateItem<T, U>(this HeartTrackContext context, int? id, T? newItem, Action<T, U> updateAction) where T : class where U: class
|
|
|
|
|
{
|
|
|
|
|
var existingT = await context.Set<U>().FindAsync(id);
|
|
|
|
|
if (existingT != null && newItem != null)
|
|
|
|
|
{
|
|
|
|
|
// Appliquer les mises à jour sur l'objet existant en utilisant l'action passée en paramètre
|
|
|
|
|
updateAction(newItem, existingT);
|
|
|
|
|
|
|
|
|
|
// Marquer l'objet comme modifié dans le contexte
|
|
|
|
|
context.Update(existingT);
|
|
|
|
|
|
|
|
|
|
// Enregistrer les modifications dans la base de données
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return existingT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<T> GetItemsWithFilterAndOrdering<T>(this IEnumerable<T> list, Func<T, bool> filter, int index, int count, Enum? orderCriterium, bool descending = false ) where T : class
|
|
|
|
|
{
|
|
|
|
|
var filteredList = list.Where(filter);
|
|
|
|
|