shoc-backend/Api.SeaHavenIndustries/Controllers/AssetController.cs

166 lines
5.7 KiB
C#
Raw Normal View History

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