Fix product repo not returning a valid object, add query
This commit is contained in:
@@ -26,7 +26,6 @@ public class CreateProductHandler(IUnitOfWork unitOfWork) : IRequestHandler<Crea
|
||||
};
|
||||
|
||||
var createdProduct = await unitOfWork.ProductRepository.AddAsync(product, cancellationToken);
|
||||
await unitOfWork.CommitTransactionAsync();
|
||||
|
||||
var categoryDto = new CategoryDto
|
||||
{
|
||||
@@ -40,6 +39,8 @@ public class CreateProductHandler(IUnitOfWork unitOfWork) : IRequestHandler<Crea
|
||||
CreatedAt = createdProduct.Category.CreatedAt,
|
||||
ModifiedAt = createdProduct.Category.ModifiedAt
|
||||
};
|
||||
|
||||
await unitOfWork.CommitTransactionAsync();
|
||||
|
||||
return new ProductDto
|
||||
{
|
||||
|
||||
@@ -75,10 +75,12 @@ public class ProductRepository(ApplicationDbContext context) : IProductRepositor
|
||||
|
||||
query = filterParameters.SortBy.ToLower() switch
|
||||
{
|
||||
"price" => 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ActionResult<IEnumerable<CategoryDto>>> 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<ActionResult<CategoryDto>> CreateCategory([FromBody] CreateCategoryCommand command)
|
||||
{
|
||||
|
||||
@@ -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<ActionResult<IEnumerable<ProductVariantDto>>> 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<ActionResult<ProductVariantDto>> 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<ActionResult> DeleteProductVariant(Guid id)
|
||||
{
|
||||
var command = new DeleteProductVariantCommand { Id = id };
|
||||
|
||||
Reference in New Issue
Block a user