feat: 添加用户积分管理列表

master
zhangyousheng 2023-07-29 15:24:01 +08:00
parent 4dd08f59fd
commit 1c4cbc6d45
14 changed files with 309 additions and 20 deletions

View File

@ -0,0 +1,54 @@
using Microsoft.EntityFrameworkCore;
using UserPointManagement.Model;
using UserPointManagement.Model.Dtos.UserPoint;
using UserPointManagement.Model.Entities;
using UserPointManagement.Persistence;
namespace UserPointManagement.Application.Services;
public interface IUserPointService
{
Task<PageResultDto<UserPointDto>> GetUserPoints(GetUserPointDto req);
Task CreateUserPoint(CreateUserPointDto input);
}
public class UserPointService : IUserPointService
{
private readonly IDbContextFactory<UserPointManagementDbContext> _dbContextFactory;
public UserPointService(IDbContextFactory<UserPointManagementDbContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
public async Task<PageResultDto<UserPointDto>> GetUserPoints(GetUserPointDto req)
{
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
var queryable = from userPoint in _userPointManagementDbContext.UserPoints
join user in _userPointManagementDbContext.Users on userPoint.UserId equals user.Id
select new UserPointDto()
{
UserPointId = userPoint.Id,
UserId = user.Id,
Name = user.Name,
Point = userPoint.Point
};
var count = queryable.Count();
var data = await queryable.Paging(req).ToListAsync().ConfigureAwait(false);
return new PageResultDto<UserPointDto>()
{
Items = data,
TotalCount = count
};
}
public async Task CreateUserPoint(CreateUserPointDto input)
{
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
_userPointManagementDbContext.UserPoints.Add(new UserPoint(input.UserId, input.Point));
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
}
}

View File

@ -11,6 +11,8 @@ public interface IUserService
{
Task<PageResultDto<User>> GetUsers(GetUserDto req);
Task<List<UserDto>> GetAllUsers();
Task CreateUser(User input);
Task DeleteUser(int userId);
@ -20,21 +22,22 @@ public interface IUserService
public class UserService : IUserService
{
private readonly UserPointManagementDbContext _userPointManagementDbContext;
private readonly IDbContextFactory<UserPointManagementDbContext> _dbContextFactory;
public UserService(UserPointManagementDbContext userPointManagementDbContext)
public UserService(IDbContextFactory<UserPointManagementDbContext> dbContextFactory)
{
_userPointManagementDbContext = userPointManagementDbContext;
_dbContextFactory = dbContextFactory;
}
public async Task<PageResultDto<User>> GetUsers(GetUserDto req)
{
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
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,
@ -42,14 +45,28 @@ public class UserService : IUserService
};
}
public async Task<List<UserDto>> GetAllUsers()
{
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
var users = await (from user in _userPointManagementDbContext.Users
select new UserDto()
{
Id = user.Id,
Name = user.Name
}).ToListAsync().ConfigureAwait(false);
return users;
}
public async Task CreateUser(User input)
{
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
_userPointManagementDbContext.Users.Add(input);
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
}
public async Task DeleteUser(int userId)
{
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
var user = await _userPointManagementDbContext.Users.FirstOrDefaultAsync(x => x.Id == userId)
.ConfigureAwait(false);
@ -59,6 +76,7 @@ public class UserService : IUserService
public async Task ModifyUser(int userId, ModifyUserDto input)
{
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
var user = await _userPointManagementDbContext.Users.FirstOrDefaultAsync(x => x.Id == userId)
.ConfigureAwait(false);

View File

@ -0,0 +1,8 @@
namespace UserPointManagement.Model.Dtos.User;
public class UserDto
{
public int Id { get; set; }
public string Name { get; set; }
}

View File

@ -0,0 +1,8 @@
namespace UserPointManagement.Model.Dtos.UserPoint;
public class CreateUserPointDto
{
public int UserId { get; set; }
public int Point { get; set; }
}

View File

@ -0,0 +1,6 @@
namespace UserPointManagement.Model.Dtos.UserPoint;
public class GetUserPointDto : PageBase
{
public int? UserId { get; set; }
}

View File

@ -0,0 +1,14 @@
namespace UserPointManagement.Model.Dtos.UserPoint;
public class UserPointDto
{
public long UserPointId { get; set; }
public int UserId { get; set; }
public string Name { get; set; }
public int Point { get; set; }
public DateTime CreateTime { get; set; }
}

View File

@ -1,9 +1,9 @@
@inherits MyAntDesignAppServer.Pages.UserManagement.UserManagementBase
@inherits UserManagementBase
@page "/"
@using UserPointManagement.Application.Services
@using UserPointManagement.Model.Entities
@using System.ComponentModel.DataAnnotations
@using System.Text.Json
@using global::UserPointManagement.Application.Services
@using global::UserPointManagement.Model.Entities
@inject IJSRuntime JS
@inject IUserService UserService;

View File

@ -4,11 +4,10 @@ using System.Threading.Tasks;
using AntDesign;
using Microsoft.AspNetCore.Components;
using UserPointManagement.Application.Services;
using UserPointManagement.Model.Dtos;
using UserPointManagement.Model.Dtos.User;
using UserPointManagement.Model.Entities;
namespace MyAntDesignAppServer.Pages.UserManagement;
namespace UserPointManagement.Web.Pages.UserManagement;
public class UserManagementBase : ComponentBase
{

View File

@ -1,8 +0,0 @@
@page "/user-point"
<PageContainer Title="用户积分">
</PageContainer>
@code {
}

View File

@ -0,0 +1,118 @@
@inherits UserPointManageBase
@page "/user-point"
@using System.ComponentModel.DataAnnotations
@using System.Text.Json
@using global::UserPointManagement.Model.Dtos.User
@using global::UserPointManagement.Model.Dtos.UserPoint
<PageContainer Title="用户积分管理">
<GridRow Style="margin: 10px 0">
<GridCol Span="12">
<Space Direction="@DirectionVHType.Horizontal">
<SpaceItem>
<Select TItem="UserDto"
TItemValue="int?"
DataSource="@_users"
@bind-Value="@_selectedUserId"
LabelName="@nameof(UserDto.Name)"
ValueName="@nameof(UserDto.Id)"
Placeholder="请选择用户"
DefaultActiveFirstOption="false"
EnableSearch
OnSelectedItemChanged="OnSelectedItemChangedHandler"
AllowClear
Style="width: 200px">
</Select>
</SpaceItem>
</Space>
</GridCol>
<GridCol Span="12">
<Space Direction="@DirectionVHType.Horizontal" Style="display: flex; justify-content: flex-end">
<SpaceItem>
<Button type="primary" OnClick="@ShowModal">新增积分</Button>
</SpaceItem>
</Space>
</GridCol>
</GridRow>
<Table TItem="UserPointDto" DataSource="@_userPoints"
Total="_total"
Loading="_loading"
PageIndex="@_pageIndex"
PageSize="@_pageSize"
OnPageIndexChange="OnPageIndexChanged"
OnPageSizeChange="OnPageSizeChange">
<PropertyColumn Property="c => c.Name" title="用户姓名"/>
<PropertyColumn Property="c => c.Point" title="积分"/>
</Table>
</PageContainer>
@code {
#region original form coding
public class Model
{
[Required]
public string Name { get; set; }
public string Mobile { get; set; }
}
private Model model = new Model();
private void OnFinishFailed(EditContext editContext)
{
Console.WriteLine($"Failed:{JsonSerializer.Serialize(model)}");
}
bool loading = false;
void toggle(bool value) => loading = value;
#endregion
#region original modal coding
bool _visible = false;
private void ShowModal()
{
_visible = true;
}
private void HandleCancel(MouseEventArgs e)
{
_visible = false;
}
#endregion
/*
* Careful!
*
* next bind submit event to modal OK button
*/
private Form<Model> _form;
/// <summary>
/// when form is submited, close the modal
/// </summary>
/// <param name="args"></param>
private void OnFinish(EditContext editContext)
{
Console.WriteLine("e");
_visible = false;
}
/// <summary>
/// on modal OK button is click, submit form manually
/// </summary>
/// <param name="e"></param>
private async Task HandleOk(MouseEventArgs e)
{
_form.Submit();
// TODO: 添加积分
await RefreshTable().ConfigureAwait(false);
}
}

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AntDesign;
using Microsoft.AspNetCore.Components;
using UserPointManagement.Application.Services;
using UserPointManagement.Model.Dtos.User;
using UserPointManagement.Model.Dtos.UserPoint;
namespace UserPointManagement.Web.Pages.UserPointPage;
public class UserPointManageBase : ComponentBase
{
[Inject] private IUserPointService _UserPointService { get; set; }
[Inject] private IUserService _UserService { get; set; }
protected List<UserPointDto> _userPoints;
protected List<UserDto> _users;
protected int _pageIndex = 1;
protected int _pageSize = 20;
protected int _total = 0;
protected bool _loading;
protected int? _selectedUserId;
protected override async Task OnInitializedAsync()
{
_users = await _UserService.GetAllUsers().ConfigureAwait(false);
await RefreshTable();
}
protected async Task OnSelectedItemChangedHandler(UserDto arg)
{
_selectedUserId = arg?.Id;
_pageIndex = 1;
await RefreshTable();
}
protected async Task RefreshTable()
{
_loading = true;
var res = await _UserPointService.GetUserPoints(new GetUserPointDto()
{
UserId = _selectedUserId,
PageIndex = _pageIndex,
PageSize = _pageSize
});
_userPoints = res.Items;
_total = res.TotalCount;
_loading = false;
}
protected async Task OnPageIndexChanged(PaginationEventArgs args)
{
_pageIndex = args.Page;
_pageSize = args.PageSize;
await RefreshTable();
}
protected async Task OnPageSizeChange(PaginationEventArgs args)
{
_pageIndex = args.Page;
_pageSize = args.PageSize;
await RefreshTable();
}
}

View File

@ -11,7 +11,6 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@ -19,7 +18,6 @@
"UserPointManagement.Web": {
"commandName": "Project",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},

View File

@ -30,7 +30,7 @@ namespace UserPointManagement.Web
services.AddServerSideBlazor();
services.AddAntDesign();
services.AddDbContext<UserPointManagementDbContext>(option =>
services.AddDbContextFactory<UserPointManagementDbContext>(option =>
{
option.UseNpgsql(Configuration["Connection:UserPointManagement"])
.UseSnakeCaseNamingConvention();

View File

@ -22,4 +22,11 @@ public class UserServiceTest
Assert.NotNull(res);
}
[Fact]
public async Task GetAllUsers()
{
var res = await _userService.GetAllUsers().ConfigureAwait(false);
Assert.NotEmpty(res);
}
}