Добавил поиск пользователя через ajax

parent 9ef1712a
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Enums;
using PhoneStore.Helpers;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels.Account;
namespace PhoneStore.Controllers
......@@ -11,10 +12,12 @@ namespace PhoneStore.Controllers
public class AccountController : Controller
{
private readonly IAccountService _accountService;
private readonly ISearchService _searchService;
public AccountController(IAccountService accountService)
public AccountController(IAccountService accountService, ISearchService searchService)
{
_accountService = accountService;
_searchService = searchService;
}
[HttpGet]
......@@ -73,5 +76,19 @@ namespace PhoneStore.Controllers
await _accountService.LogOf();
return RedirectToAction("Index", "Phones");
}
[HttpGet]
public IActionResult SearchAccounts(string searchTerm)
{
try
{
var users = _searchService.SearchUsersByAny(searchTerm);
return PartialView("PartialViews/UserSearchResultPartialView", users);
}
catch (Exception e)
{
return StatusCode(500, e.Message);
}
}
}
}
\ No newline at end of file
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Enums;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels;
namespace PhoneStore.Controllers
......
......@@ -7,7 +7,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Helpers;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels;
namespace PhoneStore.Controllers
......
......@@ -2,7 +2,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PhoneStore.Models;
using PhoneStore.Services;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Helpers
{
......@@ -22,6 +22,8 @@ namespace PhoneStore.Helpers
services.AddTransient<IUsersFilter, UsersFilter>();
services.AddTransient<IPaginationService<User>, PaginationService<User>>();
services.AddTransient<IAccountService, AccountService>();
services.AddTransient<ISearchService, SearchService>();
services.AddScoped<IUserSearchService, UserSearchService>();
}
}
}
\ No newline at end of file
......@@ -15,6 +15,7 @@
<ItemGroup>
<_ContentIncludedByDefault Remove="wwwroot\Files\book.pdf" />
<_ContentIncludedByDefault Remove="Views\Searchs\Search.cshtml" />
</ItemGroup>
<ItemGroup>
......
......@@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Enums;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels.Account;
using IdentityResult = PhoneStore.DataObjects.IdentityResult;
using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;
......
......@@ -3,7 +3,7 @@ using System.Linq;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Enums;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services
{
......
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services
{
......
using System.Collections.Generic;
using System.Threading.Tasks;
using PhoneStore.DataObjects;
using PhoneStore.Models;
using PhoneStore.ViewModels.Account;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IAccountService
{
......
......@@ -2,7 +2,7 @@ using System.Collections.Generic;
using PhoneStore.Enums;
using PhoneStore.Models;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IBasketService
{
......
using System.Threading.Tasks;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface ICreatable<in T> where T : class
{
......
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IDefaultPhoneImagePathProvider
{
......
using System.Linq;
using System.Threading.Tasks;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IPaginationService<T>
{
......
using PhoneStore.ViewModels;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IPhoneService : ICreatable<PhoneCreateViewModel>
{
......
using System.Collections.Generic;
using PhoneStore.Models;
namespace PhoneStore.Services.Interfaces;
public interface ISearchService
{
IEnumerable<User> SearchUsersByAny(string searchTerm);
}
\ No newline at end of file
using System.Collections.Generic;
using PhoneStore.Models;
namespace PhoneStore.Services.Interfaces;
public interface IUserSearchService
{
IEnumerable<User> SearchByName(string searchTerm);
IEnumerable<User> SearchByLogin(string searchTerm);
IEnumerable<User> SearchByEmail(string searchTerm);
}
\ No newline at end of file
using System.Linq;
using PhoneStore.Models;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IUsersFilter
{
......
......@@ -2,7 +2,7 @@ using System.Linq;
using PhoneStore.Models;
using Order = PhoneStore.Enums.Order;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IUsersSortService
{
......
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services
{
......
......@@ -5,7 +5,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using PhoneStore.Helpers;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels;
namespace PhoneStore.Services
......
using System.Collections.Generic;
using System.Linq;
using PhoneStore.Models;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services;
public class SearchService : ISearchService
{
private readonly IUserSearchService _userSearch;
public SearchService(IUserSearchService userSearch)
{
_userSearch = userSearch;
}
public IEnumerable<User> SearchUsersByAny(string searchTerm)
{
var resultByName = _userSearch.SearchByName(searchTerm);
var resultByLogin = _userSearch.SearchByLogin(searchTerm);
var resultByEmail = _userSearch.SearchByEmail(searchTerm);
var users = resultByName
.Concat(resultByLogin)
.Concat(resultByEmail)
.ToHashSet();
//if (users == null || !users.Any())
if (users?.Any() ?? false)
{
return users;
}
return new List<User>();
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Linq;
using PhoneStore.Models;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services;
public class UserSearchService : IUserSearchService
{
private readonly MobileContext _db;
public UserSearchService(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
using System.Linq;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services
{
......
using System.Linq;
using System.Threading.Tasks;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using Order = PhoneStore.Enums.Order;
namespace PhoneStore.Services
......
......@@ -9,7 +9,6 @@ using Microsoft.Extensions.Hosting;
using PhoneStore.Helpers;
using PhoneStore.Models;
using PhoneStore.Services;
using PhoneStore.Services.Abstractions;
namespace PhoneStore
{
......
@model IEnumerable<User>
@if (Model.Any())
{
var count = 1;
<table class="table">
<caption>Список пользователей</caption>
<thead>
<tr>
<th scope="col">
#
</th>
<th scope="col">
Имя
</th>
<th scope="col">
Login
</th>
<th scope="col">
Email
</th>
<th scope="col">
Age
</th>
<th scope="col">
Phone
</th>
</tr>
</thead>
<tbody>
@foreach (var user in Model)
{
<tr>
<th scope="row" id="@user.Id">@(count++)</th>
<td id="name">@user.Name</td>
<td id="username">@user.UserName</td>
<td id="email">@user.Email</td>
<td id="age">@user.Age</td>
<td id="phone">@(string.IsNullOrWhiteSpace(user.PhoneNumber) ? "номера нет" : user.PhoneNumber)</td>
</tr>
}
</tbody>
</table>
}
else
{
<h2>Таких пользователей нет</h2>
}
\ No newline at end of file
......@@ -27,13 +27,17 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Brands" asp-action="Create">Добавить бренд</a>
</li>
<li class="nav-item">
<input type="text" id="search" class="form-control" placeholder="Критерии для поиска">
</li>
</ul>
</div>
<div class="login_group">
@if(User.Identity.IsAuthenticated)
@if (User.Identity.IsAuthenticated)
{
<p>@User.Identity.Name</p>
<form method="post" asp-controller="Account" asp-action="LogOff">
<button class="btn btn-outline-warning" type="submit">Выход</button>
</form>
......@@ -49,6 +53,7 @@
</header>
<div class="container">
<main role="main" class="pb-3">
<div id="results"></div>
@RenderBody()
</main>
</div>
......
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