2025-06-30 16:15:08 -03:00
|
|
|
|
using Core.Interfaces;
|
2025-07-14 16:16:05 -03:00
|
|
|
|
using Domain.Dtos.Stock;
|
2025-06-30 16:15:08 -03:00
|
|
|
|
using Domain.Entities;
|
|
|
|
|
|
using Domain.Generics;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace API.Controllers.Stock
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
public class LSProductController : ControllerBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ILSProductDom _service;
|
|
|
|
|
|
|
|
|
|
|
|
public LSProductController(ILSProductDom service)
|
|
|
|
|
|
{
|
|
|
|
|
|
_service = service;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("search")]
|
|
|
|
|
|
public async Task<ActionResult<PagedResult<ELSProduct>>> Search([FromBody] LSProductSearchParams searchParams)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await _service.SearchAsync(searchParams);
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
|
public async Task<ActionResult<ELSProduct>> GetById(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var product = await _service.GetByIdAsync(id);
|
|
|
|
|
|
if (product == null)
|
|
|
|
|
|
return NotFound($"No se encontró el producto con ID {id}.");
|
|
|
|
|
|
return Ok(product);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("create")]
|
|
|
|
|
|
public async Task<ActionResult<ELSProduct>> Create([FromBody] ELSProduct model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var created = await _service.CreateAsync(model);
|
|
|
|
|
|
return CreatedAtAction(nameof(GetById), new { id = created.Id }, created);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("update")]
|
|
|
|
|
|
public async Task<IActionResult> Update([FromBody] ELSProduct model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var success = await _service.UpdateAsync(model);
|
|
|
|
|
|
if (!success)
|
|
|
|
|
|
return NotFound("No se pudo actualizar el producto.");
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
|
public async Task<IActionResult> Delete(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var success = await _service.DeleteAsync(id);
|
|
|
|
|
|
if (!success)
|
|
|
|
|
|
return NotFound("No se pudo eliminar el producto.");
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("exportfiltered")]
|
|
|
|
|
|
public async Task<IActionResult> ExportFiltered([FromBody] LSProductSearchParams searchParams)
|
|
|
|
|
|
{
|
|
|
|
|
|
var content = await _service.ExportToExcelAsync(searchParams);
|
|
|
|
|
|
var fileName = $"productos_{DateTime.Now:yyyyMMddHHmm}.xlsx";
|
|
|
|
|
|
return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
|
|
|
|
|
|
}
|
2025-07-05 13:26:22 -03:00
|
|
|
|
|
|
|
|
|
|
[HttpGet("download-template")]
|
|
|
|
|
|
public IActionResult DownloadImportTemplate()
|
|
|
|
|
|
{
|
|
|
|
|
|
var file = _service.GetImportTemplate();
|
|
|
|
|
|
return File(file,
|
|
|
|
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
|
|
|
|
"plantilla_productos.xlsx");
|
|
|
|
|
|
}
|
2025-07-14 16:16:05 -03:00
|
|
|
|
[HttpPost("preview-import")]
|
|
|
|
|
|
public ActionResult<List<ProductImportPreviewDto>> PreviewImport([FromForm] ProductImportFormDto dto)
|
|
|
|
|
|
{
|
|
|
|
|
|
using var ms = new MemoryStream();
|
|
|
|
|
|
dto.File.CopyTo(ms);
|
|
|
|
|
|
var fileBytes = ms.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
var preview = _service.PreviewImportFromExcel(fileBytes);
|
|
|
|
|
|
return Ok(preview);
|
|
|
|
|
|
}
|
|
|
|
|
|
public class ProductImportFormDto
|
|
|
|
|
|
{
|
|
|
|
|
|
public IFormFile File { get; set; } = default!;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Importa productos desde la vista previa. Espera una lista de ProductImportPreviewDto SIN errores.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost("import")]
|
|
|
|
|
|
public async Task<ActionResult<ProductImportResultDto>> ImportProducts([FromBody] List<ProductImportPreviewDto> items)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (items == null || !items.Any())
|
|
|
|
|
|
return BadRequest("No se enviaron productos para importar.");
|
|
|
|
|
|
|
|
|
|
|
|
// Opcional: validación de errores
|
|
|
|
|
|
if (items.Any(x => x.HasError))
|
|
|
|
|
|
return BadRequest("Hay productos con errores. Corríjalos antes de importar.");
|
|
|
|
|
|
|
|
|
|
|
|
var result = await _service.ImportProductsAsync(items);
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-30 16:15:08 -03:00
|
|
|
|
}
|
|
|
|
|
|
}
|