feat: 添加新增用户

master
zhangyousheng 2023-07-29 12:05:53 +08:00
parent 7e2ba5996f
commit 102f150fa4
5 changed files with 207 additions and 82 deletions

View File

@ -9,6 +9,8 @@ namespace UserPointManagement.Application.Services;
public interface IUserService public interface IUserService
{ {
Task<PageResultDto<User>> GetUsers(GetUserDto req); Task<PageResultDto<User>> GetUsers(GetUserDto req);
Task CreateUser(User input);
} }
public class UserService : IUserService public class UserService : IUserService
@ -34,4 +36,10 @@ public class UserService : IUserService
TotalCount = count TotalCount = count
}; };
} }
public async Task CreateUser(User input)
{
_userPointManagementDbContext.Users.Add(input);
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
}
} }

View File

@ -0,0 +1,137 @@
@inherits MyAntDesignAppServer.Pages.UserManagement.UserManagementBase
@page "/"
@using UserPointManagement.Application.Services
@using UserPointManagement.Model.Dtos
@using UserPointManagement.Model.Entities
@using System.ComponentModel.DataAnnotations
@using System.Text.Json
@inject IJSRuntime JS
@inject IUserService UserService;
<PageContainer Title="用户管理">
<GridRow Style="margin: 10px 0">
<GridCol Span="12">
<Space Direction="@DirectionVHType.Horizontal">
<SpaceItem>
<Search Placeholder="姓名或手机号" WrapperStyle="width: 200px;" OnSearch="OnSearch" ClassicSearchIcon/>
</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="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>
<Modal Title="@("新增用户")"
Visible="@_visible"
OnOk="@HandleOk"
OnCancel="@HandleCancel">
<Form Loading="loading" Model="@model"
LabelColSpan="8"
WrapperColSpan="16"
OnFinish="OnFinish"
OnFinishFailed="OnFinishFailed"
@ref="@_form">
<FormItem Label="姓名">
<Input @bind-Value="@context.Name" />
</FormItem>
<FormItem Label="手机号">
<Input @bind-Value="@context.Mobile" />
</FormItem>
</Form>
</Modal>
@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();
await UserService.CreateUser(new User()
{
Name = model.Name,
Mobile = model.Mobile
}).ConfigureAwait(false);
await RefreshTable().ConfigureAwait(false);
}
}

View File

@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AntDesign;
using Microsoft.AspNetCore.Components;
using UserPointManagement.Application.Services;
using UserPointManagement.Model.Dtos;
using UserPointManagement.Model.Entities;
namespace MyAntDesignAppServer.Pages.UserManagement;
public class UserManagementBase : ComponentBase
{
[Inject] private IUserService _userService { get; set; }
protected List<User> _users;
protected int _pageIndex = 1;
protected int _pageSize = 20;
protected int _total = 0;
protected bool _loading;
private string _searchValue;
protected override async Task OnInitializedAsync()
{
await RefreshTable();
}
protected async Task OnSearch(string arg)
{
_searchValue = arg;
_pageIndex = 1;
await RefreshTable();
}
protected 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;
}
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

@ -1,82 +0,0 @@
@page "/"
@using UserPointManagement.Application.Services
@using UserPointManagement.Model.Dtos
@using UserPointManagement.Model.Entities
@inject IUserService _userService;
@inject IJSRuntime JS
<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();
}
}