<二>实现用户的增删改查接口
一、设计好权限之后,我们把所有表的增删改查的基础接口设计好,先把用户的接口实现了。使用我们以前练习Minimal Api 的框架作为api框架。
1、新增接口
- 代码如下
//新增用户接口 app.MapPost("/api/AddUser", (HttpContext ctx, AddInputDto dto) => { using (var serviceScope = app.ServiceProvider.CreateScope()) { ValidationResult result = ctx.Request.Validate<AddInputDto>(dto); if (!result.IsValid) { return new HttpResult<string>() { code = (int)HttpReturnType.ValidFalid, message = string.Join(",", result.Errors.Select(s => s.ErrorMessage).ToList()), data = " " }; } //这里要使用serviceScope.ServiceProvider AutoMapper.IMapper map = serviceScope.ServiceProvider.GetRequiredService<IMapper>(); sysUserRepository = serviceScope.ServiceProvider.GetRequiredService<ISysUserRepository>(); SysUser user = map.Map<SysUser>(dto); if (user != null) { user.Id = Guid.NewGuid().ToString().Replace("_", ""); sysUserRepository.Insert<SysUser>(user); return new HttpResult<string>() { code = (int)HttpReturnType.Success, message = "新增成功。", data = " " }; } return new HttpResult<string>() { code = (int)HttpReturnType.Fail, message = "新增失败。", data = " " }; } });
- 运行结果
2、查询接口
- 代码如下
//查询用户接口 app.MapGet("/api/GetUser", () => { using (var serviceScope = app.ServiceProvider.CreateScope()) { //这里要使用serviceScope.ServiceProvider AutoMapper.IMapper map = serviceScope.ServiceProvider.GetRequiredService<IMapper>(); sysUserRepository = serviceScope.ServiceProvider.GetRequiredService<ISysUserRepository>(); List<SysUser> user= sysUserRepository.IQueryable<SysUser>().ToList(); List<SysUserOutputDto> dto= map.Map<List<SysUserOutputDto>>(user); return new HttpResult<List<SysUserOutputDto>>() { code = 200, message = "查询成功。",data= dto }; } });
- 运行结果
3、修改接口
- 代码如下
//修改用户接口 app.MapPost("/api/UpdateUser", (HttpContext ctx, UpdateInputDto dto) => { using (var serviceScope = app.ServiceProvider.CreateScope()) { ValidationResult result = ctx.Request.Validate<UpdateInputDto>(dto); if (!result.IsValid) { return new HttpResult<string>() { code = (int)HttpReturnType.ValidFalid, message = string.Join(",", result.Errors.Select(s => s.ErrorMessage).ToList()), data = " " }; } //这里要使用serviceScope.ServiceProvider AutoMapper.IMapper map = serviceScope.ServiceProvider.GetRequiredService<IMapper>(); sysUserRepository = serviceScope.ServiceProvider.GetRequiredService<ISysUserRepository>(); SysUser user = map.Map<SysUser>(dto); if (user != null) { sysUserRepository.Update<SysUser>(user); return new HttpResult<string>() { code = (int)HttpReturnType.Success, message = "更新成功。", data = " " }; } return new HttpResult<string>() { code = (int)HttpReturnType.Fail, message = "更新失败。", data = " " }; } });
- 运行如下
- 查看一下
4、删除接口
- 代码如下
app.MapDelete("/api/DeleteUser", (HttpContext ctx,[FromBody]DeleteInputDto dto) => { using (var serviceScope = app.ServiceProvider.CreateScope()) { ValidationResult result = ctx.Request.Validate<DeleteInputDto>(dto); if (!result.IsValid) { return new HttpResult<string>() { code = (int)HttpReturnType.ValidFalid, message = string.Join(",", result.Errors.Select(s => s.ErrorMessage).ToList()),data=" " }; } sysUserRepository = serviceScope.ServiceProvider.GetRequiredService<ISysUserRepository>(); Core.Entity.SysManageSetting.SysUser user = sysUserRepository.FindEntity<SysUser>(dto.Id); if (user != null) { sysUserRepository.Delete<SysUser>(user); return new HttpResult<string>() { code = (int)HttpReturnType.Success, message = "删除成功。", data = " " }; } return new HttpResult<string>() { code = (int)HttpReturnType.Fail, message = "删除失败。", data = " " }; } });
- 运行结果
- 查询结果,Test999的用户已经没有了