Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #100 from tommiller85/master

Use a dynamic dictionary for settings rather than properties for each.
  • Loading branch information...
commit 4c2cf27072ec826423f31fa0fe7bf37a2df55fd5 2 parents 9a6536b + 0352581
@shiftkey-tester shiftkey-tester authored
View
1  src/Ideastrike.Nancy/Ideastrike.Nancy.csproj
@@ -236,6 +236,7 @@
<Compile Include="Modules\HomeModule.cs" />
<Compile Include="Modules\IdeaModule.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Models\Settings.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
View
4 src/Ideastrike.Nancy/IdeastrikeBootstrapper.cs
@@ -56,6 +56,10 @@ protected override void ConfigureApplicationContainer(ILifetimeScope existingCon
.AsImplementedInterfaces()
.SingleInstance();
+ builder.RegisterType<Settings>()
+ .AsSelf()
+ .InstancePerDependency();
+
builder.Update(existingContainer.ComponentRegistry);
}
View
1  src/Ideastrike.Nancy/Models/Repositories/SettingsRepository.cs
@@ -5,7 +5,6 @@ namespace Ideastrike.Nancy.Models.Repositories
{
public class SettingsRepository : ISettingsRepository
{
-
private readonly IdeastrikeContext db;
public SettingsRepository(IdeastrikeContext db)
View
43 src/Ideastrike.Nancy/Models/Settings.cs
@@ -0,0 +1,43 @@
+using System.Dynamic;
+
+namespace Ideastrike.Nancy.Models
+{
+ /// <summary>
+ /// A dynamic wrapper around an ISettingsRepository for retrieving, adding and updating settings.
+ /// </summary>
+ public class Settings : DynamicObject
+ {
+ private readonly ISettingsRepository _settingsRepository;
+
+ public Settings(ISettingsRepository settingsRepository)
+ {
+ _settingsRepository = settingsRepository;
+ }
+
+ public override bool TryGetMember(GetMemberBinder binder, out object result)
+ {
+ var key = binder.Name;
+ result = GetSetting(key);
+
+ return true;
+ }
+
+ private string GetSetting(string key)
+ {
+ return _settingsRepository.Get(key);
+ }
+
+ public override bool TrySetMember(SetMemberBinder binder, object value)
+ {
+ var key = binder.Name;
+ AddOrUpdateSetting(key, (string)value);
+
+ return true;
+ }
+
+ private void AddOrUpdateSetting(string key, string value)
+ {
+ _settingsRepository.Set(key, value);
+ }
+ }
+}
View
64 src/Ideastrike.Nancy/Modules/AdminModule.cs
@@ -1,22 +1,22 @@
using System;
+using System.Collections.Generic;
using System.Linq;
+using System.Net;
using Ideastrike.Nancy.Models;
using Ideastrike.Nancy.Models.Repositories;
using Nancy;
using Nancy.Security;
-using System.Collections.Generic;
-using System.Net;
using Newtonsoft.Json;
namespace Ideastrike.Nancy.Modules
{
public class AdminModule : NancyModule
{
- private ISettingsRepository _settings;
+ private dynamic _settings;
private IUserRepository _users;
private IIdeaRepository _ideas;
private IActivityRepository _activities;
- public AdminModule(IdeastrikeContext dbContext, ISettingsRepository settings, IUserRepository users, IIdeaRepository ideas, IActivityRepository activities)
+ public AdminModule(IdeastrikeContext dbContext, Settings settings, IUserRepository users, IIdeaRepository ideas, IActivityRepository activities)
: base("/admin")
{
this.RequiresAuthentication();
@@ -29,56 +29,56 @@ public AdminModule(IdeastrikeContext dbContext, ISettingsRepository settings, IU
Get["/"] = _ =>
{
- var m = Context.Model(string.Format("Admin - {0}", settings.SiteTitle));
- m.Name = settings.Name;
- m.WelcomeMessage = settings.WelcomeMessage;
- m.HomePage = settings.HomePage;
- m.GAnalyticsKey = settings.GAnalyticsKey;
+ var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle));
+ m.Name = _settings.Name;
+ m.WelcomeMessage = _settings.WelcomeMessage;
+ m.HomePage = _settings.HomePage;
+ m.GAnalyticsKey = _settings.GAnalyticsKey;
return View["Admin/Index", m];
};
Get["/users"] = _ =>
{
- var m = Context.Model(string.Format("Admin - {0}", settings.SiteTitle));
- m.Name = settings.Name;
- m.WelcomeMessage = settings.WelcomeMessage;
- m.HomePage = settings.HomePage;
- m.GAnalyticsKey = settings.GAnalyticsKey;
+ var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle));
+ m.Name = _settings.Name;
+ m.WelcomeMessage = _settings.WelcomeMessage;
+ m.HomePage = _settings.HomePage;
+ m.GAnalyticsKey = _settings.GAnalyticsKey;
m.Users = users.GetAll();
return View["Admin/Users", m];
};
Get["/moderation"] = _ =>
{
- var m = Context.Model(string.Format("Admin - {0}", settings.SiteTitle));
- m.Name = settings.Name;
- m.WelcomeMessage = settings.WelcomeMessage;
- m.HomePage = settings.HomePage;
- m.GAnalyticsKey = settings.GAnalyticsKey;
+ var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle));
+ m.Name = _settings.Name;
+ m.WelcomeMessage = _settings.WelcomeMessage;
+ m.HomePage = _settings.HomePage;
+ m.GAnalyticsKey = _settings.GAnalyticsKey;
return View["Admin/Moderation", m];
};
Get["/settings"] = _ =>
{
- var m = Context.Model(string.Format("Admin - {0}", settings.SiteTitle));
- m.SiteTitle = settings.SiteTitle;
- m.Name = settings.Name;
- m.WelcomeMessage = settings.WelcomeMessage;
- m.HomePage = settings.HomePage;
- m.GAnalyticsKey = settings.GAnalyticsKey;
- m.MaxThumbnailWidth = settings.MaxThumbnailWidth;
+ var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle));
+ m.Name = _settings.Name;
+ m.SiteTitle = _settings.SiteTitle;
+ m.WelcomeMessage = _settings.WelcomeMessage;
+ m.HomePage = _settings.HomePage;
+ m.GAnalyticsKey = _settings.GAnalyticsKey;
+ m.MaxThumbnailWidth = _settings.MaxThumbnailWidth;
return View["Admin/Settings", m];
};
Post["/settings"] = _ =>
{
- settings.WelcomeMessage = Request.Form.welcomemessage;
- settings.SiteTitle = Request.Form.sitetitle;
- settings.Name = Request.Form.yourname;
- settings.HomePage = Request.Form.homepage;
- settings.GAnalyticsKey = Request.Form.analyticskey;
- settings.MaxThumbnailWidth = Request.Form.maxthumbnailwidth;
+ _settings.WelcomeMessage = Request.Form.welcomemessage;
+ _settings.SiteTitle = Request.Form.sitetitle;
+ _settings.Name = Request.Form.yourname;
+ _settings.HomePage = Request.Form.homepage;
+ _settings.GAnalyticsKey = Request.Form.analyticskey;
+ _settings.MaxThumbnailWidth = Request.Form.maxthumbnailwidth;
return Response.AsRedirect("/admin/settings");
};
View
12 src/Ideastrike.Nancy/Modules/ApiModule.Secured.cs
@@ -1,16 +1,20 @@
using System;
-using System.Linq;
using Ideastrike.Nancy.Models;
using Ideastrike.Nancy.Models.Repositories;
using Nancy;
using Nancy.ModelBinding;
-using Nancy.Security;
namespace Ideastrike.Nancy.Modules
{
public class ApiSecuredModule : NancyModule
{
- public ApiSecuredModule(IIdeaRepository ideas, ISettingsRepository settings) : base("/api") {
+ private dynamic _settings;
+
+ public ApiSecuredModule(IIdeaRepository ideas, Settings settings) : base("/api")
+ {
+
+ _settings = settings;
+
this.Before.AddItemToEndOfPipeline(ctx => {
if (ctx.CurrentUser == null)
return HttpStatusCode.Unauthorized;
@@ -25,7 +29,7 @@ public class ApiSecuredModule : NancyModule
Description = model.description,
Time = DateTime.UtcNow,
Author = (User)Context.CurrentUser,
- Status = settings.IdeaStatusDefault
+ Status = _settings.IdeaStatusDefault
};
ideas.Add(idea);
View
11 src/Ideastrike.Nancy/Modules/ApiModule.cs
@@ -1,12 +1,12 @@
using System;
using System.Data.Objects.SqlClient;
+using System.IO;
using System.Linq;
+using Ideastrike.Nancy.Helpers;
using Ideastrike.Nancy.Models;
using Ideastrike.Nancy.Models.Repositories;
using Nancy;
using Newtonsoft.Json;
-using System.IO;
-using Ideastrike.Nancy.Helpers;
namespace Ideastrike.Nancy.Modules
{
@@ -16,8 +16,13 @@ namespace Ideastrike.Nancy.Modules
// make unannounced changes to the public surface.
public class ApiModule : NancyModule
{
- public ApiModule(IdeastrikeContext db, IIdeaRepository ideas, IUserRepository users, ISettingsRepository settings)
+ private dynamic _settings;
+
+ public ApiModule(IdeastrikeContext db, IIdeaRepository ideas, IUserRepository users, Settings settings)
: base("/api") {
+
+ _settings = settings;
+
Get["/ideas"] = _ => {
return Response.AsJson(db.Ideas.Select(idea =>
new {
View
6 src/Ideastrike.Nancy/Modules/HomeModule.cs
@@ -11,9 +11,9 @@ public class HomeModule : NancyModule
{
private readonly IIdeaRepository _ideas;
private readonly IUserRepository _users;
- private readonly ISettingsRepository _settings;
+ private readonly dynamic _settings;
- public HomeModule(IIdeaRepository ideas, IUserRepository users, ISettingsRepository settings)
+ public HomeModule(IIdeaRepository ideas, IUserRepository users, Settings settings)
{
_ideas = ideas;
_users = users;
@@ -43,7 +43,7 @@ public Response ListIdeas(IEnumerable<Idea> ideas, SelectedTab selected, string
}
- var m = Context.Model(_settings.SiteTitle);
+ var m = Context.Model((string)_settings.SiteTitle);
m.Name = _settings.Name;
m.WelcomeMessage = _settings.WelcomeMessage;
m.Ideas = ideas;
View
10 src/Ideastrike.Nancy/Modules/IdeaModule.Secured.cs
@@ -14,10 +14,10 @@ public class IdeaSecuredModule : NancyModule
{
private readonly IIdeaRepository _ideas;
private readonly IUserRepository _users;
- private readonly ISettingsRepository _settings;
+ private readonly dynamic _settings;
private readonly IImageRepository _imageRepository;
- public IdeaSecuredModule(IIdeaRepository ideas, IUserRepository users, ISettingsRepository settings, IImageRepository imageRepository)
+ public IdeaSecuredModule(IIdeaRepository ideas, IUserRepository users, Settings settings, IImageRepository imageRepository)
: base("/idea")
{
_ideas = ideas;
@@ -29,7 +29,7 @@ public IdeaSecuredModule(IIdeaRepository ideas, IUserRepository users, ISettings
Get["/new"] = _ =>
{
- var m = Context.Model(string.Format("New Idea - {0}", _settings.SiteTitle));
+ var m = Context.Model(string.Format("New Idea - {0}", (string)_settings.SiteTitle));
m.Ideas = _ideas.GetAll();
m.Errors = false;
@@ -46,7 +46,7 @@ public IdeaSecuredModule(IIdeaRepository ideas, IUserRepository users, ISettings
int id = parameters.id;
var idea = _ideas.Get(id);
- var m = Context.Model(string.Format(Strings.IdeaSecuredModule_EditIdea, idea.Title, _settings.SiteTitle));
+ var m = Context.Model(string.Format(Strings.IdeaSecuredModule_EditIdea, idea.Title, (string)_settings.SiteTitle));
m.PopularIdeas = _ideas.GetAll();
m.Idea = idea;
m.StatusChoices = _settings.IdeaStatusChoices.Split(',');
@@ -116,7 +116,7 @@ public IdeaSecuredModule(IIdeaRepository ideas, IUserRepository users, ISettings
Time = DateTime.UtcNow,
Title = Request.Form.Title,
Description = Request.Form.Description,
- Status = settings.IdeaStatusDefault
+ Status = _settings.IdeaStatusDefault
};
IEnumerable<string> keys = Context.Request.Form;
View
10 src/Ideastrike.Nancy/Modules/IdeaModule.cs
@@ -13,8 +13,8 @@ public class IdeaModule : NancyModule
{
private readonly IIdeaRepository _ideas;
private readonly IUserRepository _users;
- private readonly ISettingsRepository _settings;
- public IdeaModule(IIdeaRepository ideas, IUserRepository users, ISettingsRepository settings, IImageRepository imageRepository)
+ private readonly dynamic _settings;
+ public IdeaModule(IIdeaRepository ideas, IUserRepository users, Settings settings, IImageRepository imageRepository)
: base("/idea")
{
_ideas = ideas;
@@ -37,7 +37,7 @@ public IdeaModule(IIdeaRepository ideas, IUserRepository users, ISettingsReposit
}
var viewModel = new IdeaViewModel(idea);
- var model = Context.Model(string.Format("{0} - {1}", idea.Title, _settings.SiteTitle));
+ var model = Context.Model(string.Format("{0} - {1}", idea.Title, (string)_settings.SiteTitle));
model.Idea = viewModel;
return View["Idea/Index", model];
};
@@ -77,9 +77,9 @@ public IdeaModule(IIdeaRepository ideas, IUserRepository users, ISettingsReposit
{
var drawingImage = System.Drawing.Image.FromStream(memoryStream);
int thumbWidth = (int)parameters.width;
- if (thumbWidth > settings.MaxThumbnailWidth)
+ if (thumbWidth > int.Parse(_settings.MaxThumbnailWidth))
{
- thumbWidth = settings.MaxThumbnailWidth;
+ thumbWidth = int.Parse(_settings.MaxThumbnailWidth);
}
var thumb = drawingImage.ToThumbnail(thumbWidth);
using (var thumbnailStream = new MemoryStream())
View
2  tests/IdeaStrike.Tests/IdeaModuleTests/when_viewing_an_image_thumbnail_with_width_greater_than_max.cs
@@ -22,7 +22,7 @@ public when_viewing_an_image_thumbnail_with_width_greater_than_max()
IdeaId = 1
};
_Images.Setup(i => i.Get(1)).Returns(testImage);
- _Settings.SetupGet(s => s.MaxThumbnailWidth).Returns(1000);
+ _Settings.Setup(s => s.Get("MaxThumbnailWidth")).Returns("1000");
Get("/idea/imagethumb/1/5000000");
}
View
2  tests/IdeaStrike.Tests/IdeaModuleTests/when_viewing_an_image_thumbnail_with_width_less_than_max.cs
@@ -22,7 +22,7 @@ public when_viewing_an_image_thumbnail_with_width_less_than_max()
IdeaId = 1
};
_Images.Setup(i => i.Get(1)).Returns(testImage);
- _Settings.SetupGet(s => s.MaxThumbnailWidth).Returns(1000);
+ _Settings.Setup(s => s.Get("MaxThumbnailWidth")).Returns("1000");
Get("/idea/imagethumb/1/500");
}
Please sign in to comment.
Something went wrong with that request. Please try again.