using LiteCharms.Infrastructure.Database; namespace LiteCharms.Features.CartPackages.Commands.Handlers; public class UpdatePackageStatusCommandHandler(IDbContextFactory contextFactory) : IRequestHandler { public async ValueTask Handle(UpdatePackageStatusCommand request, CancellationToken cancellationToken) { try { using var context = await contextFactory.CreateDbContextAsync(cancellationToken); var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == request.PackageId, cancellationToken); if (package is null) return Result.Fail($"Could not find package by id {request.PackageId}"); package.Active = request.Active; return await context.SaveChangesAsync(cancellationToken) > 0 ? Result.Ok() : Result.Fail($"Failed to update package with id {request.PackageId}"); } catch (Exception ex) { return Result.Fail(new Error(ex.Message).CausedBy(ex)); } } }