From 0897750d35f7a217c5b83c2cf7a4902bd8d14726 Mon Sep 17 00:00:00 2001 From: Troy Goode Date: Tue, 16 Aug 2011 20:09:07 -0400 Subject: [PATCH] added feature #6 - search for user by username or email address --- .../UserAdministrationControllerFacts.cs | 20 ++++++++++--------- .../UserAdministrationController.cs | 14 +++++++++++-- .../UserAdministration/IndexViewModel.cs | 1 + .../Views/UserAdministration/Index.cshtml | 17 +++++++++++++++- .../Content/MvcMembership.css | 5 +++++ 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/SampleWebsite.Mvc3.Tests/UserAdministrationControllerFacts.cs b/src/SampleWebsite.Mvc3.Tests/UserAdministrationControllerFacts.cs index 3129b01..ce31157 100644 --- a/src/SampleWebsite.Mvc3.Tests/UserAdministrationControllerFacts.cs +++ b/src/SampleWebsite.Mvc3.Tests/UserAdministrationControllerFacts.cs @@ -34,11 +34,11 @@ public UserAdministrationControllerFacts() public void Index_returns_default_view() { //act - var result = _controller.Index(1); + var result = _controller.Index(1, null); //assert - Assert.IsType(result); - Assert.Empty(result.ViewName); + var viewResult = Assert.IsType(result); + Assert.Empty(viewResult.ViewName); } [Fact] @@ -49,11 +49,12 @@ public void Index_retrieves_users_from_membershipService_and_passes_to_view() _userService.Setup(x => x.FindAll(It.IsAny(), It.IsAny())).Returns(users).Verifiable(); //act - var result = _controller.Index(1); + var result = _controller.Index(1, null); //assert _userService.Verify(); - var viewModel = Assert.IsType(result.ViewData.Model); + var viewResult = Assert.IsType(result); + var viewModel = Assert.IsType(viewResult.ViewData.Model); Assert.Same(users, viewModel.Users); } @@ -65,11 +66,12 @@ public void Index_retrieves_roles_from_rolesService_and_passes_to_view() _rolesService.Setup(x => x.FindAll()).Returns(roles).Verifiable(); //act - var result = _controller.Index(1); + var result = _controller.Index(1, null); //assert _rolesService.Verify(); - var viewModel = Assert.IsType(result.ViewData.Model); + var viewResult = Assert.IsType(result); + var viewModel = Assert.IsType(viewResult.ViewData.Model); Assert.Same(roles, viewModel.Roles); } @@ -80,7 +82,7 @@ public void Index_allows_the_pageIndex_to_be_specified_for_paging() const int index = 5; //act - _controller.Index(index); + _controller.Index(index, null); //assert _userService.Verify(x => x.FindAll(It.Is(v => v == index), It.IsAny())); @@ -90,7 +92,7 @@ public void Index_allows_the_pageIndex_to_be_specified_for_paging() public void Index_defaults_to_the_first_page_if_no_pageIndex_is_specified() { //act - _controller.Index(null); + _controller.Index(null, null); //assert _userService.Verify(x => x.FindAll(It.Is(v => v == 1), It.IsAny())); diff --git a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Controllers/UserAdministrationController.cs b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Controllers/UserAdministrationController.cs index 753b4fc..01c280f 100644 --- a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Controllers/UserAdministrationController.cs +++ b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Controllers/UserAdministrationController.cs @@ -46,11 +46,21 @@ public UserAdministrationController( _smtpClient = smtpClient; } - public ViewResult Index(int? page) + public ActionResult Index(int? page, string search) { + var users = string.IsNullOrWhiteSpace(search) + ? _userService.FindAll(page ?? 1, PageSize) + : search.Contains("@") + ? _userService.FindByEmail(search, page ?? 1, PageSize) + : _userService.FindByUserName(search, page ?? 1, PageSize); + + if (!string.IsNullOrWhiteSpace(search) && users.Count == 1) + return RedirectToAction("Details", new {id = users[0].ProviderUserKey.ToString()}); + return View(new IndexViewModel { - Users = _userService.FindAll(page ?? 1, PageSize), + Search = search, + Users = users, Roles = _rolesService.Enabled ? _rolesService.FindAll() : Enumerable.Empty(), diff --git a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Models/UserAdministration/IndexViewModel.cs b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Models/UserAdministration/IndexViewModel.cs index 205aa59..6e6cab8 100644 --- a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Models/UserAdministration/IndexViewModel.cs +++ b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Models/UserAdministration/IndexViewModel.cs @@ -6,6 +6,7 @@ namespace SampleWebsite.Mvc3.Areas.MvcMembership.Models.UserAdministration { public class IndexViewModel { + public string Search { get; set; } public IPagedList Users { get; set; } public IEnumerable Roles { get; set; } public bool IsRolesEnabled { get; set; } diff --git a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Views/UserAdministration/Index.cshtml b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Views/UserAdministration/Index.cshtml index 30108ed..50191c5 100644 --- a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Views/UserAdministration/Index.cshtml +++ b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Views/UserAdministration/Index.cshtml @@ -13,6 +13,21 @@ }

User Administration

+ +

Find a User + @if (!string.IsNullOrWhiteSpace(Model.Search)) { + (@Html.ActionLink("Clear Search", "Index")) + } +

+
+
+ +
+

Users (@Html.ActionLink("New User", "CreateUser", "UserAdministration"))

@@ -50,7 +65,7 @@ } - @Html.PagedListPager(Model.Users, page => Url.Action("Index", new { page })) + @Html.PagedListPager(Model.Users, page => Url.Action("Index", new { page, search = Model.Search })) }else{

No users have registered.

} diff --git a/src/SampleWebsite.Mvc3/Content/MvcMembership.css b/src/SampleWebsite.Mvc3/Content/MvcMembership.css index ebbaf71..ed562bf 100644 --- a/src/SampleWebsite.Mvc3/Content/MvcMembership.css +++ b/src/SampleWebsite.Mvc3/Content/MvcMembership.css @@ -76,4 +76,9 @@ div.mvcMembership-allUsers .PagedList-disabled a:hover{ div.mvcMembership-allRoles ul > li{ margin: .5em 0 .5em 0; +} + +form.mvcMembership-searchForm fieldset, +.mvcMembership-allRoles fieldset{ + padding-top: 1.5em; } \ No newline at end of file