feat: 完善用户列表查询逻辑
parent
2fdb95e350
commit
27b6ccedd2
|
|
@ -1,11 +1,37 @@
|
|||
namespace UserPointManagement.Application.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UserPointManagement.Model;
|
||||
using UserPointManagement.Model.Dtos;
|
||||
using UserPointManagement.Model.Entities;
|
||||
using UserPointManagement.Persistence;
|
||||
|
||||
namespace UserPointManagement.Application.Services;
|
||||
|
||||
public interface IUserService
|
||||
{
|
||||
|
||||
Task<PageResultDto<User>> GetUsers(GetUserDto req);
|
||||
}
|
||||
|
||||
public class UserService : IUserService
|
||||
{
|
||||
private readonly UserPointManagementDbContext _userPointManagementDbContext;
|
||||
|
||||
public UserService(UserPointManagementDbContext userPointManagementDbContext)
|
||||
{
|
||||
_userPointManagementDbContext = userPointManagementDbContext;
|
||||
}
|
||||
|
||||
public async Task<PageResultDto<User>> GetUsers(GetUserDto req)
|
||||
{
|
||||
var queryable = from user in _userPointManagementDbContext.Users
|
||||
select user;
|
||||
|
||||
var count = queryable.Count();
|
||||
var data = await queryable.Paging(req).ToListAsync().ConfigureAwait(false);
|
||||
|
||||
return new PageResultDto<User>()
|
||||
{
|
||||
Items = data,
|
||||
TotalCount = count
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -10,4 +10,9 @@
|
|||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UserPointManagement.Model\UserPointManagement.Model.csproj" />
|
||||
<ProjectReference Include="..\UserPointManagement.Persistence\UserPointManagement.Persistence.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
namespace UserPointManagement.Model.Dtos;
|
||||
|
||||
public class GetUserDto : PageBase
|
||||
{
|
||||
public string Keyword { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
namespace UserPointManagement.Model;
|
||||
|
||||
public class PageBase
|
||||
{
|
||||
private int _pageIndex;
|
||||
|
||||
private int _pageSize = 10;
|
||||
|
||||
public int PageIndex
|
||||
{
|
||||
get => _pageIndex;
|
||||
set
|
||||
{
|
||||
if (value - 1 < 0)
|
||||
throw new ArgumentException("分页索引不能小于0");
|
||||
_pageIndex = value - 1;
|
||||
}
|
||||
}
|
||||
|
||||
public int PageSize
|
||||
{
|
||||
get => _pageSize;
|
||||
set
|
||||
{
|
||||
if (value < 0)
|
||||
throw new ArgumentException("分页大小不能小于0");
|
||||
_pageSize = value == 0 ? 10 : value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
namespace UserPointManagement.Model;
|
||||
|
||||
public class PageResultDto<T>
|
||||
{
|
||||
public int TotalCount { get; set; }
|
||||
|
||||
public List<T> Items { get; set; } = new List<T>();
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
using System.Linq.Expressions;
|
||||
using UserPointManagement.Model;
|
||||
|
||||
namespace UserPointManagement.Persistence;
|
||||
|
||||
public static class QueryableExtensions
|
||||
{
|
||||
public static IQueryable<TEntity> Where<TEntity>(
|
||||
this IQueryable<TEntity> queryable, Expression<Func<TEntity, bool>> predicate, bool applyPredicate)
|
||||
{
|
||||
return applyPredicate ? queryable.Where(predicate) : queryable;
|
||||
}
|
||||
|
||||
public static IQueryable<TEntity> Paging<TEntity>(this IQueryable<TEntity> queryable, PageBase param)
|
||||
{
|
||||
if (param == null || param.PageIndex < 0 || param.PageSize < 0)
|
||||
{
|
||||
return queryable;
|
||||
}
|
||||
|
||||
return queryable
|
||||
.Skip(param.PageIndex * param.PageSize).Take(param.PageSize);
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
@Body
|
||||
</ChildContent>
|
||||
<FooterRender>
|
||||
<FooterView Copyright="2021 Ant Design Blazor" Links="Links"></FooterView>
|
||||
<FooterView Copyright="2023 Tiamo" Links="Links"></FooterView>
|
||||
</FooterRender>
|
||||
</AntDesign.ProLayout.BasicLayout>
|
||||
<SettingDrawer />
|
||||
|
|
|
|||
|
|
@ -1,41 +1,82 @@
|
|||
@page "/"
|
||||
@using UserPointManagement.Application.Services
|
||||
@using UserPointManagement.Model.Dtos
|
||||
@using UserPointManagement.Model.Entities
|
||||
@inject IUserService _userService;
|
||||
@inject IJSRuntime JS
|
||||
|
||||
<PageContainer Title="Welcome">
|
||||
<Card>
|
||||
<Alert
|
||||
Message="AntDesign.Templates have been published to nuget, you can download and use them directly."
|
||||
Type="success"
|
||||
ShowIcon="true"
|
||||
Banner
|
||||
Style="margin: -12px; margin-bottom: 24px"/>
|
||||
<Text Strong>
|
||||
<a target="_blank" rel="noopener noreferrer" href="https://www.nuget.org/packages/AntDesign.Templates">
|
||||
Use .NET CLI to install the latest template quickly.
|
||||
</a>
|
||||
</Text>
|
||||
<pre class="pre"><code><Text Copyable> dotnet new --install AntDesign.Templates::0.1.0-*</Text></code></pre>
|
||||
<Text
|
||||
Strong
|
||||
Style="margin-bottom: 12px">
|
||||
<a target="_blank" rel="noopener noreferrer" href="https://github.com/ant-design-blazor/ant-design-pro-blazor">
|
||||
Create an empty blazor WebAssembly project
|
||||
</a>
|
||||
</Text>
|
||||
<pre class="pre"><code><Text Copyable> dotnet new antdesign --host=wasm</Text></code></pre>
|
||||
<Text
|
||||
Strong
|
||||
Style="margin-bottom: 12px">
|
||||
<a target="_blank" rel="noopener noreferrer" href="https://github.com/ant-design-blazor/ant-design-pro-blazor">
|
||||
Create a blazor webassembly project with all pages.
|
||||
</a>
|
||||
</Text>
|
||||
<pre class="pre"><code><Text Copyable> dotnet new antdesign --host=wasm --full</Text></code></pre>
|
||||
</Card>
|
||||
<p style="text-align: center; margin-top: 24px;">
|
||||
Want to add more pages? Please refer to
|
||||
<a href="https://github.com/ant-design-blazor/ant-design-pro-blazor" target="_blank" rel="noopener noreferrer">
|
||||
ant-design-pro-blazor project
|
||||
</a>
|
||||
.
|
||||
</p>
|
||||
<PageContainer Title="用户管理">
|
||||
<Space Direction="@DirectionVHType.Horizontal" Style="margin: 10px 0">
|
||||
<SpaceItem>
|
||||
<Search Placeholder="姓名或手机号" WrapperStyle="width: 200px;" OnSearch="OnSearch" ClassicSearchIcon/>
|
||||
</SpaceItem>
|
||||
</Space>
|
||||
<Table TItem="User" DataSource="@_users"
|
||||
Total="_total"
|
||||
Loading="_loading"
|
||||
PageIndex="@_pageIndex"
|
||||
PageSize="@_pageSize"
|
||||
OnPageIndexChange="OnPageIndexChanged"
|
||||
OnPageSizeChange="OnPageSizeChange">
|
||||
<PropertyColumn Property="c => c.Name" title="姓名"/>
|
||||
<PropertyColumn Property="c => c.Mobile" title="手机号" Width="80"/>
|
||||
<ActionColumn Title="操作" Width="220">
|
||||
<Space Size=@("middle")>
|
||||
<SpaceItem>
|
||||
<button>编辑</button>
|
||||
</SpaceItem>
|
||||
</Space>
|
||||
</ActionColumn>
|
||||
</Table>
|
||||
</PageContainer>
|
||||
|
||||
@code {
|
||||
private List<User> _users;
|
||||
int _pageIndex = 1;
|
||||
int _pageSize = 20;
|
||||
int _total = 0;
|
||||
bool _loading;
|
||||
string _searchValue;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await RefreshTable();
|
||||
}
|
||||
|
||||
private async Task OnSearch(string arg)
|
||||
{
|
||||
_searchValue = arg;
|
||||
_pageIndex = 1;
|
||||
await RefreshTable();
|
||||
}
|
||||
|
||||
private async Task RefreshTable()
|
||||
{
|
||||
_loading = true;
|
||||
var res = await _userService.GetUsers(new GetUserDto()
|
||||
{
|
||||
Keyword = _searchValue,
|
||||
PageIndex = _pageIndex,
|
||||
PageSize = _pageSize
|
||||
});
|
||||
|
||||
_users = res.Items;
|
||||
_total = res.TotalCount;
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
private async Task OnPageIndexChanged(PaginationEventArgs args)
|
||||
{
|
||||
_pageIndex = args.Page;
|
||||
_pageSize = args.PageSize;
|
||||
await RefreshTable();
|
||||
}
|
||||
|
||||
private async Task OnPageSizeChange(PaginationEventArgs args)
|
||||
{
|
||||
_pageIndex = args.Page;
|
||||
_pageSize = args.PageSize;
|
||||
await RefreshTable();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
using System.Threading.Tasks;
|
||||
using UserPointManagement.Application.Services;
|
||||
using UserPointManagement.Model.Dtos;
|
||||
using Xunit;
|
||||
|
||||
namespace UserPointManagement.Application.Tests.Services;
|
||||
|
||||
public class UserServiceTest
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public UserServiceTest(IUserService userService)
|
||||
{
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetUsers()
|
||||
{
|
||||
var res = await _userService.GetUsers(new GetUserDto()).ConfigureAwait(false);
|
||||
|
||||
Assert.NotNull(res);
|
||||
}
|
||||
}
|
||||
|
|
@ -20,10 +20,6 @@
|
|||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Services" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UserPointManagement.Tests\UserPointManagement.Tests.csproj" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
Loading…
Reference in New Issue