Files
components/LiteCharms.Features/Products/Queries/Handlers/GetProductQueryHandler.cs
T
Khwezi Mngoma ff34326a53
continuous-integration/drone/pr Build is passing
Refactored database references
2026-05-09 16:58:34 +02:00

27 lines
963 B
C#

using LiteCharms.Extensions;
using LiteCharms.Infrastructure.Database;
using LiteCharms.Models;
namespace LiteCharms.Features.Products.Queries.Handlers;
public class GetProductQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetProductQuery, Result<Product>>
{
public async ValueTask<Result<Product>> Handle(GetProductQuery request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var product = await context.Products.FirstOrDefaultAsync(p => p.Id == request.ProductId, cancellationToken);
return product is not null
? Result.Ok(product.ToModel())
: Result.Fail<Product>(new Error($"Product with ID {request.ProductId} not found."));
}
catch (Exception ex)
{
return Result.Fail<Product>(new Error(ex.Message).CausedBy(ex));
}
}
}