Skip to content

Commit

Permalink
Rewrite HomeController/Index
Browse files Browse the repository at this point in the history
  • Loading branch information
dcomartin committed Nov 3, 2016
1 parent 87c7bca commit 771a535
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 47 deletions.
4 changes: 3 additions & 1 deletion MusicStore.sln.DotSettings
Expand Up @@ -105,4 +105,6 @@ See License.txt in the project root for license information</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EJavaScript_002ECodeStyle_002ESettingsUpgrade_002EJsCodeFormatterSettingsUpgrader/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EJavaScript_002ECodeStyle_002ESettingsUpgrade_002EJsCodeFormatterSettingsUpgrader/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EJavaScript_002ECodeStyle_002ESettingsUpgrade_002EJsParsFormattingSettingsUpgrader/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EJavaScript_002ECodeStyle_002ESettingsUpgrade_002EJsWrapperSettingsUpgrader/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
1 change: 1 addition & 0 deletions samples/MusicStore.Standalone/project.json
Expand Up @@ -44,6 +44,7 @@
}
},
"dependencies": {
"MediatR": "2.1.0",
"Microsoft.AspNetCore.Authentication.Cookies": "1.1.0-*",
"Microsoft.AspNetCore.Authentication.Facebook": "1.1.0-*",
"Microsoft.AspNetCore.Authentication.Google": "1.1.0-*",
Expand Down
45 changes: 10 additions & 35 deletions samples/MusicStore/Controllers/HomeController.cs
Expand Up @@ -2,54 +2,29 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using MusicStore.Features;
using MusicStore.Models;

namespace MusicStore.Controllers
{
public class HomeController : Controller
{
private readonly AppSettings _appSettings;
private readonly IMediator _mediator;

public HomeController(IOptions<AppSettings> options)
public HomeController(IMediator mediator)
{
_appSettings = options.Value;
_mediator = mediator;
}
//
// GET: /Home/
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))
{
albums = await GetTopSellingAlbumsAsync(dbContext, 6);

if (albums != null && albums.Count > 0)
{
if (_appSettings.CacheDbResults)
{
// Refresh it every 10 minutes.
// Let this be the last item to be removed by cache if cache GC kicks in.
cache.Set(
cacheKey,
albums,
new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(10))
.SetPriority(CacheItemPriority.High));
}
}
}

return View(albums);
public async Task<IActionResult> Index()
{
var viewModel = await _mediator.SendAsync(new Home());
return View(viewModel);
}

public IActionResult Error()
{
return View("~/Views/Shared/Error.cshtml");
Expand Down
58 changes: 58 additions & 0 deletions samples/MusicStore/Features/Home.cs
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using MusicStore.Models;

namespace MusicStore.Features
{
public class Home : IAsyncRequest<List<Album>> { }

public class HomeHandler : IAsyncRequestHandler<Home, List<Album>>
{
private readonly MusicStoreContext _dbContext;
private readonly IMemoryCache _cache;
private readonly IOptions<AppSettings> _options;

public HomeHandler(MusicStoreContext dbContext, IMemoryCache cache, IOptions<AppSettings> options)
{
_dbContext = dbContext;
_cache = cache;
_options = options;
}

public async Task<List<Album>> Handle(Home message)
{
// Get most popular albums
var cacheKey = "topselling";
List<Album> albums;
if (!_cache.TryGetValue(cacheKey, out albums))
{
albums = await _dbContext.Albums
.OrderByDescending(a => a.OrderDetails.Count)
.Take(6)
.ToListAsync();

if (albums != null && albums.Count > 0)
{
if (_options.Value.CacheDbResults)
{
// Refresh it every 10 minutes.
// Let this be the last item to be removed by cache if cache GC kicks in.
_cache.Set(
cacheKey,
albums,
new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(10))
.SetPriority(CacheItemPriority.High));
}
}
}
return albums;
}
}
}
2 changes: 1 addition & 1 deletion samples/MusicStore/Models/ShoppingCart.cs
Expand Up @@ -166,7 +166,7 @@ public async Task<int> CreateOrder(Order order)
}

// We're using HttpContextBase to allow access to sessions.
private static string GetCartId(HttpContext context)
public static string GetCartId(HttpContext context)
{
var cartId = context.Session.GetString("Session");

Expand Down
5 changes: 5 additions & 0 deletions samples/MusicStore/Startup.cs
@@ -1,3 +1,4 @@
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
Expand All @@ -6,7 +7,10 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MusicStore.Components;
using MusicStore.Features;
using MusicStore.Models;
using MusicStore.ViewModels;
using System.Reflection;

namespace MusicStore
{
Expand Down Expand Up @@ -55,6 +59,7 @@ public void ConfigureServices(IServiceCollection services)
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();

services.AddMediatR(typeof(Startup).GetTypeInfo().Assembly);

services.AddCors(options =>
{
Expand Down
12 changes: 4 additions & 8 deletions samples/MusicStore/project.json
Expand Up @@ -33,29 +33,25 @@
]
},
"dependencies": {
"MediatR": "2.1.0",
"MediatR.Extensions.Microsoft.DependencyInjection": "1.0.1",
"Microsoft.AspNetCore": "1.1.0-*",
"Microsoft.AspNetCore.Authentication.Cookies": "1.1.0-*",
"Microsoft.AspNetCore.Authentication.Facebook": "1.1.0-*",
"Microsoft.AspNetCore.Authentication.Google": "1.1.0-*",
"Microsoft.AspNetCore.Authentication.MicrosoftAccount": "1.1.0-*",
"Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.1.0-*",
"Microsoft.AspNetCore.Authentication.Twitter": "1.1.0-*",
"Microsoft.AspNetCore.Diagnostics": "1.1.0-*",
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.1.0-*",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0-*",
"Microsoft.AspNetCore.Mvc": "1.1.0-*",
"Microsoft.AspNetCore.Mvc.TagHelpers": "1.1.0-*",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-*",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0-*",
"Microsoft.AspNetCore.Server.WebListener": "1.1.0-*",
"Microsoft.AspNetCore.Session": "1.1.0-*",
"Microsoft.AspNetCore.StaticFiles": "1.1.0-*",
"Microsoft.EntityFrameworkCore.InMemory": "1.1.0-*",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0-*",
"Microsoft.Extensions.Configuration.CommandLine": "1.1.0-*",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0-*",
"Microsoft.Extensions.Configuration.Json": "1.1.0-*",
"Microsoft.Extensions.Logging.Console": "1.1.0-*",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0-*"
"Microsoft.Extensions.Configuration.CommandLine": "1.1.0-*"
},
"frameworks": {
"net451": {},
Expand Down
2 changes: 1 addition & 1 deletion samples/MusicStore/web.config
Expand Up @@ -4,6 +4,6 @@
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true" />
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
</system.webServer>
</configuration>
2 changes: 1 addition & 1 deletion test/MusicStore.Test/HomeControllerTest.cs
Expand Up @@ -53,7 +53,7 @@ public async Task Index_GetsSixTopAlbums()
PopulateData(dbContext);

// Action
var result = await controller.Index(dbContext, cache);
var result = await controller.Index();

// Assert
var viewResult = Assert.IsType<ViewResult>(result);
Expand Down

0 comments on commit 771a535

Please sign in to comment.