用户表增加删除和获取所有的功能
This commit is contained in:
@ -5,5 +5,6 @@ namespace WorkStation.Server.Context.Repository
|
||||
{
|
||||
public class UserRepository(MyDbContext context) : Repository<User>(context), IRepository<User>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,10 @@ namespace WorkStation.Server.Controllers
|
||||
{
|
||||
private readonly IUserService _service = service;
|
||||
[HttpPost]
|
||||
public Task<ApiResponse> Add(User item) => _service.AddAsync(item);
|
||||
public async Task<ApiResponse> Add(User item) => await _service.AddAsync(item);
|
||||
[HttpDelete]
|
||||
public async Task<ApiResponse> Delete(int id) => await _service.DelateAsync(id);
|
||||
[HttpGet]
|
||||
public async Task<ApiRespon<List<User>>> GetAll() => await _service.GetAllAsync();
|
||||
}
|
||||
}
|
||||
|
@ -9,14 +9,26 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>е<EFBFBD>MySQL<51><4C><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
var connectionString = builder.Configuration.GetConnectionString("MySQLConnection");
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>MyDbContextʹ<74>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>
|
||||
builder.Services.AddDbContext<MyDbContext>(options =>
|
||||
{
|
||||
// ʹ<><CAB9>MySQL<51><4C><EFBFBD>ݿ⣬<DDBF><E2A3AC><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>汾
|
||||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
|
||||
});
|
||||
|
||||
// <20><><EFBFBD>ӵ<EFBFBD>ԪOfWork<72><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ύ
|
||||
builder.Services.AddUnitOfWork<MyDbContext>();
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ֿ⣬<D6BF><E2A3AC><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>Userʵ<72><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
||||
builder.Services.AddCustomRepository<User, UserRepository>();
|
||||
|
||||
// ע<><D7A2>UserServiceΪ˲̬<CBB2><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ᴴ<F3B6BCBB><E1B4B4>һ<EFBFBD><D2BB><EFBFBD>µ<EFBFBD>ʵ<EFBFBD><CAB5>
|
||||
builder.Services.AddTransient<IUserService, UserService>();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,49 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace WorkStation.Share
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义API响应的统一结构,包含操作是否成功、消息和数据。
|
||||
/// </summary>
|
||||
/// <param name="success">指示请求是否成功。</param>
|
||||
/// <param name="message">可选参数,提供响应的附加信息(如错误或成功提示)。</param>
|
||||
/// <param name="data">可选参数,包含返回的具体数据(根据API而变化)。</param>
|
||||
public class ApiResponse(bool success, string? message, object? data)
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置请求的成功状态。
|
||||
/// </summary>
|
||||
public bool Success { get; set; } = success;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置响应的消息内容。
|
||||
/// </summary>
|
||||
public string? Message { get; set; } = message;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置响应的数据内容。
|
||||
/// </summary>
|
||||
public object? Data { get; set; } = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 表示API响应的结果,包含成功状态、消息内容和数据内容。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">响应数据的类型。</typeparam>
|
||||
public class ApiRespon<T>(bool success, string? message, T? data)
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置请求的成功状态。
|
||||
/// </summary>
|
||||
public bool Success { get; set; } = success;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置响应的消息内容。
|
||||
/// </summary>
|
||||
public string? Message { get; set; } = message;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置响应的数据内容。
|
||||
/// </summary>
|
||||
public T? Data { get; set; } = data;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user