30 lines
1.1 KiB
C#
30 lines
1.1 KiB
C#
using LiteCharms.Infrastructure.Database;
|
|
|
|
namespace LiteCharms.Features.Orders.Commands.Handlers;
|
|
|
|
public class CreateOrderCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<CreateOrderCommand, Result<Guid>>
|
|
{
|
|
public async ValueTask<Result<Guid>> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var newOrder = context.Orders.Add(new Entities.Order
|
|
{
|
|
CustomerId = request.CustomerId,
|
|
ProductPriceId = request.ProductPriceId,
|
|
CreatedAt = DateTime.UtcNow
|
|
});
|
|
|
|
return await context.SaveChangesAsync(cancellationToken) > 0
|
|
? Result.Ok(newOrder.Entity.Id)
|
|
: Result.Fail<Guid>(new Error($"Failed to create customer {request.CustomerId} order using product price {request.ProductPriceId}."));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
|
|
}
|
|
}
|
|
}
|