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 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 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> 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 Create([FromBody] EProductCategory category) { try { if (category == null) return BadRequest("La categoría de producto no puede ser nula."); var result = await _productCategoryService.AddAsync(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 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 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}"); } } } }