feat: 完善修改逻辑

master
zhangyousheng 2023-07-29 12:47:58 +08:00
parent c346691b54
commit f1e31e04e2
6 changed files with 110 additions and 14 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using UserPointManagement.Model; using UserPointManagement.Model;
using UserPointManagement.Model.Dtos; using UserPointManagement.Model.Dtos;
using UserPointManagement.Model.Dtos.User;
using UserPointManagement.Model.Entities; using UserPointManagement.Model.Entities;
using UserPointManagement.Persistence; using UserPointManagement.Persistence;
@ -13,6 +14,8 @@ public interface IUserService
Task CreateUser(User input); Task CreateUser(User input);
Task DeleteUser(int userId); Task DeleteUser(int userId);
Task ModifyUser(int userId, ModifyUserDto input);
} }
public class UserService : IUserService public class UserService : IUserService
@ -53,4 +56,16 @@ public class UserService : IUserService
_userPointManagementDbContext.Users.Remove(user); _userPointManagementDbContext.Users.Remove(user);
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false); await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
} }
public async Task ModifyUser(int userId, ModifyUserDto input)
{
var user = await _userPointManagementDbContext.Users.FirstOrDefaultAsync(x => x.Id == userId)
.ConfigureAwait(false);
user.Mobile = input.Mobile;
user.Name = input.Name;
_userPointManagementDbContext.Users.Update(user);
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
}
} }

View File

@ -1,4 +1,4 @@
namespace UserPointManagement.Model.Dtos; namespace UserPointManagement.Model.Dtos.User;
public class GetUserDto : PageBase public class GetUserDto : PageBase
{ {

View File

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

View File

@ -31,15 +31,49 @@
PageSize="@_pageSize" PageSize="@_pageSize"
OnPageIndexChange="OnPageIndexChanged" OnPageIndexChange="OnPageIndexChanged"
OnPageSizeChange="OnPageSizeChange"> OnPageSizeChange="OnPageSizeChange">
<PropertyColumn Property="c => c.Name" title="姓名"/> <ChildContent Context="data">
<PropertyColumn Property="c => c.Mobile" title="手机号" Width="80"/> <AntDesign.Column TData="string" Title="姓名">
<ActionColumn Title="操作" Width="220"> @if (!editCache[data.Id].edit)
<Space Size=@("middle")> {
<SpaceItem> @data.Name
<SpaceItem><Button Danger OnClick="()=>Delete(context.Id)">Delete</Button></SpaceItem> }
</SpaceItem> else
</Space> {
</ActionColumn> <Input @bind-Value="editCache[data.Id].data.Name"/>
}
</AntDesign.Column>
<AntDesign.Column TData="string" Title="手机号">
@if (!editCache[data.Id].edit)
{
@data.Mobile
}
else
{
<Input @bind-Value="editCache[data.Id].data.Mobile"/>
}
</AntDesign.Column>
<ActionColumn Title="操作" Width="220">
<Space Size=@("middle")>
<SpaceItem>
@if (!editCache[data.Id].edit)
{
<Button Type="Primary" OnClick="() => startEdit(data.Id)">编辑</Button>
}
else
{
<Button Type="Primary" OnClick="() => saveEdit(data.Id)">保存</Button>
<Popconfirm Title="Sure to cancel?"
OnConfirm="() => cancelEdit(data.Id)"
OkText="Yes"
CancelText="No">
<Button>取消</Button>
</Popconfirm>
}
<Button Danger OnClick="() => Delete(data.Id)">删除</Button>
</SpaceItem>
</Space>
</ActionColumn>
</ChildContent>
</Table> </Table>
</PageContainer> </PageContainer>
@ -54,10 +88,10 @@
OnFinishFailed="OnFinishFailed" OnFinishFailed="OnFinishFailed"
@ref="@_form"> @ref="@_form">
<FormItem Label="姓名"> <FormItem Label="姓名">
<Input @bind-Value="@context.Name" /> <Input @bind-Value="@context.Name"/>
</FormItem> </FormItem>
<FormItem Label="手机号"> <FormItem Label="手机号">
<Input @bind-Value="@context.Mobile" /> <Input @bind-Value="@context.Mobile"/>
</FormItem> </FormItem>
</Form> </Form>
</Modal> </Modal>
@ -65,10 +99,12 @@
@code { @code {
#region original form coding #region original form coding
public class Model public class Model
{ {
[Required] [Required]
public string Name { get; set; } public string Name { get; set; }
public string Mobile { get; set; } public string Mobile { get; set; }
} }
@ -133,4 +169,5 @@
}).ConfigureAwait(false); }).ConfigureAwait(false);
await RefreshTable().ConfigureAwait(false); await RefreshTable().ConfigureAwait(false);
} }
} }

View File

@ -1,9 +1,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AntDesign; using AntDesign;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using UserPointManagement.Application.Services; using UserPointManagement.Application.Services;
using UserPointManagement.Model.Dtos; using UserPointManagement.Model.Dtos;
using UserPointManagement.Model.Dtos.User;
using UserPointManagement.Model.Entities; using UserPointManagement.Model.Entities;
namespace MyAntDesignAppServer.Pages.UserManagement; namespace MyAntDesignAppServer.Pages.UserManagement;
@ -11,7 +13,10 @@ namespace MyAntDesignAppServer.Pages.UserManagement;
public class UserManagementBase : ComponentBase public class UserManagementBase : ComponentBase
{ {
[Inject] private IUserService _userService { get; set; } [Inject] private IUserService _userService { get; set; }
protected IDictionary<int, (bool edit, User data)> editCache =
new Dictionary<int, (bool edit, User data)>();
protected List<User> _users; protected List<User> _users;
protected int _pageIndex = 1; protected int _pageIndex = 1;
protected int _pageSize = 20; protected int _pageSize = 20;
@ -43,6 +48,12 @@ public class UserManagementBase : ComponentBase
_users = res.Items; _users = res.Items;
_total = res.TotalCount; _total = res.TotalCount;
_users.ForEach(item =>
{
editCache[item.Id] = (false, item);
});
_loading = false; _loading = false;
} }
@ -59,10 +70,34 @@ public class UserManagementBase : ComponentBase
_pageSize = args.PageSize; _pageSize = args.PageSize;
await RefreshTable(); await RefreshTable();
} }
protected async Task Delete(int userId) protected async Task Delete(int userId)
{ {
await _userService.DeleteUser(userId).ConfigureAwait(false); await _userService.DeleteUser(userId).ConfigureAwait(false);
await RefreshTable(); await RefreshTable();
} }
protected void startEdit(int id)
{
var data = editCache[id];
editCache[id] = (true, data.data ); // add a copy in cache
}
protected void cancelEdit(int id)
{
var data = _users.FirstOrDefault(item => item.Id == id);
editCache[id] = (false, data); // recovery
}
protected async Task saveEdit(int id)
{
var index = _users.FindIndex(item => item.Id == id);
_users[index] = editCache[id].data; // apply the copy to data source
await _userService.ModifyUser(editCache[id].data.Id, new ModifyUserDto()
{
Mobile = editCache[id].data.Mobile,
Name = editCache[id].data.Name,
}).ConfigureAwait(false);
editCache[id] = (false, _users[index]); // don't affect rows in editing
}
} }

View File

@ -1,6 +1,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using UserPointManagement.Application.Services; using UserPointManagement.Application.Services;
using UserPointManagement.Model.Dtos; using UserPointManagement.Model.Dtos;
using UserPointManagement.Model.Dtos.User;
using Xunit; using Xunit;
namespace UserPointManagement.Application.Tests.Services; namespace UserPointManagement.Application.Tests.Services;