Gremlin/Gremlin_BlazorServer/Services/GenericController.cs

407 lines
8.3 KiB
C#

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