From 4a358ede8983e7e01b070ccd29fc866f86cd1f51 Mon Sep 17 00:00:00 2001 From: Kira Date: Fri, 16 Sep 2022 12:26:11 -0700 Subject: [PATCH] Improved performance: only one call to db rather than two when regarding products with their categories information. --- .../Controllers/ProductController.cs | 22 +++++-------------- ShopOnline.Api/Entities/Product.cs | 7 +++++- ShopOnline.Api/Extensions/DtoConversions.cs | 15 +++++-------- .../Repositories/ProductRepository.cs | 12 +++++----- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/ShopOnline.Api/Controllers/ProductController.cs b/ShopOnline.Api/Controllers/ProductController.cs index 3bf66f9..092437f 100644 --- a/ShopOnline.Api/Controllers/ProductController.cs +++ b/ShopOnline.Api/Controllers/ProductController.cs @@ -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); } diff --git a/ShopOnline.Api/Entities/Product.cs b/ShopOnline.Api/Entities/Product.cs index b0c3ba0..83e86f6 100644 --- a/ShopOnline.Api/Entities/Product.cs +++ b/ShopOnline.Api/Entities/Product.cs @@ -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; } } } diff --git a/ShopOnline.Api/Extensions/DtoConversions.cs b/ShopOnline.Api/Extensions/DtoConversions.cs index 4af1f0d..637c165 100644 --- a/ShopOnline.Api/Extensions/DtoConversions.cs +++ b/ShopOnline.Api/Extensions/DtoConversions.cs @@ -16,12 +16,9 @@ namespace ShopOnline.Api.Extensions }).ToList(); } - public static IEnumerable ConvertToDto(this IEnumerable products, - IEnumerable productCategories) + public static IEnumerable ConvertToDto(this IEnumerable 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 + }; } diff --git a/ShopOnline.Api/Repositories/ProductRepository.cs b/ShopOnline.Api/Repositories/ProductRepository.cs index a081775..1a93d42 100644 --- a/ShopOnline.Api/Repositories/ProductRepository.cs +++ b/ShopOnline.Api/Repositories/ProductRepository.cs @@ -26,22 +26,24 @@ namespace ShopOnline.Api.Repositories public async Task 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> GetItems() { - var products = await this.shopOnlineDbContext.Products.ToListAsync(); + var products = await this.shopOnlineDbContext.Products + .Include(p => p.ProductCategory).ToArrayAsync(); return products; } public async Task> 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; }