using Gremlin_BlazorServer.Data.DBClasses; using Gremlin_BlazorServer.Data.EntityClasses; using Microsoft.EntityFrameworkCore; namespace Gremlin_BlazorServer.Services; public class GenericController { public IList? GetAll() where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().ToList(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public IList? GetAll(string include) where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { 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 { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().AsEnumerable().Where(t => search(t)).ToList(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task?> GetAllAsync() where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return await gremlinDb.Set().ToListAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task?> GetAllAsync(string include) where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return await gremlinDb.Set().Include(include).ToListAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task?> GetAllAsync(string include1, string include2) where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return await gremlinDb.Set().Include(include1).Include(include2).ToListAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task?> GetAllAsync(Predicate search) where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return await Task.Run(() => 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 { using (GremlinDb gremlinDb = new()) { 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 { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().AsEnumerable().FirstOrDefault(t => search(t)); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task GetAsync(Predicate search) where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return await Task.Run(() => 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 { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().Include(include).AsEnumerable().FirstOrDefault(t => search(t)); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task GetAsync(Predicate search, string include1) where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return await Task.Run(() => gremlinDb.Set().Include(include1).AsEnumerable().FirstOrDefault(t => search(t))); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task GetAsync(Predicate search, string include1, string include2) where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return await Task.Run(() => gremlinDb.Set().Include(include1).Include(include2).AsEnumerable().FirstOrDefault(t => search(t))); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public TResult? GetLast() where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().AsEnumerable().Last(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public int Insert(T entity) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { 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 { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().AddRange(entities); return gremlinDb.SaveChanges(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public async Task InsertAsync(T entity) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().Add(entity); return await gremlinDb.SaveChangesAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public async Task InsertAsync(List entities) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().AddRange(entities); return await gremlinDb.SaveChangesAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public bool IsExisting(Predicate search) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { 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 { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().Update(entity); return gremlinDb.SaveChanges(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public async Task UpdateAsync(T entity) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().Update(entity); return await gremlinDb.SaveChangesAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public int Update(List entities) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().UpdateRange(entities); return gremlinDb.SaveChanges(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public async Task UpdateAsync(List entities) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().UpdateRange(entities); return await gremlinDb.SaveChangesAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public async Task RemoveAsync(T entity) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().Remove(entity); return await gremlinDb.SaveChangesAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } }