public async Task <IActionResult> Put(AddRolePermissionModel model) { if (await PerSvc.GetByNameAsync(model.Name) != null) { return(new JsonResult(new APIResult <int> { ErrorMsg = "该权限已存在" }) { StatusCode = 400 }); } AddRolePermissionDTO dto = new AddRolePermissionDTO(); dto.Name = model.Name; dto.Description = model.Description; return(new JsonResult(new APIResult <long> { Data = await PerSvc.AddNewAsync(dto) })); }
public async Task <long> AddNewAsync(AddRolePermissionDTO dto) { RoleEntity entity = new RoleEntity(); entity.Description = dto.Description; entity.Name = dto.Name; using (AdminUserContext ctx = new AdminUserContext()) { BaseService <RoleEntity> bs = new BaseService <RoleEntity>(ctx); var per = await bs.GetAll().SingleOrDefaultAsync(e => e.Name == dto.Name); if (per != null) { throw new Exception("角色名已存在"); } await ctx.Roles.AddAsync(entity); await ctx.SaveChangesAsync(); return(entity.Id); } }