用户表增加删除和获取所有的功能

This commit is contained in:
2025-07-08 17:32:28 +08:00
parent 547142730e
commit d08922030d
8 changed files with 195 additions and 27 deletions

View File

@ -6,23 +6,103 @@ namespace WorkStation.Server.Services
{
public class UserService(IUnitOfWork unitOfWork) : IUserService
{
private IUnitOfWork _unitOfWork = unitOfWork;
public async Task<ApiResponse> AddAsync(User item)
private readonly IUnitOfWork _unitOfWork = unitOfWork;
private readonly int _pageList = 100;
public async Task<ApiResponse> AddAsync(User entity)
{
var repository = _unitOfWork.GetRepository<User>();
await repository.InsertAsync(item);
var count = await _unitOfWork.SaveChangesAsync();
return new ApiResponse(true, "添加数据", count);
try
{
if (string.IsNullOrEmpty(entity.Account))
{
return new ApiResponse(false, "添加用户失败,账户无效", entity);
}
if (string.IsNullOrEmpty(entity.Password))
{
return new ApiResponse(false, "添加用户失败,密码无效", entity);
}
entity.CreateTime = DateTime.Now;
entity.UpdateTime = DateTime.Now;
entity.Id = 0;
var repository = _unitOfWork.GetRepository<User>();
await repository.InsertAsync(entity);
var count = await _unitOfWork.SaveChangesAsync();
if (count > 0)
{
return new ApiResponse(true, "添加用户成功", entity);
}
else
{
return new ApiResponse(false, "添加用户失败unitOfWork保存失败", entity);
}
}
catch (Exception ex)
{
return new ApiResponse(false, $"添加用户异常:{ex.Message}", null);
}
}
public Task<ApiResponse> DelateAsync(int id)
public async Task<ApiResponse> DelateAsync(int id)
{
throw new NotImplementedException();
try
{
if (id <= 0)
{
return new ApiResponse(false, "id = 无效", $"id = {id}");
}
var repository = _unitOfWork.GetRepository<User>();
var user = await repository.GetFirstOrDefaultAsync(predicate: item => item.Id == id);
if (user == null)
{
return new ApiResponse(false, "删除用户失败,没有查询到该用户", id);
}
repository.Delete(user);
var count = await _unitOfWork.SaveChangesAsync();
if (count > 0)
{
return new ApiResponse(true, "删除用户成功", $"id = {id}");
}
else
{
return new ApiResponse(false, "删除用户失败unitOfWork保存失败", $"id = {id}");
}
}
catch (Exception ex)
{
return new ApiResponse(false, $"删除用户异常:{ex.Message}", $"id = {id}");
}
}
public Task<ApiResponse> GetAllAsync()
public async Task<ApiRespon<List<User>>> GetAllAsync()
{
throw new NotImplementedException();
try
{
var repository = _unitOfWork.GetRepository<User>();
var pageIndex = 0;
var data = new List<User>();
while (true)
{
var users = await repository.GetPagedListAsync(pageIndex: pageIndex,
pageSize: _pageList,
orderBy: source => source.OrderByDescending(item => item.Id));
if (users == null)
{
return new ApiRespon<List<User>>(false, "获取失败GetPagedListAsync 结果为 null", null);
}
foreach(var item in users.Items)
{
data.Add(item);
}
if (!users.HasNextPage)
{
return new ApiRespon<List<User>>(true, "获取用户成功", data);
}
pageIndex++;
}
}
catch(Exception ex)
{
return new ApiRespon<List<User>>(false, $"获取用户异常:{ex.Message}", null);
}
}
public Task<ApiResponse> GetByIdAsync(int id)
@ -30,9 +110,10 @@ namespace WorkStation.Server.Services
throw new NotImplementedException();
}
public Task<ApiResponse> UpdateAsync(User item)
public Task<ApiResponse> UpdateAsync(User entity)
{
throw new NotImplementedException();
}
}
}