diff --git a/src/Printbase.Application/Products/Handlers/CreateProductHandler.cs b/src/Printbase.Application/Products/Handlers/CreateProductHandler.cs index de8769c..c1b8c12 100644 --- a/src/Printbase.Application/Products/Handlers/CreateProductHandler.cs +++ b/src/Printbase.Application/Products/Handlers/CreateProductHandler.cs @@ -26,7 +26,6 @@ public class CreateProductHandler(IUnitOfWork unitOfWork) : IRequestHandler filterParameters.SortDirection.ToUpper() == "DESC" + "price" => filterParameters.SortDirection.Equals("DESC" + , StringComparison.CurrentCultureIgnoreCase) ? query.OrderByDescending(p => p.BasePrice) : query.OrderBy(p => p.BasePrice), - "name" => filterParameters.SortDirection.ToUpper() == "DESC" + "name" => filterParameters.SortDirection.Equals("DESC" + , StringComparison.CurrentCultureIgnoreCase) ? query.OrderByDescending(p => p.Name) : query.OrderBy(p => p.Name), _ => query.OrderBy(p => p.Name) @@ -123,9 +125,17 @@ public class ProductRepository(ApplicationDbContext context) : IProductRepositor product.Id = Guid.NewGuid(); product.CreatedAt = DateTime.UtcNow; product.ModifiedAt = DateTime.UtcNow; - + context.Products.Add(product); await context.SaveChangesAsync(cancellationToken); + + if (product.CategoryId.HasValue) + { + await context.Entry(product) + .Reference(p => p.Category) + .LoadAsync(cancellationToken); + } + return product; } diff --git a/src/Printbase.WebApi/Controllers/CategoriesController.cs b/src/Printbase.WebApi/Controllers/CategoriesController.cs index 5246dd3..5f9347b 100644 --- a/src/Printbase.WebApi/Controllers/CategoriesController.cs +++ b/src/Printbase.WebApi/Controllers/CategoriesController.cs @@ -2,6 +2,7 @@ using MediatR; using Microsoft.AspNetCore.Mvc; using Printbase.Application.Products.Commands; using Printbase.Application.Products.Dtos; +using Printbase.Application.Products.Queries; namespace Printbase.WebApi.Controllers; @@ -9,6 +10,21 @@ namespace Printbase.WebApi.Controllers; [Route("api/[controller]")] public class CategoriesController(IMediator mediator) : ControllerBase { + [HttpGet] + public async Task>> GetCategories( + [FromQuery] bool? isActive = null, + [FromQuery] bool rootCategoriesOnly = false) + { + var query = new GetCategoriesQuery + { + IsActive = isActive, + RootCategoriesOnly = rootCategoriesOnly + }; + + var result = await mediator.Send(query); + return Ok(result); + } + [HttpPost] public async Task> CreateCategory([FromBody] CreateCategoryCommand command) { diff --git a/src/Printbase.WebApi/Controllers/ProductVariantsController.cs b/src/Printbase.WebApi/Controllers/ProductVariantsController.cs index 8236427..9d6ed49 100644 --- a/src/Printbase.WebApi/Controllers/ProductVariantsController.cs +++ b/src/Printbase.WebApi/Controllers/ProductVariantsController.cs @@ -2,6 +2,7 @@ using MediatR; using Microsoft.AspNetCore.Mvc; using Printbase.Application.Products.Commands; using Printbase.Application.Products.Dtos; +using Printbase.Application.Products.Queries; namespace Printbase.WebApi.Controllers; @@ -9,6 +10,23 @@ namespace Printbase.WebApi.Controllers; [Route("api/[controller]")] public class ProductVariantsController(IMediator mediator) : ControllerBase { + [HttpGet] + public async Task>> GetProductVariants( + [FromQuery] Guid? productId = null, + [FromQuery] bool? isActive = null, + [FromQuery] bool inStockOnly = false) + { + var query = new GetProductVariantsQuery + { + ProductId = productId, + IsActive = isActive, + InStockOnly = inStockOnly + }; + + var result = await mediator.Send(query); + return Ok(result); + } + [HttpPost] public async Task> CreateProductVariant([FromBody] CreateProductVariantCommand command) { @@ -16,7 +34,7 @@ public class ProductVariantsController(IMediator mediator) : ControllerBase return CreatedAtAction(nameof(CreateProductVariant), new { id = result.Id }, result); } - [HttpDelete("{id}")] + [HttpDelete("{id:guid}")] public async Task DeleteProductVariant(Guid id) { var command = new DeleteProductVariantCommand { Id = id };