You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How can I use parameterization in this project?
In this project, we use the IMapFrom interface for Automapper.I have a problem when using .ProjectTo in some places.For example, in some places, I need to show the product price differently depending on the user type .
A more logical and best practice version of these codes is required.
using Application.Common.Interfaces;
using Application.Common.Mappings;
using Application.Common.Models;
using AutoMapper.QueryableExtensions;
using MediatR;
namespace Application.Features
{
public record GetProductsByCategoryQuery : IRequest<PaginatedList>
{
public Guid CategoryId { get; init; }
public int PageNumber { get; init; } = 1;
public int PageSize { get; init; } = 10;
}
public class GetCompanyQueryHandler : IRequestHandler<GetProductsByCategoryQuery, PaginatedList<GetProductsByCategoryQueryDto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly ICurrentUserService _currentUser;
public GetCompanyQueryHandler(IApplicationDbContext context, IMapper mapper, ICurrentUserService currentUser)
{
_context = context;
_mapper = mapper;
_currentUser = currentUser;
}
public async Task<PaginatedList<GetProductsByCategoryQueryDto>> Handle(GetProductsByCategoryQuery request, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(_currentUser.UserId))
throw new GlobalEx.UnauthorizedException("Unauthorized");
if (!Enum.TryParse<ApplicationUserType>(_currentUser.UserType, out ApplicationUserType userType))
throw new ArgumentException("Invalid user type");
IQueryable<GetProductsByCategoryQueryDto> queryDto;
if (userType == ApplicationUserType.Cosmetologist)
{
queryDto = _context.Products
.Where(i => i.CategoryId == request.CategoryId)
.ProjectTo<GetProductsByCategoryQueryDto>(
_mapper.ConfigurationProvider,
x => x.ProductPrices
);
}
else
{
queryDto = _context.Products
.Where(i => i.CategoryId == request.CategoryId)
.Select(i => new Product
{
Id = i.Id,
Name = i.Name,
Created = i.Created,
Price = i.CosmetologOnly ? null : i.Price,
ProductPrices = i.ProductPrices,
CosmetologOnly = i.CosmetologOnly,
Discount = i.Discount,
CategoryId = i.CategoryId,
ProductPhotos = i.ProductPhotos
})
.ProjectTo<GetProductsByCategoryQueryDto>(
_mapper.ConfigurationProvider
);
}
if (_currentUser.Role == "Admin")
queryDto = queryDto.IgnoreQueryFilters();
queryDto = queryDto.OrderBy(x => x.Created);
return await queryDto.PaginatedListAsync(request.PageNumber, request.PageSize);
}
}
}
public class GetProductsByCategoryQueryDto : ProductDto, IMapFrom
{
public string PictureUrl { get; set; } = string.Empty;
public int Discount { get; set; }
public DateTime Created { get; set; }
public ICollection? ProductPrices { get; set; } = new List();
public void Mapping(Profile profile)
{
profile.CreateMap<Product, GetProductsByCategoryQueryDto>()
.ForMember(d => d.ProductPrices, otp => otp.ExplicitExpansion())
.ForMember(d => d.PictureUrl, opt => opt.MapFrom(s =>
StorageManager.GetUrl(s.ProductPhotos.First().ProductFile.Storage,
s.ProductPhotos.First().ProductFile.Path) ?? string.Empty))
.ForMember(x => x.Discount, otp =>
otp.MapFrom(s => s.Discount.EndDate >= DateTime.Now ? s.Discount.DiscountPercentage : 0));
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
How can I use parameterization in this project?
In this project, we use the IMapFrom interface for Automapper.I have a problem when using .ProjectTo in some places.For example, in some places, I need to show the product price differently depending on the user type .
A more logical and best practice version of these codes is required.
using Application.Common.Interfaces;
using Application.Common.Mappings;
using Application.Common.Models;
using AutoMapper.QueryableExtensions;
using MediatR;
namespace Application.Features
{
public record GetProductsByCategoryQuery : IRequest<PaginatedList>
{
public Guid CategoryId { get; init; }
public int PageNumber { get; init; } = 1;
public int PageSize { get; init; } = 10;
}
}
public class GetProductsByCategoryQueryDto : ProductDto, IMapFrom
{
public string PictureUrl { get; set; } = string.Empty;
public int Discount { get; set; }
public DateTime Created { get; set; }
public ICollection? ProductPrices { get; set; } = new List();
public void Mapping(Profile profile)
{
profile.CreateMap<Product, GetProductsByCategoryQueryDto>()
.ForMember(d => d.ProductPrices, otp => otp.ExplicitExpansion())
.ForMember(d => d.PictureUrl, opt => opt.MapFrom(s =>
StorageManager.GetUrl(s.ProductPhotos.First().ProductFile.Storage,
s.ProductPhotos.First().ProductFile.Path) ?? string.Empty))
.ForMember(x => x.Discount, otp =>
otp.MapFrom(s => s.Discount.EndDate >= DateTime.Now ? s.Discount.DiscountPercentage : 0));
}
}
There are codes in this link that may be useful to me, but I could not use them in this project.
https://docs.automapper.org/en/stable/Queryable-Extensions.html#parameterization
Beta Was this translation helpful? Give feedback.
All reactions