• <二>实现用户的增删改查接口


    一、设计好权限之后,我们把所有表的增删改查的基础接口设计好,先把用户的接口实现了。使用我们以前练习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的用户已经没有了

  • 相关阅读:
    go资料
    typescript 装饰器 decorator
    【转】typescript class decorator装饰器
    【转】Best way to get result count before LIMIT was applied
    DirectX11 采样状态对象
    React通过redux-persist持久化数据存储
    禁止网页查看源代码
    css3 动画 -- 加载动画 Loader
    css3 动画 -- 旋转线条 rotate_line
    读书打卡:《微信互联网平民创业》
  • 原文地址:https://www.cnblogs.com/choii/p/16037612.html
Copyright © 2020-2023  润新知