Skip to content

Commit

Permalink
added feature troygoode#6 - search for user by username or email address
Browse files Browse the repository at this point in the history
  • Loading branch information
troygoode committed Aug 17, 2011
1 parent 400cb28 commit 0897750
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
20 changes: 11 additions & 9 deletions src/SampleWebsite.Mvc3.Tests/UserAdministrationControllerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ViewResult>(result);
Assert.Empty(result.ViewName);
var viewResult = Assert.IsType<ViewResult>(result);
Assert.Empty(viewResult.ViewName);
}

[Fact]
Expand All @@ -49,11 +49,12 @@ public void Index_retrieves_users_from_membershipService_and_passes_to_view()
_userService.Setup(x => x.FindAll(It.IsAny<int>(), It.IsAny<int>())).Returns(users).Verifiable();

//act
var result = _controller.Index(1);
var result = _controller.Index(1, null);

//assert
_userService.Verify();
var viewModel = Assert.IsType<IndexViewModel>(result.ViewData.Model);
var viewResult = Assert.IsType<ViewResult>(result);
var viewModel = Assert.IsType<IndexViewModel>(viewResult.ViewData.Model);
Assert.Same(users, viewModel.Users);
}

Expand All @@ -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<IndexViewModel>(result.ViewData.Model);
var viewResult = Assert.IsType<ViewResult>(result);
var viewModel = Assert.IsType<IndexViewModel>(viewResult.ViewData.Model);
Assert.Same(roles, viewModel.Roles);
}

Expand All @@ -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<int>(v => v == index), It.IsAny<int>()));
Expand All @@ -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<int>(v => v == 1), It.IsAny<int>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace SampleWebsite.Mvc3.Areas.MvcMembership.Models.UserAdministration
{
public class IndexViewModel
{
public string Search { get; set; }
public IPagedList<MembershipUser> Users { get; set; }
public IEnumerable<string> Roles { get; set; }
public bool IsRolesEnabled { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@
}

<h2 class="mvcMembership">User Administration</h2>

<h3 class="mvcMembership">Find a User
@if (!string.IsNullOrWhiteSpace(Model.Search)) {
<span>(@Html.ActionLink("Clear Search", "Index"))</span>
}
</h3>
<form method="get" class="mvcMembership-searchForm">
<fieldset>
<label>
User Name or Email Address:
<input type="text" name="search" value="@Model.Search" />
<input type="submit" value="Search" />
</label>
</fieldset>
</form>

<h3 class="mvcMembership">Users (@Html.ActionLink("New User", "CreateUser", "UserAdministration"))</h3>
<div class="mvcMembership-allUsers">
Expand Down Expand Up @@ -50,7 +65,7 @@
</li>
}
</ul>
@Html.PagedListPager(Model.Users, page => Url.Action("Index", new { page }))
@Html.PagedListPager(Model.Users, page => Url.Action("Index", new { page, search = Model.Search }))
}else{
<p>No users have registered.</p>
}
Expand Down
5 changes: 5 additions & 0 deletions src/SampleWebsite.Mvc3/Content/MvcMembership.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 0897750

Please sign in to comment.