Files
nashel-backend/src/Modules/Catalog/Application/Queries/GetMyOffersQuery.cs

47 lines
1.6 KiB
C#

using MediatR;
using Nashel.BuildingBlocks.Application.Abstractions;
using Nashel.BuildingBlocks.Domain;
using Nashel.Modules.Catalog.Application.Common;
using Nashel.Modules.Catalog.Domain.Repositories;
namespace Nashel.Modules.Catalog.Application.Queries;
/// <summary>
/// Запрос получения услуг текущего пользователя.
/// </summary>
public record GetMyOffersQuery() : IRequest<Result<List<OfferDto>>>;
public class GetMyOffersQueryHandler : IRequestHandler<GetMyOffersQuery, Result<List<OfferDto>>>
{
private readonly IOfferRepository _repository;
private readonly ICurrentUserService _currentUserService;
public GetMyOffersQueryHandler(IOfferRepository repository, ICurrentUserService currentUserService)
{
_repository = repository;
_currentUserService = currentUserService;
}
public async Task<Result<List<OfferDto>>> Handle(GetMyOffersQuery request, CancellationToken cancellationToken)
{
var userId = _currentUserService.UserId;
if (userId == null) return Result<List<OfferDto>>.Failure("Неавторизован");
var offers = await _repository.GetByPerformerIdAsync(userId.Value, cancellationToken);
var list = offers.Select(offer => new OfferDto(
offer.Id,
offer.PerformerId,
offer.CategoryId,
offer.Title,
offer.Description ?? string.Empty,
offer.Price,
offer.Attributes,
offer.IsActive,
offer.Images
)).ToList();
return Result<List<OfferDto>>.Success(list);
}
}