using Gremlin_BlazorServer.Data.DBClasses; using Gremlin_BlazorServer.Data.EntityClasses; using Microsoft.EntityFrameworkCore; namespace Gremlin_BlazorServer.Services; public class GenericController { private readonly GremlinDb gremlinDb = new(); public IList? GetAll() where TResult : class, IMetadata { try { return gremlinDb.Set().ToList(); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public IList? GetAll(string include) where TResult : class, IMetadata { try { return gremlinDb.Set().Include(include).ToList(); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public IList? GetAll(Predicate search) where TResult : class, IMetadata { try { return gremlinDb.Set().AsEnumerable().Where(t => search(t)).ToList(); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public IList? GetAll(Predicate search, string include) where TResult : class, IMetadata { try { return gremlinDb.Set().Include(include).AsEnumerable().Where(t => search(t)).ToList(); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public TResult? Get(Predicate search) where TResult : class, IMetadata { try { return gremlinDb.Set().AsEnumerable().FirstOrDefault(t => search(t)); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public TResult? Get(Predicate search, string include) where TResult : class, IMetadata { try { return gremlinDb.Set().Include(include).AsEnumerable().FirstOrDefault(t => search(t)); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public TResult? GetLast() where TResult : class, IMetadata { try { return gremlinDb.Set().AsEnumerable().Last(); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public int Insert(T entity) where T : class, IMetadata { try { _ = gremlinDb.Set().Add(entity); return gremlinDb.SaveChanges(); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public int Insert(List entities) where T : class, IMetadata { try { gremlinDb.Set().AddRange(entities); return gremlinDb.SaveChanges(); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public bool IsExisting(Predicate search) where T : class, IMetadata { try { return gremlinDb.Set().AsEnumerable().Any(t => search(t)); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return false; } } public int Update(T entity) where T : class, IMetadata { try { _ = gremlinDb.Set().Update(entity); return gremlinDb.SaveChanges(); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public int Update(List entities) where T : class, IMetadata { try { gremlinDb.Set().UpdateRange(entities); return gremlinDb.SaveChanges(); } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } }