用户表增加删除和获取所有的功能
This commit is contained in:
@ -1,14 +0,0 @@
|
||||
using WorkStation.Share;
|
||||
|
||||
namespace WorkStation.Server.Services
|
||||
{
|
||||
public interface IBaseSerivice<T>
|
||||
{
|
||||
Task<ApiResponse> AddAsync(T item);
|
||||
Task<ApiResponse> DelateAsync(int id);
|
||||
Task<ApiResponse> UpdateAsync(T item);
|
||||
Task<ApiResponse> GetByIdAsync(int id);
|
||||
Task<ApiResponse> GetAllAsync();
|
||||
|
||||
}
|
||||
}
|
45
WorkStation.Server/Services/IBaseService.cs
Normal file
45
WorkStation.Server/Services/IBaseService.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using WorkStation.Share;
|
||||
|
||||
namespace WorkStation.Server.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义了一个基础服务接口,用于规范数据操作的标准方法
|
||||
/// </summary>
|
||||
/// <typeparam name="T">表示此接口将针对的具体实体类型</typeparam>
|
||||
public interface IBaseService<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步添加一个实体项到数据存储中
|
||||
/// </summary>
|
||||
/// <param name="entity">要添加的实体项</param>
|
||||
/// <returns>返回一个包含添加操作结果的ApiResponse对象</returns>
|
||||
Task<ApiResponse> AddAsync(T entity);
|
||||
|
||||
/// <summary>
|
||||
/// 根据实体的ID异步删除一个实体项
|
||||
/// </summary>
|
||||
/// <param name="id">要删除实体项的标识ID</param>
|
||||
/// <returns>返回一个包含删除操作结果的ApiResponse对象</returns>
|
||||
Task<ApiResponse> DelateAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个实体项
|
||||
/// </summary>
|
||||
/// <param name="entity">包含更新后信息的实体项</param>
|
||||
/// <returns>返回一个包含更新操作结果的ApiResponse对象</returns>
|
||||
Task<ApiResponse> UpdateAsync(T entity);
|
||||
|
||||
/// <summary>
|
||||
/// 根据实体的ID异步获取一个实体项的详细信息
|
||||
/// </summary>
|
||||
/// <param name="id">要获取实体项的标识ID</param>
|
||||
/// <returns>返回一个包含指定实体项信息的ApiResponse对象</returns>
|
||||
Task<ApiResponse> GetByIdAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有实体项的列表
|
||||
/// </summary>
|
||||
/// <returns>返回一个包含所有实体项列表的ApiResponse对象</returns>
|
||||
Task<ApiRespon<List<T>>> GetAllAsync();
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace WorkStation.Server.Services
|
||||
{
|
||||
public interface IUserService : IBaseSerivice<User>
|
||||
public interface IUserService : IBaseService<User>
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user