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 UserPointManagement.Model;
using UserPointManagement.Model.Dtos;
using UserPointManagement.Model.Dtos.User;
using UserPointManagement.Model.Entities;
using UserPointManagement.Persistence;
@ -13,6 +14,8 @@ public interface IUserService
Task CreateUser(User input);
Task DeleteUser(int userId);
Task ModifyUser(int userId, ModifyUserDto input);
}
public class UserService : IUserService
@ -53,4 +56,16 @@ public class UserService : IUserService
_userPointManagementDbContext.Users.Remove(user);
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
{

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"
OnPageIndexChange="OnPageIndexChanged"
OnPageSizeChange="OnPageSizeChange">
<PropertyColumn Property="c => c.Name" title="姓名"/>
<PropertyColumn Property="c => c.Mobile" title="手机号" Width="80"/>
<ChildContent Context="data">
<AntDesign.Column TData="string" Title="姓名">
@if (!editCache[data.Id].edit)
{
@data.Name
}
else
{
<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>
<SpaceItem><Button Danger OnClick="()=>Delete(context.Id)">Delete</Button></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>
</PageContainer>
@ -54,10 +88,10 @@
OnFinishFailed="OnFinishFailed"
@ref="@_form">
<FormItem Label="姓名">
<Input @bind-Value="@context.Name" />
<Input @bind-Value="@context.Name"/>
</FormItem>
<FormItem Label="手机号">
<Input @bind-Value="@context.Mobile" />
<Input @bind-Value="@context.Mobile"/>
</FormItem>
</Form>
</Modal>
@ -65,10 +99,12 @@
@code {
#region original form coding
public class Model
{
[Required]
public string Name { get; set; }
public string Mobile { get; set; }
}
@ -133,4 +169,5 @@
}).ConfigureAwait(false);
await RefreshTable().ConfigureAwait(false);
}
}

View File

@ -1,9 +1,11 @@
using System.Collections.Generic;
using System.Linq;
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;
@ -12,6 +14,9 @@ public class UserManagementBase : ComponentBase
{
[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 int _pageIndex = 1;
protected int _pageSize = 20;
@ -43,6 +48,12 @@ public class UserManagementBase : ComponentBase
_users = res.Items;
_total = res.TotalCount;
_users.ForEach(item =>
{
editCache[item.Id] = (false, item);
});
_loading = false;
}
@ -65,4 +76,28 @@ public class UserManagementBase : ComponentBase
await _userService.DeleteUser(userId).ConfigureAwait(false);
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 UserPointManagement.Application.Services;
using UserPointManagement.Model.Dtos;
using UserPointManagement.Model.Dtos.User;
using Xunit;
namespace UserPointManagement.Application.Tests.Services;