Add two basic CQRS commands/queries

This commit is contained in:
lumijiez
2025-05-05 00:01:30 +03:00
parent d9332cef96
commit 6e1a1b2429
11 changed files with 312 additions and 29 deletions

View File

@@ -0,0 +1,50 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Printbase.Application.Products.Commands.CreateProduct;
using Printbase.Application.Products.Queries;
namespace Printbase.WebApi.Controllers;
[ApiController]
[Route("api/[controller]")]
public class ProductsController(IMediator mediator) : ControllerBase
{
private readonly IMediator _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetProductById(Guid id, [FromQuery] bool includeVariants = true)
{
var query = new GetProductByIdQuery(id, includeVariants);
var result = await _mediator.Send(query);
if (result == null)
{
return NotFound();
}
return Ok(result);
}
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateProduct([FromBody] CreateProductCommand command)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
var result = await _mediator.Send(command);
return CreatedAtAction(nameof(GetProductById), new { id = result.Id }, result);
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
}
}