30 lines
1.0 KiB
C#
30 lines
1.0 KiB
C#
using LiteCharms.Features.Shop.Postgres;
|
|
|
|
namespace LiteCharms.Features.Orders.Commands.Handlers;
|
|
|
|
public class UpdateOrderStatusCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdateOrderStatusCommand, Result>
|
|
{
|
|
public async ValueTask<Result> Handle(UpdateOrderStatusCommand request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var order = await context.Orders.FirstOrDefaultAsync(o => o.Id == request.OrderId, cancellationToken);
|
|
|
|
if (order is null)
|
|
return Result.Fail(new Error($"Order {request.OrderId} not found"));
|
|
|
|
order.Status = request.Status;
|
|
|
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
|
? Result.Ok()
|
|
: Result.Fail(new Error($"Failed to update order {request.OrderId}"));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
|
}
|
|
}
|
|
}
|