Api/Netina.Repository/Handlers/Addresses/GetUserAddressesQueryHandle...

42 lines
1.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using Netina.Common.Models.Api;
using Netina.Common.Models.Exception;
using Netina.Domain.CommandQueries.Queries;
using Netina.Domain.Dtos.SmallDtos;
using Netina.Domain.Entities.Users;
using Netina.Repository.Abstracts;
using Netina.Repository.Repositories.Base.Contracts;
namespace Netina.Repository.Handlers.Addresses;
public class GetUserAddressesQueryHandler : IRequestHandler<GetUserAddressesQuery, List<UserAddressSDto>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ICurrentUserService _currentUserService;
public GetUserAddressesQueryHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
{
_repositoryWrapper = repositoryWrapper;
_currentUserService = currentUserService;
}
public async Task<List<UserAddressSDto>> Handle(GetUserAddressesQuery request, CancellationToken cancellationToken)
{
Guid userId;
if (request.UserId != null)
userId = request.UserId.Value;
else
{
if (_currentUserService.UserId == null)
throw new AppException("User id notfound", ApiResultStatusCode.BadRequest);
if (!Guid.TryParse(_currentUserService.UserId, out userId))
throw new AppException("User id wrong", ApiResultStatusCode.BadRequest);
}
return await _repositoryWrapper.SetRepository<UserAddress>()
.TableNoTracking
.Where(ua => ua.UserId == userId)
.Select(UserAddressMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
}
}