using Microsoft.EntityFrameworkCore;

namespace NetinaShop.Repository.Handlers.Reviews;

public class GetReviewQueryHandler : IRequestHandler<GetReviewQuery,ReviewLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetReviewQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<ReviewLDto> Handle(GetReviewQuery request, CancellationToken cancellationToken)
    {
        var review = await _repositoryWrapper.SetRepository<Review>()
            .TableNoTracking
            .Where(r => r.Id == request.Id)
            .Select(ReviewMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (review == null)
            throw new AppException("Review not found", ApiResultStatusCode.NotFound);

        return review;
    }
}