2026-04-28 18:55:14 -05:00
|
|
|
using Data.SeaHavenIndustries;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SeaHaven.DataServices.Interfaces;
|
|
|
|
|
|
|
|
|
|
namespace SeaHaven.DataServices.Implementation
|
|
|
|
|
{
|
|
|
|
|
public class LocationDataService : ILocationDataService
|
|
|
|
|
{
|
|
|
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
|
|
|
|
|
|
public LocationDataService(ApplicationDbContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Locations?> GetByIdAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
return await _context.Locations.FindAsync(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Locations?> GetByIdWithDetailsAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
return await _context.Locations
|
|
|
|
|
.FirstOrDefaultAsync(l => l.Id == id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<Locations>> GetAllAsync()
|
|
|
|
|
{
|
|
|
|
|
return await _context.Locations.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<Locations>> GetByAccountIdAsync(int accountId)
|
|
|
|
|
{
|
2026-08-26 09:12:48 -03:00
|
|
|
return await _context.Locations
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(l => l.AccountId == accountId)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<(bool Exists, int? AccountId)> GetAccountScopeAsync(
|
|
|
|
|
int locationId,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
var row = await _context.Locations
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(l => l.Id == locationId)
|
|
|
|
|
.Select(l => new { l.AccountId })
|
|
|
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
return row == null ? (false, null) : (true, row.AccountId);
|
2026-04-28 18:55:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<(IEnumerable<Locations> Items, int TotalCount)> GetPagedAsync(
|
2026-07-24 17:35:34 -03:00
|
|
|
int page,
|
|
|
|
|
int pageSize,
|
2026-04-28 18:55:14 -05:00
|
|
|
string? search = null)
|
|
|
|
|
{
|
|
|
|
|
var query = _context.Locations.AsQueryable();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
query = query.Where(l =>
|
|
|
|
|
l.Name!.Contains(search) ||
|
2026-04-28 18:55:14 -05:00
|
|
|
l.City!.Contains(search));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var totalCount = await query.CountAsync();
|
|
|
|
|
var items = await query
|
|
|
|
|
.OrderBy(l => l.Name)
|
|
|
|
|
.Skip((page - 1) * pageSize)
|
|
|
|
|
.Take(pageSize)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
return (items, totalCount);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 17:39:25 -03:00
|
|
|
public async Task<IReadOnlyList<SiteOptionRow>> GetSiteOptionsAsync(
|
|
|
|
|
string? search = null,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
2026-07-17 10:36:32 -03:00
|
|
|
{
|
2026-08-26 17:39:25 -03:00
|
|
|
var query = _context.Locations
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(l => l.AccountId != null);
|
2026-07-17 10:36:32 -03:00
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
|
|
|
{
|
|
|
|
|
var s = search.ToLower();
|
|
|
|
|
query = query.Where(l =>
|
|
|
|
|
(l.Name ?? "").ToLower().Contains(s) ||
|
|
|
|
|
(l.City ?? "").ToLower().Contains(s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await query
|
|
|
|
|
.OrderBy(l => l.Name)
|
|
|
|
|
.Select(l => new SiteOptionRow(l.Id, l.Name, l.City, l.State))
|
2026-08-26 17:39:25 -03:00
|
|
|
.ToListAsync(cancellationToken);
|
2026-07-17 10:36:32 -03:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 18:55:14 -05:00
|
|
|
public async Task<Locations> AddAsync(Locations location)
|
|
|
|
|
{
|
|
|
|
|
location.CreatedDate = DateTime.Now;
|
|
|
|
|
await _context.Locations.AddAsync(location);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
return location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task UpdateAsync(Locations location)
|
|
|
|
|
{
|
|
|
|
|
location.LastModificationTime = DateTime.Now;
|
|
|
|
|
_context.Locations.Update(location);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DeleteAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
var entity = await GetByIdAsync(id);
|
|
|
|
|
if (entity != null)
|
|
|
|
|
{
|
|
|
|
|
_context.Locations.Remove(entity);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> ExistsAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
return await _context.Locations.AnyAsync(l => l.Id == id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> CountAsync()
|
|
|
|
|
{
|
|
|
|
|
return await _context.Locations.CountAsync();
|
|
|
|
|
}
|
2026-07-24 17:35:34 -03:00
|
|
|
|
|
|
|
|
public async Task<(List<Locations> Items, int TotalCount)> GetListPagedAsync(
|
|
|
|
|
int page,
|
|
|
|
|
int pageSize,
|
|
|
|
|
string? search,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var query = _context.Locations.AsNoTracking();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(l =>
|
|
|
|
|
l.Name!.Contains(search) ||
|
|
|
|
|
l.City!.Contains(search));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var totalCount = await query.CountAsync(cancellationToken);
|
|
|
|
|
var items = await query
|
|
|
|
|
.OrderBy(l => l.Name)
|
|
|
|
|
.Skip((page - 1) * pageSize)
|
|
|
|
|
.Take(pageSize)
|
|
|
|
|
.ToListAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
return (items, totalCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Locations?> GetDetailByIdAsync(int id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return await _context.Locations
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.FirstOrDefaultAsync(l => l.Id == id, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Locations?> GetByIdForUpdateAsync(int id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return await _context.Locations.FindAsync(new object[] { id }, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Locations> AddAsync(Locations location, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
location.CreatedDate = DateTime.Now;
|
|
|
|
|
await _context.Locations.AddAsync(location, cancellationToken);
|
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
return location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task UpdateAsync(Locations location, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
location.LastModificationTime = DateTime.Now;
|
|
|
|
|
_context.Locations.Update(location);
|
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> DeleteByIdAsync(int id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var entity = await _context.Locations.FindAsync(new object[] { id }, cancellationToken);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
_context.Locations.Remove(entity);
|
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-05-06 10:49:33 -05:00
|
|
|
|
|
|
|
|
public async Task<(IEnumerable<object> Items, int TotalCount)> GetAddressbookPagedAsync(
|
|
|
|
|
int page,
|
|
|
|
|
int pageSize,
|
|
|
|
|
string? search = null)
|
|
|
|
|
{
|
|
|
|
|
search ??= "";
|
|
|
|
|
|
|
|
|
|
var query = _context.Locations.AsQueryable();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
|
|
|
{
|
|
|
|
|
var s = search.ToLower();
|
|
|
|
|
query = query.Where(w =>
|
|
|
|
|
(w.Name ?? "").ToLower().Contains(s) ||
|
|
|
|
|
(w.Title ?? "").ToLower().Contains(s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var totalCount = await query.CountAsync();
|
|
|
|
|
|
|
|
|
|
var data = await query
|
|
|
|
|
.Select(n => new
|
|
|
|
|
{
|
|
|
|
|
n.Id,
|
|
|
|
|
n.Name,
|
|
|
|
|
n.Title
|
|
|
|
|
})
|
|
|
|
|
.OrderByDescending(d => d.Id)
|
|
|
|
|
.Skip((page - 1) * pageSize)
|
|
|
|
|
.Take(pageSize)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
return (data, totalCount);
|
|
|
|
|
}
|
2026-04-28 18:55:14 -05:00
|
|
|
}
|
|
|
|
|
}
|