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(ServiceLifetime.Scoped); } public async Task> GetAllAccountsAsync() { return await gremlinDb.Accounts.ToListAsync(); } public async Task 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 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(); } } }