mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 04:53:11 +00:00
160 lines
5 KiB
C#
160 lines
5 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
|
|
namespace SeaHaven.DataServices.Implementation
|
|
{
|
|
public class ContactDataService : IContactDataService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public ContactDataService(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<Contacts?> GetByIdAsync(int id)
|
|
{
|
|
return await _context.Contacts.FindAsync(id);
|
|
}
|
|
|
|
public async Task<Contacts?> GetByIdWithDetailsAsync(int id)
|
|
{
|
|
return await _context.Contacts
|
|
.Include(c => c.Location)
|
|
.Include(c => c.Account)
|
|
.FirstOrDefaultAsync(c => c.Id == id);
|
|
}
|
|
|
|
public async Task<IEnumerable<Contacts>> GetAllAsync()
|
|
{
|
|
return await _context.Contacts.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Contacts>> GetByLocationIdAsync(int locationId)
|
|
{
|
|
return await _context.Contacts
|
|
.Where(c => c.LocationId == locationId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Contacts>> GetByAccountIdAsync(int accountId)
|
|
{
|
|
return await _context.Contacts
|
|
.Where(c => c.AccountId == accountId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Contacts>> GetByTypeAsync(string type)
|
|
{
|
|
return await _context.Contacts
|
|
.Where(c => c.ContactType == type)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<Contacts?> GetByEmailAsync(string email)
|
|
{
|
|
return await _context.Contacts
|
|
.FirstOrDefaultAsync(c => c.Email == email);
|
|
}
|
|
|
|
public async Task<Contacts?> GetByPhoneAsync(string phone)
|
|
{
|
|
return await _context.Contacts
|
|
.FirstOrDefaultAsync(c => c.PhoneNumber == phone);
|
|
}
|
|
|
|
public async Task<(IEnumerable<Contacts> Items, int TotalCount)> GetPagedAsync(
|
|
int page,
|
|
int pageSize,
|
|
string? search = null,
|
|
string? type = null)
|
|
{
|
|
search ??= "";
|
|
var query = _context.Contacts.AsQueryable();
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
var s = search.ToLower();
|
|
query = query.Where(c =>
|
|
(c.FirstName != null && c.FirstName.ToLower().Contains(s)) ||
|
|
(c.LastName != null && c.LastName.ToLower().Contains(s)) ||
|
|
(c.Email != null && c.Email.ToLower().Contains(s)) ||
|
|
(c.PhoneNumber != null && c.PhoneNumber.Contains(s)));
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(type))
|
|
{
|
|
query = query.Where(c => c.ContactType == type);
|
|
}
|
|
|
|
var totalCount = await query.CountAsync();
|
|
var items = await query
|
|
.OrderByDescending(c => c.Id)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
return (items, totalCount);
|
|
}
|
|
|
|
public async Task<Contacts> AddAsync(Contacts contact)
|
|
{
|
|
contact.CreatedDate = DateTime.UtcNow;
|
|
await _context.Contacts.AddAsync(contact);
|
|
await _context.SaveChangesAsync();
|
|
return contact;
|
|
}
|
|
|
|
public async Task UpdateAsync(Contacts contact)
|
|
{
|
|
contact.LastModificationTime = DateTime.UtcNow;
|
|
_context.Contacts.Update(contact);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteAsync(int id)
|
|
{
|
|
var entity = await GetByIdAsync(id);
|
|
if (entity != null)
|
|
{
|
|
_context.Contacts.Remove(entity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(int id)
|
|
{
|
|
return await _context.Contacts.AnyAsync(c => c.Id == id);
|
|
}
|
|
|
|
public async Task<bool> EmailExistsAsync(string email, int? excludeId = null)
|
|
{
|
|
return await _context.Contacts.AnyAsync(c =>
|
|
c.Email == email &&
|
|
(excludeId == null || c.Id != excludeId));
|
|
}
|
|
|
|
public async Task<bool> PhoneExistsAsync(string phone, int? excludeId = null)
|
|
{
|
|
return await _context.Contacts.AnyAsync(c =>
|
|
c.PhoneNumber == phone &&
|
|
(excludeId == null || c.Id != excludeId));
|
|
}
|
|
|
|
public async Task<int> CountAsync()
|
|
{
|
|
return await _context.Contacts.CountAsync();
|
|
}
|
|
|
|
public async Task<int> CountByAccountIdAsync(int accountId)
|
|
{
|
|
return await _context.Contacts.CountAsync(c => c.AccountId == accountId);
|
|
}
|
|
|
|
public async Task<int> CountByTypeAsync(string type)
|
|
{
|
|
return await _context.Contacts.CountAsync(c => c.ContactType == type);
|
|
}
|
|
}
|
|
}
|