diff --git a/src/Imprink.Application/Commands/Products/GetProductById.cs b/src/Imprink.Application/Commands/Products/GetProductById.cs index 565aaaf..44f2d7a 100644 --- a/src/Imprink.Application/Commands/Products/GetProductById.cs +++ b/src/Imprink.Application/Commands/Products/GetProductById.cs @@ -1,6 +1,51 @@ +using Imprink.Application.Dtos; +using MediatR; + namespace Imprink.Application.Commands.Products; -public class GetProductById +public class GetProductByIdQuery : IRequest { - + public Guid ProductId { get; set; } +} + +public class GetProductById( + IUnitOfWork unitOfWork) + : IRequestHandler +{ + public async Task 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 + }; + } } \ No newline at end of file diff --git a/src/Imprink.Infrastructure/Migrations/20250625211612_InitialSetup.Designer.cs b/src/Imprink.Infrastructure/Migrations/20250625223159_InitialSetup.Designer.cs similarity index 99% rename from src/Imprink.Infrastructure/Migrations/20250625211612_InitialSetup.Designer.cs rename to src/Imprink.Infrastructure/Migrations/20250625223159_InitialSetup.Designer.cs index 65b0a44..8e73eb7 100644 --- a/src/Imprink.Infrastructure/Migrations/20250625211612_InitialSetup.Designer.cs +++ b/src/Imprink.Infrastructure/Migrations/20250625223159_InitialSetup.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Imprink.Infrastructure.Migrations { [DbContext(typeof(ApplicationDbContext))] - [Migration("20250625211612_InitialSetup")] + [Migration("20250625223159_InitialSetup")] partial class InitialSetup { /// diff --git a/src/Imprink.Infrastructure/Migrations/20250625211612_InitialSetup.cs b/src/Imprink.Infrastructure/Migrations/20250625223159_InitialSetup.cs similarity index 100% rename from src/Imprink.Infrastructure/Migrations/20250625211612_InitialSetup.cs rename to src/Imprink.Infrastructure/Migrations/20250625223159_InitialSetup.cs diff --git a/src/Imprink.WebApi/Controllers/ProductsController.cs b/src/Imprink.WebApi/Controllers/ProductsController.cs index 6d7c0b2..9a4fef9 100644 --- a/src/Imprink.WebApi/Controllers/ProductsController.cs +++ b/src/Imprink.WebApi/Controllers/ProductsController.cs @@ -21,6 +21,18 @@ public class ProductsController(IMediator mediator) : ControllerBase return Ok(result); } + [HttpGet("{id:guid}")] + [AllowAnonymous] + public async Task>> GetProductById( + Guid id, + CancellationToken cancellationToken) + { + var result = await mediator + .Send(new GetProductByIdQuery { ProductId = id}, cancellationToken); + + return Ok(result); + } + [HttpPost] [Authorize(Roles = "Admin")] public async Task>> CreateProduct(