Files
imprink/src/Imprink.WebApi/Controllers/Products/ProductsController.cs
2025-06-20 14:56:29 +03:00

53 lines
1.5 KiB
C#

using Imprink.Application.Products;
using Imprink.Application.Products.Commands;
using Imprink.Application.Products.Dtos;
using Imprink.Domain.Models;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Imprink.WebApi.Controllers;
[ApiController]
[Route("/api/products")]
public class ProductsController(IMediator mediator) : ControllerBase
{
[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<PagedResultDto<ProductDto>>> GetProducts(
[FromQuery] ProductFilterParameters filterParameters)
{
var result = await mediator.Send(new GetProductsQuery { FilterParameters = filterParameters});
return Ok(result);
}
[HttpPost]
[Authorize(Roles = "Admin")]
public async Task<ActionResult<PagedResultDto<ProductDto>>> CreateProduct(
[FromBody] CreateProductCommand command)
{
var result = await mediator.Send(command);
return Ok(result);
}
[HttpPut("{id:guid}")]
[Authorize(Roles = "Admin")]
public async Task<ActionResult<ProductDto>> UpdateProduct(
Guid id,
[FromBody] UpdateProductCommand command)
{
if (id != command.Id) return BadRequest("ID mismatch");
var result = await mediator.Send(command);
return Ok(result);
}
[HttpDelete("{id:guid}")]
[Authorize(Roles = "Admin")]
public async Task<ActionResult> DeleteProduct(Guid id)
{
await mediator.Send(new DeleteProductCommand { Id = id });
return NoContent();
}
}