Reacting to FromServices changes
This commit is contained in:
parent
9d3ceb66e5
commit
5a3c4957d9
|
|
@ -17,11 +17,12 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
[Authorize("ManageStore")]
|
||||
public class StoreManagerController : Controller
|
||||
{
|
||||
[FromServices]
|
||||
public MusicStoreContext DbContext { get; set; }
|
||||
public StoreManagerController(MusicStoreContext dbContext)
|
||||
{
|
||||
DbContext = dbContext;
|
||||
}
|
||||
|
||||
[FromServices]
|
||||
public IMemoryCache Cache { get; set; }
|
||||
public MusicStoreContext DbContext { get; }
|
||||
|
||||
//
|
||||
// GET: /StoreManager/
|
||||
|
|
@ -37,12 +38,14 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
|
||||
//
|
||||
// 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);
|
||||
|
||||
Album album;
|
||||
if(!Cache.TryGetValue(cacheKey, out album))
|
||||
if (!cache.TryGetValue(cacheKey, out album))
|
||||
{
|
||||
album = await DbContext.Albums
|
||||
.Where(a => a.AlbumId == id)
|
||||
|
|
@ -53,7 +56,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
if (album != null)
|
||||
{
|
||||
//Remove it from cache if not retrieved in last 10 minutes.
|
||||
Cache.Set(
|
||||
cache.Set(
|
||||
cacheKey,
|
||||
album,
|
||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||
|
|
@ -62,7 +65,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
|
||||
if (album == null)
|
||||
{
|
||||
Cache.Remove(cacheKey);
|
||||
cache.Remove(cacheKey);
|
||||
return HttpNotFound();
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +84,10 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
// POST: /StoreManager/Create
|
||||
[HttpPost]
|
||||
[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)
|
||||
{
|
||||
|
|
@ -94,7 +100,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
Url = Url.Action("Details", "Store", new { id = album.AlbumId })
|
||||
};
|
||||
|
||||
Cache.Remove("latestAlbum");
|
||||
cache.Remove("latestAlbum");
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
|
|
@ -125,14 +131,17 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
// POST: /StoreManager/Edit/5
|
||||
[HttpPost]
|
||||
[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)
|
||||
{
|
||||
DbContext.Update(album);
|
||||
await DbContext.SaveChangesAsync(requestAborted);
|
||||
//Invalidate the cache entry as it is modified
|
||||
Cache.Remove(GetCacheKey(album.AlbumId));
|
||||
cache.Remove(GetCacheKey(album.AlbumId));
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
|
|
@ -157,7 +166,10 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
//
|
||||
// POST: /StoreManager/RemoveAlbum/5
|
||||
[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();
|
||||
if (album == null)
|
||||
|
|
@ -168,7 +180,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
DbContext.Albums.Remove(album);
|
||||
await DbContext.SaveChangesAsync(requestAborted);
|
||||
//Remove the cache entry as it is removed
|
||||
Cache.Remove(GetCacheKey(id));
|
||||
cache.Remove(GetCacheKey(id));
|
||||
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,12 +14,17 @@ namespace MusicStore.Controllers
|
|||
[Authorize]
|
||||
public class AccountController : Controller
|
||||
{
|
||||
public AccountController(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
SignInManager<ApplicationUser> signInManager)
|
||||
{
|
||||
UserManager = userManager;
|
||||
SignInManager = signInManager;
|
||||
}
|
||||
|
||||
[FromServices]
|
||||
public UserManager<ApplicationUser> UserManager { get; set; }
|
||||
public UserManager<ApplicationUser> UserManager { get; }
|
||||
|
||||
[FromServices]
|
||||
public SignInManager<ApplicationUser> SignInManager { get; set; }
|
||||
public SignInManager<ApplicationUser> SignInManager { get; }
|
||||
|
||||
//
|
||||
// GET: /Account/Login
|
||||
|
|
|
|||
|
|
@ -15,9 +15,6 @@ namespace MusicStore.Controllers
|
|||
{
|
||||
private const string PromoCode = "FREE";
|
||||
|
||||
[FromServices]
|
||||
public MusicStoreContext DbContext { get; set; }
|
||||
|
||||
//
|
||||
// GET: /Checkout/
|
||||
public IActionResult AddressAndPayment()
|
||||
|
|
@ -30,7 +27,10 @@ namespace MusicStore.Controllers
|
|||
|
||||
[HttpPost]
|
||||
[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)
|
||||
{
|
||||
|
|
@ -52,14 +52,14 @@ namespace MusicStore.Controllers
|
|||
order.OrderDate = DateTime.Now;
|
||||
|
||||
//Add the Order
|
||||
DbContext.Orders.Add(order);
|
||||
dbContext.Orders.Add(order);
|
||||
|
||||
//Process the order
|
||||
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
||||
var cart = ShoppingCart.GetCart(dbContext, HttpContext);
|
||||
await cart.CreateOrder(order);
|
||||
|
||||
// Save all changes
|
||||
await DbContext.SaveChangesAsync(requestAborted);
|
||||
await dbContext.SaveChangesAsync(requestAborted);
|
||||
|
||||
return RedirectToAction("Complete", new { id = order.OrderId });
|
||||
}
|
||||
|
|
@ -74,10 +74,12 @@ namespace MusicStore.Controllers
|
|||
//
|
||||
// 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
|
||||
bool isValid = await DbContext.Orders.AnyAsync(
|
||||
bool isValid = await dbContext.Orders.AnyAsync(
|
||||
o => o.OrderId == id &&
|
||||
o.Username == HttpContext.User.GetUserName());
|
||||
|
||||
|
|
|
|||
|
|
@ -11,28 +11,24 @@ namespace MusicStore.Controllers
|
|||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
[FromServices]
|
||||
public MusicStoreContext DbContext { get; set; }
|
||||
|
||||
[FromServices]
|
||||
public IMemoryCache Cache { get; set; }
|
||||
|
||||
//
|
||||
// GET: /Home/
|
||||
public async Task<IActionResult> Index()
|
||||
public async Task<IActionResult> Index(
|
||||
[FromServices] MusicStoreContext dbContext,
|
||||
[FromServices] IMemoryCache cache)
|
||||
{
|
||||
// Get most popular albums
|
||||
var cacheKey = "topselling";
|
||||
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)
|
||||
{
|
||||
// Refresh it every 10 minutes.
|
||||
// Let this be the last item to be removed by cache if cache GC kicks in.
|
||||
Cache.Set(
|
||||
cache.Set(
|
||||
cacheKey,
|
||||
albums,
|
||||
new MemoryCacheEntryOptions()
|
||||
|
|
@ -59,13 +55,13 @@ namespace MusicStore.Controllers
|
|||
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
|
||||
// the albums with the highest count
|
||||
|
||||
// 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())
|
||||
.Take(count)
|
||||
.ToListAsync();
|
||||
|
|
|
|||
|
|
@ -8,18 +8,20 @@ using MusicStore.Models;
|
|||
|
||||
namespace MusicStore.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for ManageController
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
public class ManageController : Controller
|
||||
{
|
||||
|
||||
[FromServices]
|
||||
public UserManager<ApplicationUser> UserManager { get; set; }
|
||||
public ManageController(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
SignInManager<ApplicationUser> signInManager)
|
||||
{
|
||||
UserManager = userManager;
|
||||
SignInManager = signInManager;
|
||||
}
|
||||
|
||||
[FromServices]
|
||||
public SignInManager<ApplicationUser> SignInManager { get; set; }
|
||||
public UserManager<ApplicationUser> UserManager { get; }
|
||||
|
||||
public SignInManager<ApplicationUser> SignInManager { get; }
|
||||
|
||||
//
|
||||
// GET: /Manage/Index
|
||||
|
|
@ -349,7 +351,7 @@ namespace MusicStore.Controllers
|
|||
{
|
||||
return await UserManager.FindByIdAsync(HttpContext.User.GetUserId());
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
@ -12,11 +12,12 @@ namespace MusicStore.Controllers
|
|||
{
|
||||
public class ShoppingCartController : Controller
|
||||
{
|
||||
[FromServices]
|
||||
public MusicStoreContext DbContext { get; set; }
|
||||
public ShoppingCartController(MusicStoreContext dbContext)
|
||||
{
|
||||
DbContext = dbContext;
|
||||
}
|
||||
|
||||
[FromServices]
|
||||
public IAntiforgery Antiforgery { get; set; }
|
||||
public MusicStoreContext DbContext { get; }
|
||||
|
||||
//
|
||||
// GET: /ShoppingCart/
|
||||
|
|
@ -58,7 +59,10 @@ namespace MusicStore.Controllers
|
|||
//
|
||||
// AJAX: /ShoppingCart/RemoveFromCart/5
|
||||
[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 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
|
||||
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
|
||||
|
|
|
|||
|
|
@ -10,11 +10,12 @@ namespace MusicStore.Controllers
|
|||
{
|
||||
public class StoreController : Controller
|
||||
{
|
||||
[FromServices]
|
||||
public MusicStoreContext DbContext { get; set; }
|
||||
public StoreController(MusicStoreContext dbContext)
|
||||
{
|
||||
DbContext = dbContext;
|
||||
}
|
||||
|
||||
[FromServices]
|
||||
public IMemoryCache Cache { get; set; }
|
||||
public MusicStoreContext DbContext { get; }
|
||||
|
||||
//
|
||||
// GET: /Store/
|
||||
|
|
@ -43,11 +44,13 @@ namespace MusicStore.Controllers
|
|||
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);
|
||||
Album album;
|
||||
if(!Cache.TryGetValue(cacheKey, out album))
|
||||
if (!cache.TryGetValue(cacheKey, out album))
|
||||
{
|
||||
album = await DbContext.Albums
|
||||
.Where(a => a.AlbumId == id)
|
||||
|
|
@ -58,7 +61,7 @@ namespace MusicStore.Controllers
|
|||
if (album != null)
|
||||
{
|
||||
//Remove it from cache if not retrieved in last 10 minutes
|
||||
Cache.Set(
|
||||
cache.Set(
|
||||
cacheKey,
|
||||
album,
|
||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||
|
|
|
|||
Loading…
Reference in New Issue