using Data.SeaHavenIndustries; using Microsoft.EntityFrameworkCore; using SeaHaven.DataServices.Interfaces; namespace SeaHaven.DataServices.Implementation { public class AssetDataService : IAssetDataService { private readonly ApplicationDbContext _context; public AssetDataService(ApplicationDbContext context) { _context = context; } public async Task GetByIdAsync(int id) { return await _context.Assets.FindAsync(id); } public async Task GetByIdWithDetailsAsync(int id) { return await _context.Assets .FirstOrDefaultAsync(a => a.Id == id); } public async Task> GetAllAsync() { return await _context.Assets.ToListAsync(); } public async Task> GetByLocationIdAsync(int locationId) { return await _context.Assets .Where(a => a.LocationId == locationId) .ToListAsync(); } public async Task<(IEnumerable Items, int TotalCount)> GetPagedAsync( int page, int pageSize, string? search = null) { var query = _context.Assets.AsQueryable(); var totalCount = await query.CountAsync(); var items = await query .OrderBy(a => a.Id) .Skip((page - 1) * pageSize) .Take(pageSize) .ToListAsync(); return (items, totalCount); } public async Task AddAsync(Assets asset) { asset.CreatedDate = DateTime.Now; await _context.Assets.AddAsync(asset); await _context.SaveChangesAsync(); return asset; } public async Task UpdateAsync(Assets asset) { asset.LastModificationTime = DateTime.Now; _context.Assets.Update(asset); await _context.SaveChangesAsync(); } public async Task DeleteAsync(int id) { var entity = await GetByIdAsync(id); if (entity != null) { _context.Assets.Remove(entity); await _context.SaveChangesAsync(); } } public async Task ExistsAsync(int id) { return await _context.Assets.AnyAsync(a => a.Id == id); } public async Task CountAsync() { return await _context.Assets.CountAsync(); } } }