Gremlin/Gremlin_BlazorServer/Services/ControllerService.cs

137 lines
2.7 KiB
C#

using Gremlin_BlazorServer.Data.DBClasses;
using Gremlin_BlazorServer.Data.EntityClasses;
using Microsoft.EntityFrameworkCore;
namespace Gremlin_BlazorServer.Services
{
public class ControllerService<T> where T : class
{
private readonly GremlinDb gremlinDb;
public ControllerService(GremlinDb gdb) => gremlinDb = gdb;
public async Task<List<T>> GetAllAsync()
{
return await gremlinDb.Set<T>().ToListAsync();
}
public async Task InsertAccountAsync(T entity)
{
try
{
await gremlinDb.Set<T>().AddAsync(entity);
await gremlinDb.SaveChangesAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public async Task<T> GetByIdAsync(uint id)
{
try
{
return await gremlinDb.Set<T>().FirstAsync(t => t.Equals(id));
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public async Task UpdateAccountAsync(T entity)
{
try
{
gremlinDb.Set<T>().Update(entity);
await gremlinDb.SaveChangesAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public async Task DeleteAccountAsync(T entity)
{
try
{
gremlinDb.Set<T>().Remove(entity);
await gremlinDb.SaveChangesAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public async Task<List<Product>> GetAllProductsAsync(ProductLine productLine)
{
try
{
return await gremlinDb.Products.Where(p => p.ProductLine.Equals(productLine)).ToListAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public async Task<List<Account>> GetAllAccountsAsync(AccountType accountType)
{
try
{
return await gremlinDb.Accounts.Where(a => a.AccountType.Equals(accountType)).ToListAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public async Task<List<Contact>> GetAllContactsAsync(Account account)
{
try
{
return await gremlinDb.Contacts.Where(a => a.Account.Equals(account)).ToListAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
// public async Task<List<T>> GetAllAsync(Func<object, T> searcher)
// {
// try
// {
// List<T> results = await gremlinDb.Set<T>().ToListAsync();
//
// // return results.Where(s => s.TSearch.Equals(search)).ToList();
// }
// catch (Exception e)
// {
// Console.WriteLine(e);
// throw;
// }
// }
//
// public void Search(ISearcher<Account,AccountType> searcher, T search)
// {
// List<T> results = searcher.Search(search);
// }
//
// public interface ISearcher<out TResult, in TSearch>
// {
// TResult Search(TSearch search);
// }
}
}