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
This commit is contained in:
parent
8e4580f956
commit
48b17c886c
|
|
@ -19,20 +19,9 @@ namespace MusicStore.Components
|
||||||
|
|
||||||
public async Task<IViewComponentResult> InvokeAsync()
|
public async Task<IViewComponentResult> InvokeAsync()
|
||||||
{
|
{
|
||||||
var genres = await GetGenres();
|
var genres = await DbContext.Genres.Select(g => g.Name).Take(9).ToListAsync();
|
||||||
|
|
||||||
return View(genres);
|
return View(genres);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<List<Genre>> 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -40,13 +40,13 @@ namespace MusicStore.Controllers
|
||||||
public async Task<IActionResult> AddToCart(int id, CancellationToken requestAborted)
|
public async Task<IActionResult> AddToCart(int id, CancellationToken requestAborted)
|
||||||
{
|
{
|
||||||
// Retrieve the album from the database
|
// Retrieve the album from the database
|
||||||
var addedAlbum = DbContext.Albums
|
var addedAlbum = await DbContext.Albums
|
||||||
.Single(album => album.AlbumId == id);
|
.SingleAsync(album => album.AlbumId == id);
|
||||||
|
|
||||||
// Add it to the shopping cart
|
// Add it to the shopping cart
|
||||||
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
||||||
|
|
||||||
cart.AddToCart(addedAlbum);
|
await cart.AddToCart(addedAlbum);
|
||||||
|
|
||||||
await DbContext.SaveChangesAsync(requestAborted);
|
await DbContext.SaveChangesAsync(requestAborted);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,10 +24,10 @@ namespace MusicStore.Models
|
||||||
public static ShoppingCart GetCart(MusicStoreContext db, string cartId)
|
public static ShoppingCart GetCart(MusicStoreContext db, string cartId)
|
||||||
=> new ShoppingCart(db, cartId);
|
=> new ShoppingCart(db, cartId);
|
||||||
|
|
||||||
public void AddToCart(Album album)
|
public async Task AddToCart(Album album)
|
||||||
{
|
{
|
||||||
// Get the matching cart and album instances
|
// Get the matching cart and album instances
|
||||||
var cartItem = _dbContext.CartItems.SingleOrDefault(
|
var cartItem = await _dbContext.CartItems.SingleOrDefaultAsync(
|
||||||
c => c.CartId == _shoppingCartId
|
c => c.CartId == _shoppingCartId
|
||||||
&& c.AlbumId == album.AlbumId);
|
&& c.AlbumId == album.AlbumId);
|
||||||
|
|
||||||
|
|
@ -127,7 +127,7 @@ namespace MusicStore.Models
|
||||||
foreach (var item in cartItems)
|
foreach (var item in cartItems)
|
||||||
{
|
{
|
||||||
//var album = _db.Albums.Find(item.AlbumId);
|
//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
|
var orderDetail = new OrderDetail
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ namespace MusicStore
|
||||||
//The allowed values are Development,Staging and Production
|
//The allowed values are Development,Staging and Production
|
||||||
public void ConfigureDevelopment(IApplicationBuilder app, ILoggerFactory loggerFactory)
|
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.
|
// StatusCode pages to gracefully handle status codes 400-599.
|
||||||
app.UseStatusCodePagesWithRedirects("~/Home/StatusCodePage");
|
app.UseStatusCodePagesWithRedirects("~/Home/StatusCodePage");
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
@model IEnumerable<Genre>
|
@model IEnumerable<string>
|
||||||
|
|
||||||
<li class="dropdown">
|
<li class="dropdown">
|
||||||
<a asp-controller="Store" asp-action="Index" class="dropdown-toggle" data-toggle="dropdown">Store <b class="caret"></b></a>
|
<a asp-controller="Store" asp-action="Index" class="dropdown-toggle" data-toggle="dropdown">Store <b class="caret"></b></a>
|
||||||
<ul class="dropdown-menu">
|
<ul class="dropdown-menu">
|
||||||
@foreach (var genre in Model)
|
@foreach (var genreName in Model)
|
||||||
{
|
{
|
||||||
<li>
|
<li>
|
||||||
<a asp-controller="Store" asp-action="Browse" asp-route-Genre="@genre.Name">@genre.Name</a>
|
<a asp-controller="Store" asp-action="Browse" asp-route-Genre="@genreName">@genreName</a>
|
||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
<li class="divider"></li>
|
<li class="divider"></li>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue