using Api.SeaHavenIndustries.DTOs; using Data.SeaHavenIndustries; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace Api.SeaHavenIndustries.Controllers { //[Authorize] // Removed for testing [ApiController] [Route("api/Asset")] public class AssetController : Controller { private readonly UserManager _userManager; private readonly ApplicationDbContext _db; public AssetController(UserManager userManager, ApplicationDbContext db) { _userManager = userManager; _db = db; } [HttpPost] [Route("AddAsset")] public async Task AddAsset(Asset_DTO asset) { try { Assets model = new Assets(); model.AssetCode = asset.AssetCode; model.Name = asset.Name; model.Description = asset.Description; model.AccountId = asset.AccountId; model.LocationId = asset.LocationId; model.IsActive = asset.IsActive; _db.Assets.Add(model); await _db.SaveChangesAsync(); return Ok(new DataResponse { Message = "Asset Created Successfully", Status = "200" }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = ex.Message }); } } [HttpPost] [Route("EditAsset")] public async Task EditAsset(EditAsset_DTO asset) { try { var exist = _db.Assets.Where(w => w.Id == asset.Id).FirstOrDefault(); if (exist == null) { return BadRequest(new Response { Status = "Error", Message = "Asset not found!" }); } exist.AssetCode = asset.AssetCode; exist.Name = asset.Name; exist.Description = asset.Description; exist.AccountId = asset.AccountId; exist.LocationId = asset.LocationId; exist.IsActive = asset.IsActive; _db.Assets.Update(exist); await _db.SaveChangesAsync(); return Ok(new DataResponse { Message = "Asset Updated Successfully", Status = "200" }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = ex.Message }); } } [HttpGet] [Route("DeleteAsset")] public async Task DeleteAsset(int id) { try { var exist = _db.Assets.Where(w => w.Id == id).FirstOrDefault(); if (exist != null) { _db.Remove(exist); } else { return BadRequest(new Response { Status = "Error", Message = "Asset not found!" }); } await _db.SaveChangesAsync(); return Ok(new DataResponse { Message = "Asset Deleted Successfully", Status = "200" }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = ex.Message }); } } [HttpGet] [Route("GetAssetById")] public async Task GetAssetById(int id) { try { var asset = await _db.Assets .Include(a => a.Account) .Include(a => a.Location) .Where(w => w.Id == id) .FirstOrDefaultAsync(); if (asset == null) { return NotFound(new Response { Status = "Error", Message = "Asset not found" }); } return Ok(asset); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = ex.Message }); } } [HttpGet] [Route("GetAssetList")] public IActionResult GetAssetList(string? search = "", int page = 1, int pageSize = 10) { // Build the query with search filter var query = _db.Assets .Include(a => a.Account) .Include(a => a.Location) .Where(s => string.IsNullOrEmpty(search) || (s.Name != null && s.Name.ToLower().Contains(search.ToLower())) || (s.AssetCode != null && s.AssetCode.ToLower().Contains(search.ToLower())) || (s.Description != null && s.Description.ToLower().Contains(search.ToLower())) ); // Get total count AFTER applying search filter var totalCount = query.Count(); // Get paginated data var data = query .Select(n => new { n.Id, n.AssetCode, n.Name, n.Description, n.IsActive, AccountName = n.Account != null ? n.Account.Name : "", LocationName = n.Location != null ? n.Location.Name : "" }) .OrderByDescending(d => d.Id) .Skip((page - 1) * pageSize) .Take(pageSize) .ToList(); var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); var viewModel = new Pagination_DTO { Data = data, PageNumber = page, PageSize = pageSize, TotalCount = totalCount, TotalPages = totalPages }; return Ok(viewModel); } } }