Fix product repo not returning a valid object, add query

This commit is contained in:
lumijiez
2025-05-27 13:50:15 +03:00
parent 5f8c974eea
commit 585593da36
4 changed files with 50 additions and 5 deletions

View File

@@ -26,7 +26,6 @@ public class CreateProductHandler(IUnitOfWork unitOfWork) : IRequestHandler<Crea
}; };
var createdProduct = await unitOfWork.ProductRepository.AddAsync(product, cancellationToken); var createdProduct = await unitOfWork.ProductRepository.AddAsync(product, cancellationToken);
await unitOfWork.CommitTransactionAsync();
var categoryDto = new CategoryDto var categoryDto = new CategoryDto
{ {
@@ -40,6 +39,8 @@ public class CreateProductHandler(IUnitOfWork unitOfWork) : IRequestHandler<Crea
CreatedAt = createdProduct.Category.CreatedAt, CreatedAt = createdProduct.Category.CreatedAt,
ModifiedAt = createdProduct.Category.ModifiedAt ModifiedAt = createdProduct.Category.ModifiedAt
}; };
await unitOfWork.CommitTransactionAsync();
return new ProductDto return new ProductDto
{ {

View File

@@ -75,10 +75,12 @@ public class ProductRepository(ApplicationDbContext context) : IProductRepositor
query = filterParameters.SortBy.ToLower() switch query = filterParameters.SortBy.ToLower() switch
{ {
"price" => filterParameters.SortDirection.ToUpper() == "DESC" "price" => filterParameters.SortDirection.Equals("DESC"
, StringComparison.CurrentCultureIgnoreCase)
? query.OrderByDescending(p => p.BasePrice) ? query.OrderByDescending(p => p.BasePrice)
: query.OrderBy(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.OrderByDescending(p => p.Name)
: query.OrderBy(p => p.Name), : query.OrderBy(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.Id = Guid.NewGuid();
product.CreatedAt = DateTime.UtcNow; product.CreatedAt = DateTime.UtcNow;
product.ModifiedAt = DateTime.UtcNow; product.ModifiedAt = DateTime.UtcNow;
context.Products.Add(product); context.Products.Add(product);
await context.SaveChangesAsync(cancellationToken); await context.SaveChangesAsync(cancellationToken);
if (product.CategoryId.HasValue)
{
await context.Entry(product)
.Reference(p => p.Category)
.LoadAsync(cancellationToken);
}
return product; return product;
} }

View File

@@ -2,6 +2,7 @@ using MediatR;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Printbase.Application.Products.Commands; using Printbase.Application.Products.Commands;
using Printbase.Application.Products.Dtos; using Printbase.Application.Products.Dtos;
using Printbase.Application.Products.Queries;
namespace Printbase.WebApi.Controllers; namespace Printbase.WebApi.Controllers;
@@ -9,6 +10,21 @@ namespace Printbase.WebApi.Controllers;
[Route("api/[controller]")] [Route("api/[controller]")]
public class CategoriesController(IMediator mediator) : ControllerBase 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] [HttpPost]
public async Task<ActionResult<CategoryDto>> CreateCategory([FromBody] CreateCategoryCommand command) public async Task<ActionResult<CategoryDto>> CreateCategory([FromBody] CreateCategoryCommand command)
{ {

View File

@@ -2,6 +2,7 @@ using MediatR;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Printbase.Application.Products.Commands; using Printbase.Application.Products.Commands;
using Printbase.Application.Products.Dtos; using Printbase.Application.Products.Dtos;
using Printbase.Application.Products.Queries;
namespace Printbase.WebApi.Controllers; namespace Printbase.WebApi.Controllers;
@@ -9,6 +10,23 @@ namespace Printbase.WebApi.Controllers;
[Route("api/[controller]")] [Route("api/[controller]")]
public class ProductVariantsController(IMediator mediator) : ControllerBase 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] [HttpPost]
public async Task<ActionResult<ProductVariantDto>> CreateProductVariant([FromBody] CreateProductVariantCommand command) 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); return CreatedAtAction(nameof(CreateProductVariant), new { id = result.Id }, result);
} }
[HttpDelete("{id}")] [HttpDelete("{id:guid}")]
public async Task<ActionResult> DeleteProductVariant(Guid id) public async Task<ActionResult> DeleteProductVariant(Guid id)
{ {
var command = new DeleteProductVariantCommand { Id = id }; var command = new DeleteProductVariantCommand { Id = id };