Добавил функционал поиска пользователей по имени, мылу и логину.

parent ec9df425
...@@ -3,7 +3,6 @@ using System.Threading.Tasks; ...@@ -3,7 +3,6 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using PhoneStore.Enums; using PhoneStore.Enums;
using PhoneStore.Helpers; using PhoneStore.Helpers;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions; using PhoneStore.Services.Abstractions;
using PhoneStore.ViewModels.Account; using PhoneStore.ViewModels.Account;
...@@ -74,5 +73,12 @@ namespace PhoneStore.Controllers ...@@ -74,5 +73,12 @@ namespace PhoneStore.Controllers
await _accountService.LogOf(); await _accountService.LogOf();
return RedirectToAction("Index", "Phones"); return RedirectToAction("Index", "Phones");
} }
[HttpGet]
public IActionResult SearchAccounts(string searchTerm)
{
var users = _accountService.SearchUsersByAnyTerm(searchTerm);
return PartialView("PartialViews/SearchResultPartial", users);
}
} }
} }
\ No newline at end of file
...@@ -22,6 +22,7 @@ namespace PhoneStore.Helpers ...@@ -22,6 +22,7 @@ namespace PhoneStore.Helpers
services.AddTransient<IUsersFilter, UsersFilter>(); services.AddTransient<IUsersFilter, UsersFilter>();
services.AddTransient<IPaginationService<User>, PaginationService<User>>(); services.AddTransient<IPaginationService<User>, PaginationService<User>>();
services.AddTransient<IAccountService, AccountService>(); services.AddTransient<IAccountService, AccountService>();
services.AddTransient<IUsersSearcher, UsersSearcher>();
} }
} }
} }
\ No newline at end of file
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using PhoneStore.DataObjects; using PhoneStore.DataObjects;
using PhoneStore.Models;
using PhoneStore.ViewModels.Account; using PhoneStore.ViewModels.Account;
namespace PhoneStore.Services.Abstractions namespace PhoneStore.Services.Abstractions
...@@ -9,5 +11,6 @@ namespace PhoneStore.Services.Abstractions ...@@ -9,5 +11,6 @@ namespace PhoneStore.Services.Abstractions
Task<IdentityResult> Register(RegisterViewModel model); Task<IdentityResult> Register(RegisterViewModel model);
Task<IdentityResult> LogIn(LoginViewModel model); Task<IdentityResult> LogIn(LoginViewModel model);
Task LogOf(); Task LogOf();
IEnumerable<User> SearchUsersByAnyTerm(string searchTerm);
} }
} }
\ No newline at end of file
using System.Collections.Generic;
using PhoneStore.Models;
namespace PhoneStore.Services.Abstractions
{
public interface IUsersSearcher
{
IEnumerable<User> SearchByName(string searchTerm);
IEnumerable<User> SearchByLogin(string searchTerm);
IEnumerable<User> SearchByEmail(string searchTerm);
}
}
\ No newline at end of file
...@@ -2,11 +2,13 @@ using System.Collections.Generic; ...@@ -2,11 +2,13 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Enums; using PhoneStore.Enums;
using PhoneStore.Models; using PhoneStore.Models;
using PhoneStore.Services.Abstractions; using PhoneStore.Services.Abstractions;
using PhoneStore.ViewModels.Account; using PhoneStore.ViewModels.Account;
using IdentityResult = PhoneStore.DataObjects.IdentityResult; using IdentityResult = PhoneStore.DataObjects.IdentityResult;
using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;
namespace PhoneStore.Services namespace PhoneStore.Services
{ {
...@@ -14,13 +16,16 @@ namespace PhoneStore.Services ...@@ -14,13 +16,16 @@ namespace PhoneStore.Services
{ {
private readonly UserManager<User> _userManager; private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager; private readonly SignInManager<User> _signInManager;
private readonly IUsersSearcher _usersSearcher;
public AccountService( public AccountService(
UserManager<User> userManager, UserManager<User> userManager,
SignInManager<User> signInManager) SignInManager<User> signInManager,
IUsersSearcher usersSearcher)
{ {
_userManager = userManager; _userManager = userManager;
_signInManager = signInManager; _signInManager = signInManager;
_usersSearcher = usersSearcher;
} }
public async Task<IdentityResult> Register(RegisterViewModel model) public async Task<IdentityResult> Register(RegisterViewModel model)
...@@ -34,7 +39,9 @@ namespace PhoneStore.Services ...@@ -34,7 +39,9 @@ namespace PhoneStore.Services
User user = new User User user = new User
{ {
Email = model.Email, Email = model.Email,
UserName = model.Email UserName = model.Email,
Age = model.Age,
Name = model.Name
}; };
var result = await _userManager.CreateAsync(user, model.Password); var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded) if (result.Succeeded)
...@@ -74,5 +81,19 @@ namespace PhoneStore.Services ...@@ -74,5 +81,19 @@ namespace PhoneStore.Services
public async Task LogOf() public async Task LogOf()
=> await _signInManager.SignOutAsync(); => await _signInManager.SignOutAsync();
[HttpGet]
public IEnumerable<User> SearchUsersByAnyTerm(string searchTerm)
{
var resultByName = _usersSearcher.SearchByName(searchTerm);
var resultByLogin = _usersSearcher.SearchByLogin(searchTerm);
var resultByEmail = _usersSearcher.SearchByEmail(searchTerm);
var users = resultByName
.Concat(resultByLogin)
.Concat(resultByEmail)
.ToHashSet();
return users;
}
} }
} }
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
namespace PhoneStore.Services
{
public class UsersSearcher : IUsersSearcher
{
private readonly MobileContext _db;
public UsersSearcher(MobileContext db)
{
_db = db;
}
public IEnumerable<User> SearchByName(string searchTerm)
=> _db.Users.Where(u => u.Name
.ToLower()
.Contains(searchTerm
.ToLower()));
public IEnumerable<User> SearchByLogin(string searchTerm)
=> _db.Users.Where(u => u.UserName.ToLower().Contains(searchTerm.ToLower()));
public IEnumerable<User> SearchByEmail(string searchTerm)
=> _db.Users.Where(u => u.Email.ToLower().Contains(searchTerm.ToLower()));
}
}
\ No newline at end of file
@model IEnumerable<User>
@if (Model.Any())
{
var i = 1;
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">UserName</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody>
@foreach (var user in Model)
{
<tr>
<th scope="row" id="@user.Id">@(i++)</th>
<td>@user.Name</td>
<td>@user.Email</td>
<td>@user.UserName</td>
<td>@user.Age</td>
</tr>
}
</tbody>
</table>
}
else
{
<p>Список пуст</p>
}
\ No newline at end of file
@model Phone @model PhoneViewModel
@{ @{
ViewBag.Title = "Подробная информация"; ViewBag.Title = "Подробная информация";
......
...@@ -27,6 +27,9 @@ ...@@ -27,6 +27,9 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Brands" asp-action="Create">Добавить бренд</a> <a class="nav-link text-dark" asp-area="" asp-controller="Brands" asp-action="Create">Добавить бренд</a>
</li> </li>
<li class="nav-item">
<input type="text" id="search" class="form-control" placeholder="Критерии для поиска">
</li>
</ul> </ul>
</div> </div>
<div class="login_group"> <div class="login_group">
...@@ -49,6 +52,7 @@ ...@@ -49,6 +52,7 @@
</header> </header>
<div class="container"> <div class="container">
<main role="main" class="pb-3"> <main role="main" class="pb-3">
<div id="results"></div>
@RenderBody() @RenderBody()
</main> </main>
</div> </div>
......
{ {
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Server=localhost;Port=54320;database=lesson58;Username=postgres;Password=postgres;" "DefaultConnection": "Server=localhost;Port=54320;database=lesson63;Username=postgres;Password=postgres;"
}, },
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
......
// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification $(document).ready(function (){
// for details on configuring this project to bundle and minify static web assets. $('#search').on('keyup', function (e){
e.preventDefault();
// Write your JavaScript code. let searchTerm = $(this).val();
console.log(searchTerm)
searchTerm = encodeURIComponent(searchTerm);
console.log(searchTerm)
$('#results')
.load(`https://localhost:5001/Account/SearchAccounts?searchTerm=${searchTerm}`);
})
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment