fromliberty
fromliberty
CC#
Created by fromliberty on 12/1/2023 in #help
getting null error
public async Task<IEnumerable<ProductDto>> GetProductsByLabel(string labelName)
{
// this will get products based on label, it will be useful to filter based on label.
var productsQuery = await _db.Products
.Where(p => p.Labels.Any(l => l.Name == labelName)).ToListAsync();
var products = MapProductsToDto(productsQuery);
return _mapper.Map<List<ProductDto>>(products);
}

private static List<ProductDto> MapProductsToDto(IEnumerable<Product> products)
{
return products.Select(MapProductToDto).ToList();
}

private static ProductDto MapProductToDto(Product p)
{
return new ProductDto
{
ProductId = p.ProductId,
Name = p.Name,
CategoryName = p.CategoryName,
ImageUrl = p.ImageUrl,
Description = p.Description,
HoverImageUrl = p.HoverImageUrl,
Colours = p.Colours.Select(c => new ColourDto
{
Id = c.Id,
Name = c.Name,
Img = c.Img,
Quantity = c.Quantity
}).ToList(),
Labels = p.Labels.Select(l => new LabelDto
{
Id = l.LabelId,
Name = l.Name
}).ToList(),
Price = p.Price,
Rating = new RatingDto
{
Count = p.Rating.Count,
Rate = p.Rating.Rate
}
};
}
public async Task<IEnumerable<ProductDto>> GetProductsByLabel(string labelName)
{
// this will get products based on label, it will be useful to filter based on label.
var productsQuery = await _db.Products
.Where(p => p.Labels.Any(l => l.Name == labelName)).ToListAsync();
var products = MapProductsToDto(productsQuery);
return _mapper.Map<List<ProductDto>>(products);
}

private static List<ProductDto> MapProductsToDto(IEnumerable<Product> products)
{
return products.Select(MapProductToDto).ToList();
}

private static ProductDto MapProductToDto(Product p)
{
return new ProductDto
{
ProductId = p.ProductId,
Name = p.Name,
CategoryName = p.CategoryName,
ImageUrl = p.ImageUrl,
Description = p.Description,
HoverImageUrl = p.HoverImageUrl,
Colours = p.Colours.Select(c => new ColourDto
{
Id = c.Id,
Name = c.Name,
Img = c.Img,
Quantity = c.Quantity
}).ToList(),
Labels = p.Labels.Select(l => new LabelDto
{
Id = l.LabelId,
Name = l.Name
}).ToList(),
Price = p.Price,
Rating = new RatingDto
{
Count = p.Rating.Count,
Rate = p.Rating.Rate
}
};
}
` so i have this code, i just refactor pre-existing code to use in multiple methods but, getting error now somehow
5 replies
CC#
Created by fromliberty on 11/30/2023 in #help
my response
No description
160 replies