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
|
try
|
||||||
{
|
{
|
||||||
var products = await this.productRepository.GetItems();
|
var products = await this.productRepository.GetItems();
|
||||||
foreach (var product in products)
|
if (products == null)
|
||||||
{
|
|
||||||
Console.WriteLine("Product [" + product.Id + "]: " + product.Price + " / " + product.Quantity);
|
|
||||||
}
|
|
||||||
|
|
||||||
var productCategories = await this.productRepository.GetCategories();
|
|
||||||
if (products == null || productCategories == null)
|
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var productDtos = products.ConvertToDto(productCategories);
|
var productDtos = products.ConvertToDto();
|
||||||
|
|
||||||
//foreach (var product in productDtos)
|
|
||||||
//{
|
|
||||||
// Console.WriteLine("ProductDTO [" + product.Id + "]: " + product.Price + " / " + product.Quantity);
|
|
||||||
//}
|
|
||||||
|
|
||||||
return Ok(productDtos);
|
return Ok(productDtos);
|
||||||
}
|
}
|
||||||
|
@ -63,8 +52,8 @@ namespace ShopOnline.Api.Controllers
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var productCategory = await this.productRepository.GetCategory(product.CategoryId);
|
|
||||||
var productDto = product.ConvertToDto(productCategory);
|
var productDto = product.ConvertToDto();
|
||||||
return Ok(productDto);
|
return Ok(productDto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,8 +89,7 @@ namespace ShopOnline.Api.Controllers
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var products = await productRepository.GetItemsByCategory(categoryId);
|
var products = await productRepository.GetItemsByCategory(categoryId);
|
||||||
var productCategories = await productRepository.GetCategories();
|
var productDtos = products.ConvertToDto();
|
||||||
var productDtos = products.ConvertToDto(productCategories);
|
|
||||||
|
|
||||||
return Ok(productDtos);
|
return Ok(productDtos);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
namespace ShopOnline.Api.Entities
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace ShopOnline.Api.Entities
|
||||||
{
|
{
|
||||||
public class Product
|
public class Product
|
||||||
{
|
{
|
||||||
|
@ -9,5 +11,8 @@
|
||||||
public decimal Price { get; set; }
|
public decimal Price { get; set; }
|
||||||
public int Quantity { get; set; }
|
public int Quantity { get; set; }
|
||||||
public int CategoryId { get; set; }
|
public int CategoryId { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("CategoryId")]
|
||||||
|
public ProductCategory ProductCategory { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,12 +16,9 @@ namespace ShopOnline.Api.Extensions
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<ProductDto> ConvertToDto(this IEnumerable<Product> products,
|
public static IEnumerable<ProductDto> ConvertToDto(this IEnumerable<Product> products)
|
||||||
IEnumerable<ProductCategory> productCategories)
|
|
||||||
{
|
{
|
||||||
return (from product in products
|
return (from product in products
|
||||||
join ProductCategory in productCategories
|
|
||||||
on product.CategoryId equals ProductCategory.Id
|
|
||||||
select new ProductDto
|
select new ProductDto
|
||||||
{
|
{
|
||||||
Id = product.Id,
|
Id = product.Id,
|
||||||
|
@ -30,13 +27,12 @@ namespace ShopOnline.Api.Extensions
|
||||||
ImageURL = product.ImageURL,
|
ImageURL = product.ImageURL,
|
||||||
Price = product.Price,
|
Price = product.Price,
|
||||||
Quantity = product.Quantity,
|
Quantity = product.Quantity,
|
||||||
CategoryId = product.CategoryId,
|
CategoryId = product.ProductCategory.Id,
|
||||||
CategoryName = ProductCategory.Name
|
CategoryName = product.ProductCategory.Name
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ProductDto ConvertToDto(this Product product,
|
public static ProductDto ConvertToDto(this Product product)
|
||||||
ProductCategory productCategory)
|
|
||||||
{
|
{
|
||||||
return new ProductDto
|
return new ProductDto
|
||||||
{
|
{
|
||||||
|
@ -47,7 +43,8 @@ namespace ShopOnline.Api.Extensions
|
||||||
Price= product.Price,
|
Price= product.Price,
|
||||||
Quantity= product.Quantity,
|
Quantity= product.Quantity,
|
||||||
CategoryId= product.CategoryId,
|
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)
|
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;
|
return product;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<Product>> GetItems()
|
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;
|
return products;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<Product>> GetItemsByCategory(int id)
|
public async Task<IEnumerable<Product>> GetItemsByCategory(int id)
|
||||||
{
|
{
|
||||||
var products = await (from product in shopOnlineDbContext.Products
|
var products = await this.shopOnlineDbContext.Products
|
||||||
where product.CategoryId == id
|
.Include(p => p.ProductCategory)
|
||||||
select product).ToListAsync();
|
.Where(p => p.CategoryId == id).ToListAsync();
|
||||||
|
|
||||||
return products;
|
return products;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue