61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using Gremlin_BlazorServer.Data.DBClasses;
|
|
using Gremlin_BlazorServer.Data.EntityClasses;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Diagnostics;
|
|
|
|
namespace Gremlin_BlazorServer.Services
|
|
{
|
|
public class ContactService
|
|
{
|
|
private static GremlinDb gremlinDb = new();
|
|
private readonly AccountService accountService = new(gremlinDb);
|
|
private readonly AccountTypeService accountTypeService = new(gremlinDb);
|
|
|
|
public ContactService(GremlinDb gC) => gremlinDb = gC;
|
|
|
|
public void ConfigureServices(IServiceCollection services) => services.AddDbContext<GremlinDb>(ServiceLifetime.Scoped);
|
|
|
|
public async Task<List<Contact>> GetAllContactsAsync() => await gremlinDb.Contacts.Include(c => c.Account).ToListAsync();
|
|
|
|
public async Task<bool> InsertContactAsync(Contact contact)
|
|
{
|
|
contact.Account = await accountService.GetAccountAsync(contact.AccountId);
|
|
contact.Account.AccountType = await accountTypeService.GetAccountTypeAsync("FPC") ?? new();
|
|
try
|
|
{
|
|
await gremlinDb.Contacts.AddAsync(contact);
|
|
await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Debug.WriteLine(exception.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<Contact> GetContactAsync(uint contactId)
|
|
{
|
|
Contact? contact = await gremlinDb.Contacts.FirstOrDefaultAsync(c => c.ContactId.Equals(contactId));
|
|
return contact ?? new Contact();
|
|
}
|
|
|
|
public async Task<Contact> GetContactAsync(string lastName)
|
|
{
|
|
Contact? contact = await gremlinDb.Contacts.FirstOrDefaultAsync(c => c.LastName.Equals(lastName));
|
|
return contact ?? new Contact();
|
|
}
|
|
|
|
public async Task UpdateContactAsync(Contact contact)
|
|
{
|
|
gremlinDb.Contacts.Update(contact);
|
|
await gremlinDb.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteContactAsync(Contact contact)
|
|
{
|
|
gremlinDb.Remove(contact);
|
|
await gremlinDb.SaveChangesAsync();
|
|
}
|
|
}
|
|
} |