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 GetByIdAsync(int id) { return await _context.Locations.FindAsync(id); } public async Task GetByIdWithDetailsAsync(int id) { return await _context.Locations .FirstOrDefaultAsync(l => l.Id == id); } public async Task> GetAllAsync() { return await _context.Locations.ToListAsync(); } public async Task> GetByAccountIdAsync(int accountId) { // AccountId doesn't exist in database - return all for now // TODO: Add AccountId column to database if needed return await _context.Locations.ToListAsync(); } public async Task<(IEnumerable Items, int TotalCount)> GetPagedAsync( int page, int pageSize, string? search = null) { var query = _context.Locations.AsQueryable(); if (!string.IsNullOrWhiteSpace(search)) { query = query.Where(l => l.Name!.Contains(search) || 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); } public async Task 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 ExistsAsync(int id) { return await _context.Locations.AnyAsync(l => l.Id == id); } public async Task CountAsync() { return await _context.Locations.CountAsync(); } public async Task<(IEnumerable 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); } } }