aspnetcore/src/MusicStore/Controllers/StoreController.cs

76 lines
2.1 KiB
C#

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.Framework.Caching.Memory;
using MusicStore.Models;
namespace MusicStore.Controllers
{
public class StoreController : Controller
{
[FromServices]
public MusicStoreContext DbContext { get; set; }
[FromServices]
public IMemoryCache Cache { get; set; }
//
// GET: /Store/
public async Task<IActionResult> Index()
{
var genres = await DbContext.Genres.ToListAsync();
return View(genres);
}
//
// GET: /Store/Browse?genre=Disco
public async Task<IActionResult> Browse(string genre)
{
// Retrieve Genre genre and its Associated associated Albums albums from database
var genreModel = await DbContext.Genres
.Include(g => g.Albums)
.Where(g => g.Name == genre)
.FirstOrDefaultAsync();
if (genreModel == null)
{
return HttpNotFound();
}
return View(genreModel);
}
public async Task<IActionResult> Details(int id)
{
var cacheKey = string.Format("album_{0}", id);
Album album;
if(!Cache.TryGetValue(cacheKey, out album))
{
album = await DbContext.Albums
.Where(a => a.AlbumId == id)
.Include(a => a.Artist)
.Include(a => a.Genre)
.FirstOrDefaultAsync();
if (album != null)
{
//Remove it from cache if not retrieved in last 10 minutes
Cache.Set(
cacheKey,
album,
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
}
}
if (album == null)
{
return HttpNotFound();
}
return View(album);
}
}
}