public void UpdateApiScope(int id, ApiScopeDto apiScopeDto) { ApiScope apiScope = this.Session.Get <ApiScope>(id); apiScope = apiScopeDto.ToEntity(apiScope); if (!CanInsertApiScope(apiScope)) { throw new FluentValidationException("ApiScope名称重复。"); } var transaction = this.Session.BeginTransaction(); try { this.Session.Update(apiScope); this.Session.CreateQuery("delete from ApiScopeClaim where ApiScopeId=:ApiScopeId") .SetInt32("ApiScopeId", id) .ExecuteUpdate(); apiScopeDto.UserClaims.ForEach(type => { ApiScopeClaim apiScopeClaim = new ApiScopeClaim(); apiScopeClaim.ApiScopeId = apiScope.Id; apiScopeClaim.Type = type; this.Session.Save(apiScopeClaim); }); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } }
public void AddApiScope(ApiScopeDto apiScopeDto) { ApiScope apiScope = apiScopeDto.ToEntity(); if (!CanInsertApiScope(apiScope)) { throw new FluentValidationException("ApiScope名称重复。"); } var transaction = this.Session.BeginTransaction(); try { this.Session.Save(apiScope); apiScopeDto.UserClaims.ForEach(type => { ApiScopeClaim apiScopeClaim = new ApiScopeClaim(); apiScopeClaim.ApiScopeId = apiScope.Id; apiScopeClaim.Type = type; this.Session.Save(apiScopeClaim); }); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } }
private ApiScope CreateApiScope() { var claim = new ApiScopeClaim(); var claims = new List <ApiScopeClaim>() { claim }; var scope = new ApiScope { ApiScopeClaims = claims }; claim.ApiScpope = scope; return(scope); }
public async Task <IActionResult> Create(ApiScopeClaimViewModel vm) { if (vm == null) { return(BadRequest()); } if (ModelState.IsValid) { var apiScope = await _dbContext.Set <ApiScope>().FindAsync(vm.ApiScopeId); if (apiScope == null) { return(BadRequest()); } var obj = new ApiScopeClaim { Type = vm.Type, ApiScope = apiScope }; _dbContext.Set <ApiScopeClaim>().Add(obj); try { await _dbContext.SaveChangesAsync(); _logger.LogInformation($"API scope claim Id {obj.Id} created by {User?.Identity?.Name}."); } catch (DbException ex) { _logger.LogError(ex.GetBaseException()?.Message ?? ex.Message); throw; } } return(RedirectToAction("Edit", "ApiScopes", new { id = vm.ApiScopeId })); }