Add product by id handler

This commit is contained in:
lumijiez
2025-06-26 01:36:12 +03:00
parent a8ea4b41ee
commit c7dcbc1926
4 changed files with 60 additions and 3 deletions

View File

@@ -1,6 +1,51 @@
using Imprink.Application.Dtos;
using MediatR;
namespace Imprink.Application.Commands.Products; namespace Imprink.Application.Commands.Products;
public class GetProductById public class GetProductByIdQuery : IRequest<ProductDto?>
{ {
public Guid ProductId { get; set; }
}
public class GetProductById(
IUnitOfWork unitOfWork)
: IRequestHandler<GetProductByIdQuery, ProductDto?>
{
public async Task<ProductDto?> Handle(
GetProductByIdQuery request,
CancellationToken cancellationToken)
{
var product = await unitOfWork.ProductRepository
.GetByIdAsync(request.ProductId, cancellationToken);
if (product == null)
return null;
return new ProductDto
{
Id = product.Id,
Name = product.Name,
Description = product.Description,
BasePrice = product.BasePrice,
IsCustomizable = product.IsCustomizable,
IsActive = product.IsActive,
ImageUrl = product.ImageUrl,
CategoryId = product.CategoryId,
Category = new CategoryDto
{
Id = product.Category.Id,
Name = product.Category.Name,
Description = product.Category.Description,
ImageUrl = product.Category.ImageUrl,
SortOrder = product.Category.SortOrder,
IsActive = product.Category.IsActive,
ParentCategoryId = product.Category.ParentCategoryId,
CreatedAt = product.Category.CreatedAt,
ModifiedAt = product.Category.ModifiedAt
},
CreatedAt = product.CreatedAt,
ModifiedAt = product.ModifiedAt
};
}
} }

View File

@@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Imprink.Infrastructure.Migrations namespace Imprink.Infrastructure.Migrations
{ {
[DbContext(typeof(ApplicationDbContext))] [DbContext(typeof(ApplicationDbContext))]
[Migration("20250625211612_InitialSetup")] [Migration("20250625223159_InitialSetup")]
partial class InitialSetup partial class InitialSetup
{ {
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -21,6 +21,18 @@ public class ProductsController(IMediator mediator) : ControllerBase
return Ok(result); return Ok(result);
} }
[HttpGet("{id:guid}")]
[AllowAnonymous]
public async Task<ActionResult<PagedResultDto<ProductDto>>> GetProductById(
Guid id,
CancellationToken cancellationToken)
{
var result = await mediator
.Send(new GetProductByIdQuery { ProductId = id}, cancellationToken);
return Ok(result);
}
[HttpPost] [HttpPost]
[Authorize(Roles = "Admin")] [Authorize(Roles = "Admin")]
public async Task<ActionResult<PagedResultDto<ProductDto>>> CreateProduct( public async Task<ActionResult<PagedResultDto<ProductDto>>> CreateProduct(