using Api.SeaHavenIndustries.DTOs; using Data.SeaHavenIndustries; using FluentValidation; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using SeaHaven.Services.Interfaces; using SeaHaven.Services.DTOs; using System.Security.Claims; namespace Api.SeaHavenIndustries.Controllers { //[Authorize] // Removed for testing [ApiController] [Route("api/Asset")] public class AssetController : Controller { private readonly UserManager _userManager; private readonly IAssetService _assetService; public AssetController(UserManager userManager, IAssetService assetService) { _userManager = userManager; _assetService = assetService; } [HttpPost] [Route("AddAsset")] public async Task AddAsset(Asset_DTO asset) { try { var createDto = new CreateAssetDTO { Name = asset.Name ?? throw new ArgumentException("Name is required"), LocationId = asset.LocationId }; var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (userId == null) return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" }); await _assetService.CreateAssetAsync(createDto, userId); return Ok(new DataResponse { Message = "Asset Created Successfully", Status = "200" }); } catch (ValidationException vex) { var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage)); return BadRequest(new Response { Status = "Validation Error", Message = errors }); } catch (Exception ex) { return StatusCode(500, new Response { Status = "Error", Message = ex.Message }); } } [HttpPost] [Route("EditAsset")] public async Task EditAsset(EditAsset_DTO asset) { try { var updateDto = new UpdateAssetDTO { Name = asset.Name, LocationId = asset.LocationId }; var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (userId == null) return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" }); await _assetService.UpdateAssetAsync(asset.Id, updateDto, userId); return Ok(new DataResponse { Message = "Asset Updated Successfully", Status = "200" }); } catch (ValidationException vex) { var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage)); return BadRequest(new Response { Status = "Validation Error", Message = errors }); } catch (KeyNotFoundException) { return NotFound(new Response { Status = "Error", Message = "Asset not found" }); } catch (Exception ex) { return StatusCode(500, new Response { Status = "Error", Message = ex.Message }); } } [HttpGet] [Route("DeleteAsset")] public async Task DeleteAsset(int id) { try { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (userId == null) return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" }); await _assetService.DeleteAssetAsync(id, userId!); return Ok(new DataResponse { Message = "Asset Deleted Successfully", Status = "200" }); } catch (KeyNotFoundException) { return NotFound(new Response { Status = "Error", Message = "Asset not found" }); } catch (Exception ex) { return StatusCode(500, new Response { Status = "Error", Message = ex.Message }); } } [HttpGet] [Route("GetAssetById")] public async Task GetAssetById(int id) { try { var asset = await _assetService.GetAssetByIdWithDetailsAsync(id); 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 async Task GetAssetList(string? search = "", int page = 1, int pageSize = 10) { var pagedResult = await _assetService.GetAssetsPagedAsync(page, pageSize, search); var data = pagedResult.Items.Select(a => new { a.Id, a.Name, LocationName = a.LocationName ?? "", a.CreatedDate }); var viewModel = new Pagination_DTO { Data = data, PageNumber = page, PageSize = pageSize, TotalCount = pagedResult.TotalCount, TotalPages = (int)Math.Ceiling(pagedResult.TotalCount / (double)pageSize) }; return Ok(viewModel); } } }