diff --git a/src/UserPointManagement.Application/Services/UserService.cs b/src/UserPointManagement.Application/Services/UserService.cs index bdbd997..fb99556 100644 --- a/src/UserPointManagement.Application/Services/UserService.cs +++ b/src/UserPointManagement.Application/Services/UserService.cs @@ -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> GetUsers(GetUserDto req); } public class UserService : IUserService { - + private readonly UserPointManagementDbContext _userPointManagementDbContext; + + public UserService(UserPointManagementDbContext userPointManagementDbContext) + { + _userPointManagementDbContext = userPointManagementDbContext; + } + + public async Task> 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() + { + Items = data, + TotalCount = count + }; + } } \ No newline at end of file diff --git a/src/UserPointManagement.Application/UserPointManagement.Application.csproj b/src/UserPointManagement.Application/UserPointManagement.Application.csproj index 168c16f..adf03e5 100644 --- a/src/UserPointManagement.Application/UserPointManagement.Application.csproj +++ b/src/UserPointManagement.Application/UserPointManagement.Application.csproj @@ -10,4 +10,9 @@ + + + + + diff --git a/src/UserPointManagement.Model/Dtos/GetUserDto.cs b/src/UserPointManagement.Model/Dtos/GetUserDto.cs new file mode 100644 index 0000000..13e4b09 --- /dev/null +++ b/src/UserPointManagement.Model/Dtos/GetUserDto.cs @@ -0,0 +1,6 @@ +namespace UserPointManagement.Model.Dtos; + +public class GetUserDto : PageBase +{ + public string Keyword { get; set; } +} \ No newline at end of file diff --git a/src/UserPointManagement.Model/PageBase.cs b/src/UserPointManagement.Model/PageBase.cs new file mode 100644 index 0000000..d43659a --- /dev/null +++ b/src/UserPointManagement.Model/PageBase.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/UserPointManagement.Model/PageResultDto.cs b/src/UserPointManagement.Model/PageResultDto.cs new file mode 100644 index 0000000..efb87ef --- /dev/null +++ b/src/UserPointManagement.Model/PageResultDto.cs @@ -0,0 +1,8 @@ +namespace UserPointManagement.Model; + +public class PageResultDto +{ + public int TotalCount { get; set; } + + public List Items { get; set; } = new List(); +} \ No newline at end of file diff --git a/src/UserPointManagement.Persistence/QueryableExtensions.cs b/src/UserPointManagement.Persistence/QueryableExtensions.cs new file mode 100644 index 0000000..e93ba48 --- /dev/null +++ b/src/UserPointManagement.Persistence/QueryableExtensions.cs @@ -0,0 +1,24 @@ +using System.Linq.Expressions; +using UserPointManagement.Model; + +namespace UserPointManagement.Persistence; + +public static class QueryableExtensions +{ + public static IQueryable Where( + this IQueryable queryable, Expression> predicate, bool applyPredicate) + { + return applyPredicate ? queryable.Where(predicate) : queryable; + } + + public static IQueryable Paging(this IQueryable queryable, PageBase param) + { + if (param == null || param.PageIndex < 0 || param.PageSize < 0) + { + return queryable; + } + + return queryable + .Skip(param.PageIndex * param.PageSize).Take(param.PageSize); + } +} \ No newline at end of file diff --git a/src/UserPointManagement.Web/Layouts/BasicLayout.razor b/src/UserPointManagement.Web/Layouts/BasicLayout.razor index 408a496..424db3d 100644 --- a/src/UserPointManagement.Web/Layouts/BasicLayout.razor +++ b/src/UserPointManagement.Web/Layouts/BasicLayout.razor @@ -11,7 +11,7 @@ @Body - + diff --git a/src/UserPointManagement.Web/Pages/Welcome.razor b/src/UserPointManagement.Web/Pages/Welcome.razor index 52b7a10..88b2b2c 100644 --- a/src/UserPointManagement.Web/Pages/Welcome.razor +++ b/src/UserPointManagement.Web/Pages/Welcome.razor @@ -1,41 +1,82 @@ @page "/" +@using UserPointManagement.Application.Services +@using UserPointManagement.Model.Dtos +@using UserPointManagement.Model.Entities +@inject IUserService _userService; +@inject IJSRuntime JS - - - - - - Use .NET CLI to install the latest template quickly. - - -
 dotnet new --install AntDesign.Templates::0.1.0-*
- - - Create an empty blazor WebAssembly project - - -
 dotnet new antdesign --host=wasm
- - - Create a blazor webassembly project with all pages. - - -
 dotnet new antdesign --host=wasm --full
-
-

- Want to add more pages? Please refer to - - ant-design-pro-blazor project - - . -

+ + + + + + + + + + + + + + + + +
+ +@code { + private List _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(); + } + +} \ No newline at end of file diff --git a/test/UserPointManagement.Application.Tests/Services/UserServiceTest.cs b/test/UserPointManagement.Application.Tests/Services/UserServiceTest.cs new file mode 100644 index 0000000..f04d4a5 --- /dev/null +++ b/test/UserPointManagement.Application.Tests/Services/UserServiceTest.cs @@ -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); + } +} \ No newline at end of file diff --git a/test/UserPointManagement.Application.Tests/UserPointManagement.Application.Tests.csproj b/test/UserPointManagement.Application.Tests/UserPointManagement.Application.Tests.csproj index 16f9692..bba0197 100644 --- a/test/UserPointManagement.Application.Tests/UserPointManagement.Application.Tests.csproj +++ b/test/UserPointManagement.Application.Tests/UserPointManagement.Application.Tests.csproj @@ -20,10 +20,6 @@
- - - -