28 lines
1000 B
C#
28 lines
1000 B
C#
using LiteCharms.Extensions;
|
|
using LiteCharms.Infrastructure.Database;
|
|
using LiteCharms.Models;
|
|
|
|
namespace LiteCharms.Features.Products.Queries.Handlers;
|
|
|
|
public class GetProductPricesQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetProductPricesQuery, Result<ProductPrice[]>>
|
|
{
|
|
public async ValueTask<Result<ProductPrice[]>> Handle(GetProductPricesQuery request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var products = await context.ProductPrices.AsNoTracking()
|
|
.OrderByDescending(o => o.Id)
|
|
.Take(request.MaxRecords)
|
|
.ToArrayAsync(cancellationToken);
|
|
|
|
return Result.Ok(products.Select(p => p.ToModel()).ToArray());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail<ProductPrice[]>(new Error(ex.Message).CausedBy(ex));
|
|
}
|
|
}
|
|
}
|