- 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:
Andrew Peters 2016-04-14 13:11:23 -07:00
Родитель 8e4580f956
Коммит 48b17c886c
6 изменённых файлов: 12 добавлений и 23 удалений

Просмотреть файл

@ -19,20 +19,9 @@ namespace MusicStore.Components
public async Task<IViewComponentResult> InvokeAsync()
{
var genres = await GetGenres();
var genres = await DbContext.Genres.Select(g => g.Name).Take(9).ToListAsync();
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)
{
// 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);

Просмотреть файл

@ -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; }
}

Просмотреть файл

@ -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
{

Просмотреть файл

@ -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");

Просмотреть файл

@ -1,12 +1,12 @@
@model IEnumerable<Genre>
@model IEnumerable<string>
<li class="dropdown">
<a asp-controller="Store" asp-action="Index" class="dropdown-toggle" data-toggle="dropdown">Store <b class="caret"></b></a>
<ul class="dropdown-menu">
@foreach (var genre in Model)
@foreach (var genreName in Model)
{
<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 class="divider"></li>