From 48b17c886ca14b124cdace813402f48a79ae4502 Mon Sep 17 00:00:00 2001 From: Andrew Peters Date: Thu, 14 Apr 2016 13:11:23 -0700 Subject: [PATCH 1/4] Various improvements: - Use more efficient projection query for GenreMenuComponent. - Change some sync EF calls to async. - Log at Information level when Development. Fix: #636 Conflicts: src/MusicStore/Components/GenreMenuComponent.cs --- src/MusicStore/Components/GenreMenuComponent.cs | 13 +------------ .../Controllers/ShoppingCartController.cs | 6 +++--- src/MusicStore/Models/CartItem.cs | 2 +- src/MusicStore/Models/ShoppingCart.cs | 6 +++--- src/MusicStore/Startup.cs | 2 +- .../Shared/Components/GenreMenu/Default.cshtml | 6 +++--- 6 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/MusicStore/Components/GenreMenuComponent.cs b/src/MusicStore/Components/GenreMenuComponent.cs index 883144b..ddf7bdd 100644 --- a/src/MusicStore/Components/GenreMenuComponent.cs +++ b/src/MusicStore/Components/GenreMenuComponent.cs @@ -19,20 +19,9 @@ namespace MusicStore.Components public async Task InvokeAsync() { - var genres = await GetGenres(); + var genres = await DbContext.Genres.Select(g => g.Name).Take(9).ToListAsync(); return View(genres); } - - private async Task> GetGenres() - { - return await DbContext.Genres - .Include(g => g.Albums).ThenInclude(a => a.OrderDetails) - // TODO use nested sum https://github.com/aspnet/EntityFramework/issues/3792 - //.OrderByDescending( - // g => g.Albums.Sum(a => a.OrderDetails.Sum(od => od.Quantity))) - .Take(9) - .ToListAsync(); - } } } \ No newline at end of file diff --git a/src/MusicStore/Controllers/ShoppingCartController.cs b/src/MusicStore/Controllers/ShoppingCartController.cs index d2bfb40..e51d8bc 100644 --- a/src/MusicStore/Controllers/ShoppingCartController.cs +++ b/src/MusicStore/Controllers/ShoppingCartController.cs @@ -40,13 +40,13 @@ namespace MusicStore.Controllers public async Task AddToCart(int id, CancellationToken requestAborted) { // Retrieve the album from the database - var addedAlbum = DbContext.Albums - .Single(album => album.AlbumId == id); + var addedAlbum = await DbContext.Albums + .SingleAsync(album => album.AlbumId == id); // Add it to the shopping cart var cart = ShoppingCart.GetCart(DbContext, HttpContext); - cart.AddToCart(addedAlbum); + await cart.AddToCart(addedAlbum); await DbContext.SaveChangesAsync(requestAborted); diff --git a/src/MusicStore/Models/CartItem.cs b/src/MusicStore/Models/CartItem.cs index 64550ab..3311528 100644 --- a/src/MusicStore/Models/CartItem.cs +++ b/src/MusicStore/Models/CartItem.cs @@ -14,7 +14,7 @@ namespace MusicStore.Models public int Count { get; set; } [DataType(DataType.DateTime)] - public DateTime DateCreated { get; set; } + public DateTime DateCreated { get; set; } public virtual Album Album { get; set; } } diff --git a/src/MusicStore/Models/ShoppingCart.cs b/src/MusicStore/Models/ShoppingCart.cs index 80a0943..483f2c8 100644 --- a/src/MusicStore/Models/ShoppingCart.cs +++ b/src/MusicStore/Models/ShoppingCart.cs @@ -24,10 +24,10 @@ namespace MusicStore.Models public static ShoppingCart GetCart(MusicStoreContext db, string cartId) => new ShoppingCart(db, cartId); - public void AddToCart(Album album) + public async Task AddToCart(Album album) { // Get the matching cart and album instances - var cartItem = _dbContext.CartItems.SingleOrDefault( + var cartItem = await _dbContext.CartItems.SingleOrDefaultAsync( c => c.CartId == _shoppingCartId && c.AlbumId == album.AlbumId); @@ -127,7 +127,7 @@ namespace MusicStore.Models foreach (var item in cartItems) { //var album = _db.Albums.Find(item.AlbumId); - var album = _dbContext.Albums.Single(a => a.AlbumId == item.AlbumId); + var album = await _dbContext.Albums.SingleAsync(a => a.AlbumId == item.AlbumId); var orderDetail = new OrderDetail { diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index 921d2b6..dda5532 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -96,7 +96,7 @@ namespace MusicStore //The allowed values are Development,Staging and Production public void ConfigureDevelopment(IApplicationBuilder app, ILoggerFactory loggerFactory) { - loggerFactory.AddConsole(minLevel: LogLevel.Warning); + loggerFactory.AddConsole(minLevel: LogLevel.Information); // StatusCode pages to gracefully handle status codes 400-599. app.UseStatusCodePagesWithRedirects("~/Home/StatusCodePage"); diff --git a/src/MusicStore/Views/Shared/Components/GenreMenu/Default.cshtml b/src/MusicStore/Views/Shared/Components/GenreMenu/Default.cshtml index 1034041..601d74c 100644 --- a/src/MusicStore/Views/Shared/Components/GenreMenu/Default.cshtml +++ b/src/MusicStore/Views/Shared/Components/GenreMenu/Default.cshtml @@ -1,12 +1,12 @@ -@model IEnumerable +@model IEnumerable