using NetinaShop.Domain.Entities.Orders;

namespace NetinaShop.Repository.Handlers.Orders;

public class CreateBaseOrderCommandHandler : IRequestHandler<CreateBaseOrderCommand,Order>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public CreateBaseOrderCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<Order> Handle(CreateBaseOrderCommand request, CancellationToken cancellationToken)
    {
        if (request.UserId == default)
            throw new AppException("User id is null");

        var order = Order.Create(request.UserId);

        _repositoryWrapper.SetRepository<Order>()
            .Add(order);

        await _repositoryWrapper.SaveChangesAsync(cancellationToken);

        return order;
    }
}