Gremlin/Gremlin_BlazorServer/Services/AccountService.cs

71 lines
1.5 KiB
C#

using Gremlin_BlazorServer.Data.DBClasses;
using Gremlin_BlazorServer.Data.EntityClasses;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
namespace Gremlin_BlazorServer.Services
{
public class AccountService
{
private readonly GremlinDb gremlinDb;
public AccountService(GremlinDb gC)
{
gremlinDb = gC;
}
public void ConfigureServices(IServiceCollection services)
{
_ = services.AddDbContext<GremlinDb>(ServiceLifetime.Scoped);
}
public async Task<List<Account>> GetAllAccountsAsync()
{
return await gremlinDb.Accounts.ToListAsync();
}
public async Task<bool> InsertAccountAsync(Account account)
{
try
{
_ = await gremlinDb.Accounts.AddAsync(account);
_ = await gremlinDb.SaveChangesAsync();
return true;
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
return false;
}
}
public async Task<Account> GetAccountAsync(uint accountId)
{
Account account = new();
if (accountId != 0)
{
try
{
account = await gremlinDb.Accounts.FirstAsync(a => a.AccountId.Equals(accountId));
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
return account ?? new Account();
}
public async Task UpdateAccountAsync(Account account)
{
_ = gremlinDb.Accounts.Update(account);
_ = await gremlinDb.SaveChangesAsync();
}
public async Task DeleteAccountAsync(Account account)
{
_ = gremlinDb.Remove(account);
_ = await gremlinDb.SaveChangesAsync();
}
}
}