コード例 #1
0
        public async Task AddAsync(ApiResource apiResource)
        {
            var dbApiResource = await FindApiResourceAsync(apiResource.Name);

            if (dbApiResource != null)
            {
                var resourceName = apiResource.Name;
                logger.LogError("Could not add ApiResource - Reason: Name={resourceName} already exists.", resourceName);
                throw new InvalidOperationException($"ApiResource with Name={apiResource.Name} already exists.");
            }

            var entity = apiResource.MapApiResources();

            await using var con = new SqlConnection(_connectionString);
            await con.OpenAsync();

            await using var t = await con.BeginTransactionAsync();

            //added  Created , Updated, LastAccessed, NonEditable
            var apiId = await con.ExecuteScalarAsync <int>($"insert into {_options.DbSchema}.ApiResources ([Description],DisplayName,Enabled,[Name], Created , Updated, LastAccessed, NonEditable) values (@Description,@DisplayName,@Enabled,@Name, @Created , @Updated, @LastAccessed, @NonEditable);{_options.GetLastInsertID}", new
            {
                entity.Description,
                entity.DisplayName,
                entity.Enabled,
                entity.Name,
                entity.Created,
                entity.Updated,
                entity.LastAccessed,
                entity.NonEditable
            }, commandType : CommandType.Text, transaction : t);

            entity.Id = apiId;

            if (apiResource.UserClaims != null && apiResource.UserClaims.Any())
            {
                foreach (var item in apiResource.UserClaims)
                {
                    await InsertApiResourceClaim(item, entity.Id, con, t);
                }
            }
            if (apiResource.ApiSecrets != null && apiResource.ApiSecrets.Any())
            {
                foreach (var item in apiResource.ApiSecrets)
                {
                    await InsertApiSecretsByApiResourceId(item, apiId, con, t);
                }
            }
            if (apiResource.Scopes != null && apiResource.Scopes.Any())
            {
                await InsertApiScopeByApiResourceId(apiResource.Scopes, entity.Id, con, t);
            }
            t.Commit();
        }
        public async Task UpdateAsync(ApiResource apiResource)
        {
            var dbItem = await GetByName(apiResource.Name);

            if (dbItem == null)
            {
                throw new InvalidOperationException($"Can not find ApiResource for Name={apiResource.Name}.");
            }

            var updateEntity = apiResource.MapApiResources();

            updateEntity.Id     = dbItem.Id;
            await using var con = new SqlConnection(_connectionString);
            await con.OpenAsync();

            await using var t = await con.BeginTransactionAsync();

            try
            {
                var ret = await con.ExecuteAsync($"update {_options.DbSchema}.ApiResources set [Description] = @Description," +
                                                 "DisplayName=@DisplayName," +
                                                 "Enabled=@Enabled," +
                                                 "[Name]=@Name where Id=@Id;", updateEntity, commandType : CommandType.Text, transaction : t);

                await UpdateScopesByApiResourceId(apiResource.Scopes, updateEntity, con, t);
                await UpdateApiResourceSecretsByApiResourceId(apiResource.ApiSecrets, updateEntity.Id, con, t);
                await UpdateClaimsByApiResourceId(apiResource.UserClaims, updateEntity.Id, con, t);

                await t.CommitAsync();
            }
            catch (Exception)
            {
                await t.RollbackAsync();

                throw;
            }
        }