Add query to retrieve all products
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace Printbase.Application.Products.Dtos;
|
||||
|
||||
public class AllProductsDto
|
||||
{
|
||||
public ICollection<ProductDto> Products {get;set;}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ public class ProductDto
|
||||
public Guid TypeId { get; set; }
|
||||
public string TypeName { get; set; } = string.Empty;
|
||||
public string GroupName { get; set; } = string.Empty;
|
||||
public ICollection<ProductVariantDto>? Variants { get; set; }
|
||||
public ICollection<ProductVariantDto> Variants { get; set; } = new List<ProductVariantDto>();
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using MediatR;
|
||||
using Printbase.Application.Products.Dtos;
|
||||
|
||||
namespace Printbase.Application.Products.Queries.GetAllProducts;
|
||||
|
||||
public class GetAllProductsQuery(bool includeVariants = true) : IRequest<AllProductsDto?>
|
||||
{
|
||||
public bool IncludeVariants { get; } = includeVariants;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using MediatR;
|
||||
using Printbase.Application.Products.Dtos;
|
||||
using Printbase.Domain.Repositories;
|
||||
|
||||
namespace Printbase.Application.Products.Queries.GetAllProducts;
|
||||
|
||||
public class GetAllProductsQueryHandler(IProductRepository productRepository)
|
||||
: IRequestHandler<GetAllProductsQuery, AllProductsDto?>
|
||||
{
|
||||
private readonly IProductRepository _productRepository = productRepository
|
||||
?? throw new ArgumentNullException(nameof(productRepository));
|
||||
|
||||
public async Task<AllProductsDto?> Handle(GetAllProductsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var products = await _productRepository.GetAllAsync(true, cancellationToken);
|
||||
|
||||
var allProducts = new AllProductsDto
|
||||
{
|
||||
Products = products.Select(p => new ProductDto
|
||||
{
|
||||
Id = p.Id,
|
||||
Name = p.Name,
|
||||
Description = p.Description,
|
||||
TypeId = p.TypeId,
|
||||
TypeName = p.Type.Name,
|
||||
GroupName = p.Type.Group.Name,
|
||||
CreatedAt = p.CreatedAt,
|
||||
UpdatedAt = p.UpdatedAt,
|
||||
IsActive = p.IsActive,
|
||||
Variants = request.IncludeVariants
|
||||
? p.Variants.Select(v => new ProductVariantDto
|
||||
{
|
||||
Id = v.Id,
|
||||
Color = v.Color,
|
||||
Size = v.Size,
|
||||
Price = v.Price,
|
||||
Discount = v.Discount,
|
||||
Stock = v.Stock,
|
||||
SKU = v.SKU,
|
||||
IsActive = v.IsActive
|
||||
}).ToList()
|
||||
: []
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
return allProducts;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Printbase.Application.Products.Commands.CreateProduct;
|
||||
using Printbase.Application.Products.Queries;
|
||||
using Printbase.Application.Products.Queries.GetAllProducts;
|
||||
using Printbase.Application.Products.Queries.GetProductById;
|
||||
|
||||
namespace Printbase.WebApi.Controllers;
|
||||
@@ -12,6 +12,18 @@ public class ProductsController(IMediator mediator) : ControllerBase
|
||||
{
|
||||
private readonly IMediator _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetAllProducts()
|
||||
{
|
||||
var query = new GetAllProductsQuery();
|
||||
var result = await _mediator.Send(query);
|
||||
|
||||
if (result == null) return NotFound();
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
@@ -20,11 +32,7 @@ public class ProductsController(IMediator mediator) : ControllerBase
|
||||
var query = new GetProductByIdQuery(id, includeVariants);
|
||||
var result = await _mediator.Send(query);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (result == null) return NotFound();
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
@@ -33,10 +41,7 @@ public class ProductsController(IMediator mediator) : ControllerBase
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> CreateProduct([FromBody] CreateProductCommand command)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user