Improved performance: only one call to db rather than two when regarding products with their categories information.
This commit is contained in:
parent
a9e1cb479f
commit
4a358ede89
|
@ -21,24 +21,13 @@ namespace ShopOnline.Api.Controllers
|
|||
try
|
||||
{
|
||||
var products = await this.productRepository.GetItems();
|
||||
foreach (var product in products)
|
||||
{
|
||||
Console.WriteLine("Product [" + product.Id + "]: " + product.Price + " / " + product.Quantity);
|
||||
}
|
||||
|
||||
var productCategories = await this.productRepository.GetCategories();
|
||||
if (products == null || productCategories == null)
|
||||
if (products == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
var productDtos = products.ConvertToDto(productCategories);
|
||||
|
||||
//foreach (var product in productDtos)
|
||||
//{
|
||||
// Console.WriteLine("ProductDTO [" + product.Id + "]: " + product.Price + " / " + product.Quantity);
|
||||
//}
|
||||
var productDtos = products.ConvertToDto();
|
||||
|
||||
return Ok(productDtos);
|
||||
}
|
||||
|
@ -63,8 +52,8 @@ namespace ShopOnline.Api.Controllers
|
|||
}
|
||||
else
|
||||
{
|
||||
var productCategory = await this.productRepository.GetCategory(product.CategoryId);
|
||||
var productDto = product.ConvertToDto(productCategory);
|
||||
|
||||
var productDto = product.ConvertToDto();
|
||||
return Ok(productDto);
|
||||
}
|
||||
}
|
||||
|
@ -100,8 +89,7 @@ namespace ShopOnline.Api.Controllers
|
|||
try
|
||||
{
|
||||
var products = await productRepository.GetItemsByCategory(categoryId);
|
||||
var productCategories = await productRepository.GetCategories();
|
||||
var productDtos = products.ConvertToDto(productCategories);
|
||||
var productDtos = products.ConvertToDto();
|
||||
|
||||
return Ok(productDtos);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
namespace ShopOnline.Api.Entities
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace ShopOnline.Api.Entities
|
||||
{
|
||||
public class Product
|
||||
{
|
||||
|
@ -9,5 +11,8 @@
|
|||
public decimal Price { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
public int CategoryId { get; set; }
|
||||
|
||||
[ForeignKey("CategoryId")]
|
||||
public ProductCategory ProductCategory { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,12 +16,9 @@ namespace ShopOnline.Api.Extensions
|
|||
}).ToList();
|
||||
}
|
||||
|
||||
public static IEnumerable<ProductDto> ConvertToDto(this IEnumerable<Product> products,
|
||||
IEnumerable<ProductCategory> productCategories)
|
||||
public static IEnumerable<ProductDto> ConvertToDto(this IEnumerable<Product> products)
|
||||
{
|
||||
return (from product in products
|
||||
join ProductCategory in productCategories
|
||||
on product.CategoryId equals ProductCategory.Id
|
||||
select new ProductDto
|
||||
{
|
||||
Id = product.Id,
|
||||
|
@ -30,13 +27,12 @@ namespace ShopOnline.Api.Extensions
|
|||
ImageURL = product.ImageURL,
|
||||
Price = product.Price,
|
||||
Quantity = product.Quantity,
|
||||
CategoryId = product.CategoryId,
|
||||
CategoryName = ProductCategory.Name
|
||||
CategoryId = product.ProductCategory.Id,
|
||||
CategoryName = product.ProductCategory.Name
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public static ProductDto ConvertToDto(this Product product,
|
||||
ProductCategory productCategory)
|
||||
public static ProductDto ConvertToDto(this Product product)
|
||||
{
|
||||
return new ProductDto
|
||||
{
|
||||
|
@ -47,7 +43,8 @@ namespace ShopOnline.Api.Extensions
|
|||
Price= product.Price,
|
||||
Quantity= product.Quantity,
|
||||
CategoryId= product.CategoryId,
|
||||
CategoryName= productCategory.Name
|
||||
CategoryName= product.ProductCategory.Name
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -26,22 +26,24 @@ namespace ShopOnline.Api.Repositories
|
|||
|
||||
public async Task<Product> GetItem(int id)
|
||||
{
|
||||
var product = await shopOnlineDbContext.Products.FindAsync(id);
|
||||
var product = await shopOnlineDbContext.Products.Include(p => p.ProductCategory).SingleOrDefaultAsync(p => p.Id == id);
|
||||
|
||||
return product;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> GetItems()
|
||||
{
|
||||
var products = await this.shopOnlineDbContext.Products.ToListAsync();
|
||||
var products = await this.shopOnlineDbContext.Products
|
||||
.Include(p => p.ProductCategory).ToArrayAsync();
|
||||
|
||||
return products;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Product>> GetItemsByCategory(int id)
|
||||
{
|
||||
var products = await (from product in shopOnlineDbContext.Products
|
||||
where product.CategoryId == id
|
||||
select product).ToListAsync();
|
||||
var products = await this.shopOnlineDbContext.Products
|
||||
.Include(p => p.ProductCategory)
|
||||
.Where(p => p.CategoryId == id).ToListAsync();
|
||||
|
||||
return products;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue