private async Task <Response <string> > AddAsync(ApiScopeViewModel viewModel) { //判断 var identityResource = await _configurationDbContext.ApiScopes.FirstOrDefaultAsync(d => d.Name == viewModel.Name); if (identityResource != null) { return(new Response <string>(success: false, msg: "认证资源名称已存在!") { Data = "认证资源名称已存在!" }); } identityResource = new IdentityServer4.EntityFramework.Entities.ApiScope { Name = viewModel.Name, DisplayName = viewModel.DisplayName, Description = viewModel.Description, }; await _configurationDbContext.ApiScopes.AddAsync(identityResource); var result = await _configurationDbContext.SaveChangesAsync(); if (result < 1) { return(new Response <string>(success: false, msg: "注册失败,请重试!") { Data = "注册失败,请重试!" }); } return(new Response <string>(msg: "注册成功!") { Data = "注册成功!" }); }
private async Task <Response <string> > UpdateAsync(int id, ApiScopeViewModel viewModel) { //判断 var identityResource = await _configurationDbContext.ApiScopes.FirstOrDefaultAsync(d => d.Name == viewModel.Name && d.Id != id); if (identityResource != null) { return(new Response <string>(success: false, msg: "认证资源名称已存在!") { Data = "认证资源名称已存在!" }); } identityResource = viewModel; _configurationDbContext.ApiScopes.Update(identityResource); var result = await _configurationDbContext.SaveChangesAsync(); if (result < 1) { return(new Response <string>(success: false, msg: "修改失败,请重试!") { Data = "修改失败,请重试!" }); } return(new Response <string>(msg: "修改成功!") { Data = "修改成功!" }); }
public async Task <IActionResult> Edit(ApiScopeViewModel vm) { if (vm == null) { return(BadRequest()); } if (ModelState.IsValid) { var apiScope = await _dbContext.Set <ApiScope>().FindAsync(vm.Id); apiScope.Name = vm.Name; apiScope.DisplayName = vm.DisplayName; apiScope.Description = vm.Description; apiScope.Required = vm.Required; apiScope.ShowInDiscoveryDocument = vm.ShowInDiscoveryDocument; apiScope.Emphasize = vm.Emphasize; try { await _dbContext.SaveChangesAsync(); _logger.LogInformation($"API scope Id {apiScope.Id} updated by {User?.Identity?.Name}."); return(RedirectToAction("Edit", "ApiResources", new { id = vm.ApiResourceId })); } catch (DbException ex) { _logger.LogError(ex.GetBaseException()?.Message ?? ex.Message); throw; } } return(View(vm)); }
public async Task <IActionResult> Create(ApiScopeViewModel vm) { if (vm == null) { return(BadRequest()); } if (ModelState.IsValid) { var apiResource = await _dbContext.Set <ApiResource>().FindAsync(vm.ApiResourceId); if (apiResource == null) { return(BadRequest()); } var obj = GetApiScope(vm, apiResource); _dbContext.Set <ApiScope>().Add(obj); try { await _dbContext.SaveChangesAsync(); _logger.LogInformation($"API scope Id {obj.Id} created by {User?.Identity?.Name}."); return(RedirectToAction("Edit", "ApiResources", new { id = vm.ApiResourceId })); } catch (DbException ex) { _logger.LogError(ex.GetBaseException()?.Message ?? ex.Message); throw; } } return(View(vm)); }
public async Task <IActionResult> Delete(ApiScopeViewModel vm) { if (vm == null) { return(BadRequest()); } var apiScope = await _dbContext.Set <ApiScope>().FindAsync(vm.Id); if (apiScope == null) { return(NotFound()); } _dbContext.Set <ApiScope>().Remove(apiScope); try { await _dbContext.SaveChangesAsync(); _logger.LogInformation($"API scope Id {vm.Id} removed by {User?.Identity?.Name}."); return(RedirectToAction("Edit", "ApiResources", new { id = vm.ApiResourceId })); } catch (DbException ex) { _logger.LogError(ex.GetBaseException()?.Message ?? ex.Message); throw; } }
public async Task <Response <string> > Save([FromBody] ApiScopeViewModel viewModel) { var result = new Response <string>(); if (viewModel.Id > 0) { result = await UpdateAsync(viewModel.Id, viewModel); } else { result = await AddAsync(viewModel); } return(result); }
private async Task <ApiScopeViewModel> GetByIdAsync(int id) { var viewModel = new ApiScopeViewModel(); var model = await _configurationDbContext.ApiScopes.FirstOrDefaultAsync(d => d.Id == id); if (model != null) { viewModel = new ApiScopeViewModel { Id = model.Id, Name = model.Name, DisplayName = model.DisplayName, Description = model.Description, }; } return(viewModel); }
private ApiScope GetApiScope(ApiScopeViewModel vm, ApiResource apiResource) { Contract.Requires(apiResource != null); if (vm != null) { return(new ApiScope { Name = vm.Name, DisplayName = vm.DisplayName, Description = vm.Description, Required = vm.Required, ShowInDiscoveryDocument = vm.ShowInDiscoveryDocument, Emphasize = vm.Emphasize, ApiResource = apiResource }); } return(null); }