Reacting to FromServices changes
This commit is contained in:
parent
9d3ceb66e5
commit
5a3c4957d9
|
|
@ -17,11 +17,12 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
[Authorize("ManageStore")]
|
[Authorize("ManageStore")]
|
||||||
public class StoreManagerController : Controller
|
public class StoreManagerController : Controller
|
||||||
{
|
{
|
||||||
[FromServices]
|
public StoreManagerController(MusicStoreContext dbContext)
|
||||||
public MusicStoreContext DbContext { get; set; }
|
{
|
||||||
|
DbContext = dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
[FromServices]
|
public MusicStoreContext DbContext { get; }
|
||||||
public IMemoryCache Cache { get; set; }
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /StoreManager/
|
// GET: /StoreManager/
|
||||||
|
|
@ -37,12 +38,14 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /StoreManager/Details/5
|
// GET: /StoreManager/Details/5
|
||||||
public async Task<IActionResult> Details(int id)
|
public async Task<IActionResult> Details(
|
||||||
|
[FromServices] IMemoryCache cache,
|
||||||
|
int id)
|
||||||
{
|
{
|
||||||
var cacheKey = GetCacheKey(id);
|
var cacheKey = GetCacheKey(id);
|
||||||
|
|
||||||
Album album;
|
Album album;
|
||||||
if(!Cache.TryGetValue(cacheKey, out album))
|
if (!cache.TryGetValue(cacheKey, out album))
|
||||||
{
|
{
|
||||||
album = await DbContext.Albums
|
album = await DbContext.Albums
|
||||||
.Where(a => a.AlbumId == id)
|
.Where(a => a.AlbumId == id)
|
||||||
|
|
@ -53,7 +56,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
if (album != null)
|
if (album != null)
|
||||||
{
|
{
|
||||||
//Remove it from cache if not retrieved in last 10 minutes.
|
//Remove it from cache if not retrieved in last 10 minutes.
|
||||||
Cache.Set(
|
cache.Set(
|
||||||
cacheKey,
|
cacheKey,
|
||||||
album,
|
album,
|
||||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||||
|
|
@ -62,7 +65,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
|
|
||||||
if (album == null)
|
if (album == null)
|
||||||
{
|
{
|
||||||
Cache.Remove(cacheKey);
|
cache.Remove(cacheKey);
|
||||||
return HttpNotFound();
|
return HttpNotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,7 +84,10 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
// POST: /StoreManager/Create
|
// POST: /StoreManager/Create
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> Create(Album album, CancellationToken requestAborted)
|
public async Task<IActionResult> Create(
|
||||||
|
Album album,
|
||||||
|
[FromServices] IMemoryCache cache,
|
||||||
|
CancellationToken requestAborted)
|
||||||
{
|
{
|
||||||
if (ModelState.IsValid)
|
if (ModelState.IsValid)
|
||||||
{
|
{
|
||||||
|
|
@ -94,7 +100,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
Url = Url.Action("Details", "Store", new { id = album.AlbumId })
|
Url = Url.Action("Details", "Store", new { id = album.AlbumId })
|
||||||
};
|
};
|
||||||
|
|
||||||
Cache.Remove("latestAlbum");
|
cache.Remove("latestAlbum");
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -125,14 +131,17 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
// POST: /StoreManager/Edit/5
|
// POST: /StoreManager/Edit/5
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> Edit(Album album, CancellationToken requestAborted)
|
public async Task<IActionResult> Edit(
|
||||||
|
[FromServices] IMemoryCache cache,
|
||||||
|
Album album,
|
||||||
|
CancellationToken requestAborted)
|
||||||
{
|
{
|
||||||
if (ModelState.IsValid)
|
if (ModelState.IsValid)
|
||||||
{
|
{
|
||||||
DbContext.Update(album);
|
DbContext.Update(album);
|
||||||
await DbContext.SaveChangesAsync(requestAborted);
|
await DbContext.SaveChangesAsync(requestAborted);
|
||||||
//Invalidate the cache entry as it is modified
|
//Invalidate the cache entry as it is modified
|
||||||
Cache.Remove(GetCacheKey(album.AlbumId));
|
cache.Remove(GetCacheKey(album.AlbumId));
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,7 +166,10 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
//
|
//
|
||||||
// POST: /StoreManager/RemoveAlbum/5
|
// POST: /StoreManager/RemoveAlbum/5
|
||||||
[HttpPost, ActionName("RemoveAlbum")]
|
[HttpPost, ActionName("RemoveAlbum")]
|
||||||
public async Task<IActionResult> RemoveAlbumConfirmed(int id, CancellationToken requestAborted)
|
public async Task<IActionResult> RemoveAlbumConfirmed(
|
||||||
|
[FromServices] IMemoryCache cache,
|
||||||
|
int id,
|
||||||
|
CancellationToken requestAborted)
|
||||||
{
|
{
|
||||||
var album = await DbContext.Albums.Where(a => a.AlbumId == id).FirstOrDefaultAsync();
|
var album = await DbContext.Albums.Where(a => a.AlbumId == id).FirstOrDefaultAsync();
|
||||||
if (album == null)
|
if (album == null)
|
||||||
|
|
@ -168,7 +180,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
DbContext.Albums.Remove(album);
|
DbContext.Albums.Remove(album);
|
||||||
await DbContext.SaveChangesAsync(requestAborted);
|
await DbContext.SaveChangesAsync(requestAborted);
|
||||||
//Remove the cache entry as it is removed
|
//Remove the cache entry as it is removed
|
||||||
Cache.Remove(GetCacheKey(id));
|
cache.Remove(GetCacheKey(id));
|
||||||
|
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,17 @@ namespace MusicStore.Controllers
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class AccountController : Controller
|
public class AccountController : Controller
|
||||||
{
|
{
|
||||||
|
public AccountController(
|
||||||
|
UserManager<ApplicationUser> userManager,
|
||||||
|
SignInManager<ApplicationUser> signInManager)
|
||||||
|
{
|
||||||
|
UserManager = userManager;
|
||||||
|
SignInManager = signInManager;
|
||||||
|
}
|
||||||
|
|
||||||
[FromServices]
|
public UserManager<ApplicationUser> UserManager { get; }
|
||||||
public UserManager<ApplicationUser> UserManager { get; set; }
|
|
||||||
|
|
||||||
[FromServices]
|
public SignInManager<ApplicationUser> SignInManager { get; }
|
||||||
public SignInManager<ApplicationUser> SignInManager { get; set; }
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /Account/Login
|
// GET: /Account/Login
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,6 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
private const string PromoCode = "FREE";
|
private const string PromoCode = "FREE";
|
||||||
|
|
||||||
[FromServices]
|
|
||||||
public MusicStoreContext DbContext { get; set; }
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /Checkout/
|
// GET: /Checkout/
|
||||||
public IActionResult AddressAndPayment()
|
public IActionResult AddressAndPayment()
|
||||||
|
|
@ -30,7 +27,10 @@ namespace MusicStore.Controllers
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> AddressAndPayment([FromForm] Order order, CancellationToken requestAborted)
|
public async Task<IActionResult> AddressAndPayment(
|
||||||
|
[FromServices] MusicStoreContext dbContext,
|
||||||
|
[FromForm] Order order,
|
||||||
|
CancellationToken requestAborted)
|
||||||
{
|
{
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
{
|
{
|
||||||
|
|
@ -52,14 +52,14 @@ namespace MusicStore.Controllers
|
||||||
order.OrderDate = DateTime.Now;
|
order.OrderDate = DateTime.Now;
|
||||||
|
|
||||||
//Add the Order
|
//Add the Order
|
||||||
DbContext.Orders.Add(order);
|
dbContext.Orders.Add(order);
|
||||||
|
|
||||||
//Process the order
|
//Process the order
|
||||||
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
var cart = ShoppingCart.GetCart(dbContext, HttpContext);
|
||||||
await cart.CreateOrder(order);
|
await cart.CreateOrder(order);
|
||||||
|
|
||||||
// Save all changes
|
// Save all changes
|
||||||
await DbContext.SaveChangesAsync(requestAborted);
|
await dbContext.SaveChangesAsync(requestAborted);
|
||||||
|
|
||||||
return RedirectToAction("Complete", new { id = order.OrderId });
|
return RedirectToAction("Complete", new { id = order.OrderId });
|
||||||
}
|
}
|
||||||
|
|
@ -74,10 +74,12 @@ namespace MusicStore.Controllers
|
||||||
//
|
//
|
||||||
// GET: /Checkout/Complete
|
// GET: /Checkout/Complete
|
||||||
|
|
||||||
public async Task<IActionResult> Complete(int id)
|
public async Task<IActionResult> Complete(
|
||||||
|
[FromServices] MusicStoreContext dbContext,
|
||||||
|
int id)
|
||||||
{
|
{
|
||||||
// Validate customer owns this order
|
// Validate customer owns this order
|
||||||
bool isValid = await DbContext.Orders.AnyAsync(
|
bool isValid = await dbContext.Orders.AnyAsync(
|
||||||
o => o.OrderId == id &&
|
o => o.OrderId == id &&
|
||||||
o.Username == HttpContext.User.GetUserName());
|
o.Username == HttpContext.User.GetUserName());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,28 +11,24 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
public class HomeController : Controller
|
public class HomeController : Controller
|
||||||
{
|
{
|
||||||
[FromServices]
|
|
||||||
public MusicStoreContext DbContext { get; set; }
|
|
||||||
|
|
||||||
[FromServices]
|
|
||||||
public IMemoryCache Cache { get; set; }
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /Home/
|
// GET: /Home/
|
||||||
public async Task<IActionResult> Index()
|
public async Task<IActionResult> Index(
|
||||||
|
[FromServices] MusicStoreContext dbContext,
|
||||||
|
[FromServices] IMemoryCache cache)
|
||||||
{
|
{
|
||||||
// Get most popular albums
|
// Get most popular albums
|
||||||
var cacheKey = "topselling";
|
var cacheKey = "topselling";
|
||||||
List<Album> albums;
|
List<Album> albums;
|
||||||
if(!Cache.TryGetValue(cacheKey, out albums))
|
if (!cache.TryGetValue(cacheKey, out albums))
|
||||||
{
|
{
|
||||||
albums = await GetTopSellingAlbumsAsync(6);
|
albums = await GetTopSellingAlbumsAsync(dbContext, 6);
|
||||||
|
|
||||||
if (albums != null && albums.Count > 0)
|
if (albums != null && albums.Count > 0)
|
||||||
{
|
{
|
||||||
// Refresh it every 10 minutes.
|
// Refresh it every 10 minutes.
|
||||||
// Let this be the last item to be removed by cache if cache GC kicks in.
|
// Let this be the last item to be removed by cache if cache GC kicks in.
|
||||||
Cache.Set(
|
cache.Set(
|
||||||
cacheKey,
|
cacheKey,
|
||||||
albums,
|
albums,
|
||||||
new MemoryCacheEntryOptions()
|
new MemoryCacheEntryOptions()
|
||||||
|
|
@ -59,13 +55,13 @@ namespace MusicStore.Controllers
|
||||||
return View("~/Views/Shared/AccessDenied.cshtml");
|
return View("~/Views/Shared/AccessDenied.cshtml");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<List<Album>> GetTopSellingAlbumsAsync(int count)
|
private async Task<List<Album>> GetTopSellingAlbumsAsync(MusicStoreContext dbContext, int count)
|
||||||
{
|
{
|
||||||
// Group the order details by album and return
|
// Group the order details by album and return
|
||||||
// the albums with the highest count
|
// the albums with the highest count
|
||||||
|
|
||||||
// TODO [EF] We don't query related data as yet, so the OrderByDescending isn't doing anything
|
// TODO [EF] We don't query related data as yet, so the OrderByDescending isn't doing anything
|
||||||
return await DbContext.Albums
|
return await dbContext.Albums
|
||||||
.OrderByDescending(a => a.OrderDetails.Count())
|
.OrderByDescending(a => a.OrderDetails.Count())
|
||||||
.Take(count)
|
.Take(count)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
|
||||||
|
|
@ -8,18 +8,20 @@ using MusicStore.Models;
|
||||||
|
|
||||||
namespace MusicStore.Controllers
|
namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Summary description for ManageController
|
|
||||||
/// </summary>
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class ManageController : Controller
|
public class ManageController : Controller
|
||||||
{
|
{
|
||||||
|
public ManageController(
|
||||||
[FromServices]
|
UserManager<ApplicationUser> userManager,
|
||||||
public UserManager<ApplicationUser> UserManager { get; set; }
|
SignInManager<ApplicationUser> signInManager)
|
||||||
|
{
|
||||||
|
UserManager = userManager;
|
||||||
|
SignInManager = signInManager;
|
||||||
|
}
|
||||||
|
|
||||||
[FromServices]
|
public UserManager<ApplicationUser> UserManager { get; }
|
||||||
public SignInManager<ApplicationUser> SignInManager { get; set; }
|
|
||||||
|
public SignInManager<ApplicationUser> SignInManager { get; }
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /Manage/Index
|
// GET: /Manage/Index
|
||||||
|
|
@ -349,7 +351,7 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
return await UserManager.FindByIdAsync(HttpContext.User.GetUserId());
|
return await UserManager.FindByIdAsync(HttpContext.User.GetUserId());
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -12,11 +12,12 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
public class ShoppingCartController : Controller
|
public class ShoppingCartController : Controller
|
||||||
{
|
{
|
||||||
[FromServices]
|
public ShoppingCartController(MusicStoreContext dbContext)
|
||||||
public MusicStoreContext DbContext { get; set; }
|
{
|
||||||
|
DbContext = dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
[FromServices]
|
public MusicStoreContext DbContext { get; }
|
||||||
public IAntiforgery Antiforgery { get; set; }
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /ShoppingCart/
|
// GET: /ShoppingCart/
|
||||||
|
|
@ -58,7 +59,10 @@ namespace MusicStore.Controllers
|
||||||
//
|
//
|
||||||
// AJAX: /ShoppingCart/RemoveFromCart/5
|
// AJAX: /ShoppingCart/RemoveFromCart/5
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> RemoveFromCart(int id, CancellationToken requestAborted)
|
public async Task<IActionResult> RemoveFromCart(
|
||||||
|
[FromServices] IAntiforgery antiforgery,
|
||||||
|
int id,
|
||||||
|
CancellationToken requestAborted)
|
||||||
{
|
{
|
||||||
var cookieToken = string.Empty;
|
var cookieToken = string.Empty;
|
||||||
var formToken = string.Empty;
|
var formToken = string.Empty;
|
||||||
|
|
@ -75,7 +79,7 @@ namespace MusicStore.Controllers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Antiforgery.ValidateTokens(HttpContext, new AntiforgeryTokenSet(formToken, cookieToken));
|
antiforgery.ValidateTokens(HttpContext, new AntiforgeryTokenSet(formToken, cookieToken));
|
||||||
|
|
||||||
// Retrieve the current user's shopping cart
|
// Retrieve the current user's shopping cart
|
||||||
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,12 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
public class StoreController : Controller
|
public class StoreController : Controller
|
||||||
{
|
{
|
||||||
[FromServices]
|
public StoreController(MusicStoreContext dbContext)
|
||||||
public MusicStoreContext DbContext { get; set; }
|
{
|
||||||
|
DbContext = dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
[FromServices]
|
public MusicStoreContext DbContext { get; }
|
||||||
public IMemoryCache Cache { get; set; }
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /Store/
|
// GET: /Store/
|
||||||
|
|
@ -43,11 +44,13 @@ namespace MusicStore.Controllers
|
||||||
return View(genreModel);
|
return View(genreModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> Details(int id)
|
public async Task<IActionResult> Details(
|
||||||
|
[FromServices] IMemoryCache cache,
|
||||||
|
int id)
|
||||||
{
|
{
|
||||||
var cacheKey = string.Format("album_{0}", id);
|
var cacheKey = string.Format("album_{0}", id);
|
||||||
Album album;
|
Album album;
|
||||||
if(!Cache.TryGetValue(cacheKey, out album))
|
if (!cache.TryGetValue(cacheKey, out album))
|
||||||
{
|
{
|
||||||
album = await DbContext.Albums
|
album = await DbContext.Albums
|
||||||
.Where(a => a.AlbumId == id)
|
.Where(a => a.AlbumId == id)
|
||||||
|
|
@ -58,7 +61,7 @@ namespace MusicStore.Controllers
|
||||||
if (album != null)
|
if (album != null)
|
||||||
{
|
{
|
||||||
//Remove it from cache if not retrieved in last 10 minutes
|
//Remove it from cache if not retrieved in last 10 minutes
|
||||||
Cache.Set(
|
cache.Set(
|
||||||
cacheKey,
|
cacheKey,
|
||||||
album,
|
album,
|
||||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue