mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 17:43:12 +00:00
165 lines
5.7 KiB
C#
165 lines
5.7 KiB
C#
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<ApplicationUser> _userManager;
|
|
private readonly IAssetService _assetService;
|
|
|
|
public AssetController(UserManager<ApplicationUser> userManager, IAssetService assetService)
|
|
{
|
|
_userManager = userManager;
|
|
_assetService = assetService;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("AddAsset")]
|
|
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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);
|
|
}
|
|
}
|
|
}
|
|
|