Browse files

cleaned up notifications

fixed page reload in IE
  • Loading branch information...
1 parent 1168737 commit ccdfc6f2b68dd301130f43d619d13f774c1cf2e9 @jarrettmeyer committed Jan 26, 2010
View
4 src/Project.Core/Controllers/ApplicationController.cs
@@ -1,8 +1,10 @@
using System.Web.Mvc;
+using Project.Core.Lib.ActionFilters;
using Project.Core.Lib.Infrastructure;
namespace Project.Core.Controllers
{
+ [HandleErrorWithElmah, LogRequests]
public class ApplicationController : Controller
{
protected readonly IAppScope _appScope;
@@ -24,7 +26,7 @@ protected virtual void OnControllerCreated()
{
_appScope.AddInfo("Welcome. This appears to be your first visit!");
}
- _appScope.AddWarning("This site is currently being developed.");
+ _appScope.AddWarning("This site is currently being developed. Proceed with caution.");
}
}
}
View
13 src/Project.Core/Controllers/ContactsController.cs
@@ -20,24 +20,16 @@ public ContactsController(IRepository repository, IAppScope appScope)
public ActionResult Create(Contact contact)
{
_repository.Insert(contact);
- if (_appScope.IsXhr)
- {
- return PartialView("ShowPartial", contact);
- }
- _appScope.AddSuccess("Successfully created a new contact");
+ _appScope.AddSuccess("New contact was successfully created.");
return RedirectToAction("Index");
}
[AcceptVerbs(HttpVerbs.Delete), ValidateAntiForgeryToken]
public ActionResult Delete(int id)
{
var contact = _repository.FindById<Contact>(id);
- if (contact == null)
- {
- Log.BoundTo(this).WriteWarningMessage(string.Format("Attempted to delete contact with ID {0}", id));
- return Json(false);
- }
_repository.Delete(contact);
+ _appScope.AddSuccess("Contact successfully deleted.");
return Json(true);
}
@@ -79,6 +71,7 @@ public ActionResult Update(int id, Contact contact)
original.FirstName = contact.FirstName;
original.LastName = contact.LastName;
_repository.Commit();
+ _appScope.AddSuccess("Contact successfully updated.");
return RedirectToAction("Index");
}
}
View
11 src/Project.Core/Controllers/NotesController.cs
@@ -4,7 +4,6 @@
using Project.Core.Lib.Infrastructure;
using Project.Core.Models.Entities;
using Project.Core.Models.ViewModels;
-using Project.Core.Models.ViewModels.Notifications;
namespace Project.Core.Controllers
{
@@ -26,7 +25,7 @@ public ActionResult Create(int contactId, Note note)
note.DateAdded = DateTime.Now;
contact.AddNote(note);
_repository.Commit();
- _appScope.AddSuccess(string.Format("Successfully created new note for {0}", contact.EmailAddress));
+ _appScope.AddSuccess(string.Format("Note was successfully created for {0}", contact.EmailAddress));
return RedirectToAction("Index", new { contactId });
}
@@ -35,11 +34,8 @@ public ActionResult Delete(int id)
{
var note = _repository.FindById<Note>(id);
_repository.Delete(note);
- if (_appScope.IsXhr)
- {
- return Json(true);
- }
- return RedirectToAction("Index");
+ _appScope.AddSuccess("Note was successfully deleted.");
+ return Json(true);
}
public ActionResult Edit(int id)
@@ -73,6 +69,7 @@ public ActionResult Update(int id, Note note)
var original = _repository.FindById<Note>(id);
original.NoteText = note.NoteText;
_repository.Commit();
+ _appScope.AddSuccess("Note was successfully updated.");
return new EmptyResult();
}
}
View
3 src/Project.Core/Controllers/NotificationsController.cs
@@ -18,8 +18,7 @@ public ActionResult Index()
}
protected override void OnControllerCreated()
- {
- _appScope.AddWarning("Ack!");
+ {
}
}
}
View
30 src/Project.Core/Lib/ActionFilters/LogRequestsAttribute.cs
@@ -0,0 +1,30 @@
+using System.Web.Mvc;
+using Project.Core.Lib.Data;
+using Project.Core.Lib.Infrastructure;
+using Project.Core.Models.Entities;
+
+namespace Project.Core.Lib.ActionFilters
+{
+ public class LogRequestsAttribute : ActionFilterAttribute
+ {
+ private IRepository _repository;
+
+ public IRepository Repository
+ {
+ get
+ {
+ return _repository ?? (_repository = ServiceLocator.Resolve<IRepository>());
+ }
+ set
+ {
+ _repository = value;
+ }
+ }
+
+ public override void OnActionExecuting(ActionExecutingContext filterContext)
+ {
+ var pageRequest = PageRequest.Create(filterContext.HttpContext.Request);
+ Repository.Insert(pageRequest);
+ }
+ }
+}
View
7 src/Project.Core/Lib/Infrastructure/HttpAppScope.cs
@@ -48,8 +48,11 @@ private void AddNotification(INotification notification)
{
Ensure.ArgumentNotNull(notification, "notification");
var notifications = GetNotificationsInternal();
- notifications.Add(notification);
- SetNotfications(notifications);
+ if (!notifications.Contains(notification))
+ {
+ notifications.Add(notification);
+ SetNotfications(notifications);
+ }
}
public void AddSuccess(string success)
View
65 src/Project.Core/Models/Entities/PageRequest.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.Web;
+
+namespace Project.Core.Models.Entities
+{
+ public class PageRequest
+ {
+ public virtual int Id { get; set; }
+ public virtual string Path { get; set; }
+ public virtual string QueryString { get; set; }
+ public virtual string HttpMethod { get; set; }
+ public virtual bool IsLocal { get; set; }
+ public virtual bool IsSecure { get; set; }
+ //public virtual string User { get; set; }
+ public virtual string UserHostAddress { get; set; }
+ public virtual string UserHostName { get; set; }
+ public virtual string Browser { get; set; }
+ public virtual string BrowserId { get; set; }
+ public virtual string BrowserVersion { get; set; }
+ public virtual bool Crawler { get; set; }
+ public virtual bool IsMobile { get; set; }
+ public virtual bool AllowJs { get; set; }
+ public virtual int Height { get; set; }
+ public virtual int Width { get; set; }
+ public virtual string Platform { get; set; }
+ public virtual DateTime Time { get; set; }
+
+ private static string BuildQueryString(NameValueCollection collection)
+ {
+ var parts = new List<string>();
+ foreach (var key in collection.Keys)
+ {
+ parts.Add(string.Format("{0}={1}", key, collection[key.ToString()]));
+ }
+ return string.Join(Environment.NewLine, parts.ToArray());
+ }
+
+ public static PageRequest Create(HttpRequestBase request)
+ {
+ var pageRequest = new PageRequest();
+ pageRequest.Path = request.Path;
+ pageRequest.QueryString = BuildQueryString(request.QueryString);
+ pageRequest.HttpMethod = request.HttpMethod;
+ pageRequest.IsLocal = request.IsLocal;
+ pageRequest.IsSecure = request.IsSecureConnection;
+ //pageRequest.User = string.Empty;
+ pageRequest.UserHostAddress = request.UserHostAddress;
+ pageRequest.UserHostName = request.UserHostName;
+ pageRequest.Browser = request.Browser.Browser;
+ pageRequest.BrowserId = request.Browser.Id;
+ pageRequest.BrowserVersion = request.Browser.Version;
+ pageRequest.Crawler = request.Browser.Crawler;
+ pageRequest.IsMobile = request.Browser.IsMobileDevice;
+ pageRequest.AllowJs = request.Browser.SupportsXmlHttp;
+ pageRequest.Height = request.Browser.ScreenPixelsHeight;
+ pageRequest.Width = request.Browser.ScreenPixelsWidth;
+ pageRequest.Platform = request.Browser.Platform;
+ pageRequest.Time = DateTime.Now;
+ return pageRequest;
+ }
+
+ }
+}
View
30 src/Project.Core/Models/Mappings/PageRequestMap.cs
@@ -0,0 +1,30 @@
+using FluentNHibernate.Mapping;
+using Project.Core.Models.Entities;
+
+namespace Project.Core.Models.Mappings
+{
+ public class PageRequestMap : ClassMap<PageRequest>
+ {
+ public PageRequestMap()
+ {
+ Id(x => x.Id);
+ Map(x => x.Path);
+ Map(x => x.QueryString);
+ Map(x => x.HttpMethod);
+ Map(x => x.IsLocal);
+ Map(x => x.IsSecure);
+ Map(x => x.UserHostAddress);
+ Map(x => x.UserHostName);
+ Map(x => x.Browser);
+ Map(x => x.BrowserId);
+ Map(x => x.BrowserVersion);
+ Map(x => x.Crawler);
+ Map(x => x.IsMobile);
+ Map(x => x.AllowJs);
+ Map(x => x.Height);
+ Map(x => x.Width);
+ Map(x => x.Platform);
+ Map(x => x.Time);
+ }
+ }
+}
View
27 src/Project.Core/Models/ViewModels/Notifications/NotificationBase.cs
@@ -13,5 +13,32 @@ public virtual string Message
{
get { return _message; }
}
+
+ public override bool Equals(object obj)
+ {
+ return Equals((NotificationBase)obj);
+ }
+
+ public bool Equals(NotificationBase other)
+ {
+ if (ReferenceEquals(null, other)) return false;
+ if (ReferenceEquals(this, other)) return true;
+ return Equals(other._message, _message);
+ }
+
+ public override int GetHashCode()
+ {
+ return (_message != null ? _message.GetHashCode() : 0);
+ }
+
+ public static bool operator ==(NotificationBase left, NotificationBase right)
+ {
+ return Equals(left, right);
+ }
+
+ public static bool operator !=(NotificationBase left, NotificationBase right)
+ {
+ return !Equals(left, right);
+ }
}
}
View
3 src/Project.Core/Project.Core.csproj
@@ -91,6 +91,7 @@
<Compile Include="Controllers\NotificationsController.cs" />
<Compile Include="Ensure.cs" />
<Compile Include="Lib\ActionFilters\HandleErrorWithElmahAttribute.cs" />
+ <Compile Include="Lib\ActionFilters\LogRequestsAttribute.cs" />
<Compile Include="Lib\Html\ExtensionsOfHtmlHelper.cs" />
<Compile Include="ExtensionsOfString.cs" />
<Compile Include="ExtensionsOfUrlHelper.cs" />
@@ -109,9 +110,11 @@
<Compile Include="Lib\Infrastructure\SqlNHibernateSessionConfiguration.cs" />
<Compile Include="Lib\Infrastructure\WebContextModule.cs" />
<Compile Include="Models\Entities\Contact.cs" />
+ <Compile Include="Models\Entities\PageRequest.cs" />
<Compile Include="Models\Mappings\ContactMap.cs" />
<Compile Include="Models\Mappings\NoteMap.cs" />
<Compile Include="Models\Entities\Note.cs" />
+ <Compile Include="Models\Mappings\PageRequestMap.cs" />
<Compile Include="Models\ViewModels\Notifications\ErrorNotification.cs" />
<Compile Include="Models\ViewModels\Notifications\InfoNotification.cs" />
<Compile Include="Models\ViewModels\ListNotesViewModel.cs" />
View
18 src/Project.Tests/Core/Controllers/ContactsContollerTests.cs
@@ -10,24 +10,6 @@ namespace Project.Core.Controllers
public class ContactsContollerTests
{
[Fact]
- public void TestCreateAsXhrReturnsPartial()
- {
- // Assemble
- var scope = new Mock<IAppScope>();
- scope.SetupGet(x => x.IsXhr).Returns(true);
- var repo = new Mock<IRepository>();
- var controller = new ContactsController(repo.Object, scope.Object);
-
- // Act
- var contact = new Contact();
- var result = controller.Create(contact);
-
- // Assert
- var partialViewResult = Assert.IsType<PartialViewResult>(result);
- Assert.IsType<Contact>(partialViewResult.ViewData.Model);
- }
-
- [Fact]
public void TestCreateReturnsIndex()
{
// Assemble
View
6 src/Project.Web/Content/Scripts/application.contacts.js
@@ -3,24 +3,26 @@
event.preventDefault();
var form = $(this).parents("form");
var url = form.attr("action");
+ var currentPath = window.location.href;
var formData = form.serialize();
$.ajax({
type: "put",
url: url,
data: formData,
- success: function() { window.location = "/Contacts"; }
+ success: function() { window.location = currentPath; }
});
});
$(".delete-contact").live("click", function(event) {
event.preventDefault();
var url = $(this).attr("href");
+ var currentPath = window.location.href;
var formData = $(this).parents("form").serialize();
if (!window.confirm("You are about to delete a contact. This is permanent and cannot be undone. Are you sure?")) { return; }
$.ajax({
type: "DELETE",
url: url,
data: formData,
- success: function() { window.location = "/Contacts"; }
+ success: function() { window.location = currentPath; }
});
});
});
View
6 src/Project.Web/Content/Scripts/application.notes.js
@@ -3,25 +3,27 @@
event.preventDefault();
var form = $(this).parents("form");
var url = form.attr("action");
+ var currentPath = window.location.href;
var contactId = $("input[name=ContactId]").val();
var formData = form.serialize();
$.ajax({
type: "put",
url: url,
data: formData,
- success: function() { window.location = "/Contacts/" + contactId + "/Notes" ; }
+ success: function() { window.location = currentPath; }
});
});
$(".delete-note").live("click", function(event) {
event.preventDefault();
var url = $(this).attr("href");
+ var currentPath = window.location.href;
var formData = $(this).parents("form").serialize();
if (!window.confirm("You are about to delete a note. This is permanent and cannot be undone. Are you sure?")) { return; }
$.ajax({
type: "DELETE",
url: url,
data: formData,
- success: function() { window.location.reload(true); }
+ success: function() { window.location = currentPath; }
});
});
});
View
33 src/Project.Web/Content/Stylesheets/Screen.css
@@ -111,24 +111,33 @@ tbody td {
}
.error-notification {
- background: #E09698 url('../Images/notification-error.png') no-repeat 10px 13px;
- color: #910000;
- border-color: #910000;
- padding-left: 24px;
+ background: #E09698 url('../Images/notification-error.png') no-repeat 10px 13px;
+ border-color: #990000;
+ padding-left: 36px;
+}
+
+.error-notification li {
+ color: #990000;
}
.info-notification {
- background: #E09698 url('../Images/notification-info.png') no-repeat 10px 13px;
- color: #910000;
- border-color: #910000;
- padding-left: 24px;
+ background: #66CCFF url('../Images/notification-info.png') no-repeat 10px 13px;
+ border-color: #000099;
+ padding-left: 36px;
+}
+
+.info-notification li {
+ color: #000099;
}
.success-notification {
- background: #E09698 url('../Images/notification-success.png') no-repeat 10px 13px;
- color: #910000;
- border-color: #910000;
- padding-left: 24px;
+ background: #CCFFCC url('../Images/notification-success.png') no-repeat 10px 13px;
+ border-color: #006600;
+ padding-left: 36px;
+}
+
+.success-notification li {
+ color: #006600;
}
.warning-notification {
View
2 src/Project.Web/Project.Web.csproj
@@ -127,7 +127,7 @@
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>3010</DevelopmentServerPort>
- <DevelopmentServerVPath>/</DevelopmentServerVPath>
+ <DevelopmentServerVPath>/aspnetmvc-template/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>

0 comments on commit ccdfc6f

Please sign in to comment.