All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 2m21s
134 lines
4.5 KiB
C#
134 lines
4.5 KiB
C#
using Core.Interfaces;
|
|
using Domain.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Reflection;
|
|
|
|
namespace phronCare.API.Controllers.Sales
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class ProductCategoryController : ControllerBase
|
|
{
|
|
private readonly IProductCategoryDom _productCategoryService;
|
|
|
|
public ProductCategoryController(IProductCategoryDom productCategoryService)
|
|
{
|
|
_productCategoryService = productCategoryService ?? throw new ArgumentNullException(nameof(productCategoryService));
|
|
}
|
|
|
|
[HttpGet("all")]
|
|
public async Task<IActionResult> GetAll()
|
|
{
|
|
try
|
|
{
|
|
var result = await _productCategoryService.GetAllAsync();
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpGet("search")]
|
|
public async Task<IActionResult> Search([FromQuery] string? term)
|
|
{
|
|
try
|
|
{
|
|
var result = await _productCategoryService.SearchAsync(term ?? string.Empty);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpGet("{id:int}")]
|
|
public async Task<ActionResult<EProductCategory>> GetById(int id)
|
|
{
|
|
try
|
|
{
|
|
var result = await _productCategoryService.GetByIdAsync(id);
|
|
if (result == null)
|
|
return NotFound($"No se encontró una categoría de producto con ID {id}.");
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, $"Error: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpPost("create")]
|
|
public async Task<IActionResult> Create([FromBody] EProductCategory category)
|
|
{
|
|
try
|
|
{
|
|
if (category == null)
|
|
return BadRequest("La categoría de producto no puede ser nula.");
|
|
|
|
var result = await _productCategoryService.CreateAsync(category);
|
|
return Ok(result);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return BadRequest($"Validación fallida: {ex.Message}");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return BadRequest($"Error de negocio: {ex.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpPut("update")]
|
|
public async Task<IActionResult> Update([FromBody] EProductCategory category)
|
|
{
|
|
try
|
|
{
|
|
if (category == null || category.Id <= 0)
|
|
return BadRequest("La categoría de producto es inválida o no tiene un ID válido.");
|
|
|
|
var success = await _productCategoryService.UpdateAsync(category);
|
|
|
|
if (!success)
|
|
return NotFound($"No se encontró una categoría de producto con ID {category.Id}.");
|
|
|
|
return Ok("Categoría de producto actualizada correctamente.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
[HttpDelete("{id:int}")]
|
|
public async Task<IActionResult> Delete(int id)
|
|
{
|
|
try
|
|
{
|
|
var success = await _productCategoryService.DeleteAsync(id);
|
|
|
|
if (!success)
|
|
return NotFound($"No se encontró una categoría de producto con ID {id}.");
|
|
|
|
return Ok("Categoría de producto eliminada correctamente.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
|
|
return StatusCode(500, $"{methodName} Message: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|