28 lines
1000 B
C#
28 lines
1000 B
C#
using LiteCharms.Extensions;
|
|
using LiteCharms.Features.Shop.CartPackages.Models;
|
|
using LiteCharms.Features.Shop.Postgres;
|
|
|
|
namespace LiteCharms.Features.CartPackages.Queries.Handlers;
|
|
|
|
public class GetPackageQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetPackageQuery, Result<Package>>
|
|
{
|
|
public async ValueTask<Result<Package>> Handle(GetPackageQuery request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == request.PackageId, cancellationToken);
|
|
|
|
return package is not null
|
|
? Result.Ok(package.ToModel())
|
|
: Result.Fail($"Failed to find package by ID {request.PackageId}");
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail<Package>(new Error(ex.Message).CausedBy(ex));
|
|
}
|
|
}
|
|
}
|