private async Task RemoveApiResourceScopesAsync(ApiResource identityResource)
        {
            //Remove old api resource scopes
            var apiResourceScopes = await DbContext.ApiResourceScopes.Where(x => x.ApiResource.Id == identityResource.Id).ToListAsync();

            DbContext.ApiResourceScopes.RemoveRange(apiResourceScopes);
        }
        public async Task <IActionResult> OnGetAsync(int apiResourceId, int id)
        {
            TenantId      = _sessionTenantAccessor.TenantId;
            ApiResourceId = apiResourceId;


            Entity = await _adminServices.GetApiResourceByIdAsync(TenantId, ApiResourceId);

            var scopeEntity = Entity.Scopes.FirstOrDefault(x => x.Id == id);

            if (scopeEntity == null)
            {
                return(RedirectToPage("../Index", new { id = ApiResourceId }));
            }

            var subScope = scopeEntity.Scope.Substring(Entity.Name.Length + 1);

            Input = new InputModel()
            {
                Id          = id,
                PrependName = Entity.Name,
                Scope       = subScope
            };
            return(Page());
        }
        public virtual async Task <int> DeleteApiResourceAsync(ApiResource apiResource)
        {
            var resource = await DbContext.ApiResources.Where(x => x.Id == apiResource.Id).SingleOrDefaultAsync();

            DbContext.Remove(resource);

            return(await AutoSaveChangesAsync());
        }
        /// <summary>
        /// Add new api resource
        /// </summary>
        /// <param name="apiResource"></param>
        /// <returns>This method return new api resource id</returns>
        public virtual async Task <int> AddApiResourceAsync(ApiResource apiResource)
        {
            DbContext.ApiResources.Add(apiResource);

            await AutoSaveChangesAsync();

            return(apiResource.Id);
        }
        public virtual async Task <int> UpdateApiResourceAsync(ApiResource apiResource)
        {
            //Remove old relations
            await RemoveApiResourceClaimsAsync(apiResource);
            await RemoveApiResourceScopesAsync(apiResource);

            //Update with new data
            DbContext.ApiResources.Update(apiResource);

            return(await AutoSaveChangesAsync());
        }
        public async Task OnGetAsync(int id)
        {
            TenantId      = _sessionTenantAccessor.TenantId;
            ApiResourceId = id;
            Entity        = await _adminServices.GetApiResourceByIdAsync(TenantId, ApiResourceId);

            Input = new InputModel()
            {
                PrependName = Entity.Name
            };
        }
        public virtual async Task <bool> CanInsertApiResourceAsync(ApiResource apiResource)
        {
            if (apiResource.Id == 0)
            {
                var existsWithSameName = await DbContext.ApiResources.Where(x => x.Name == apiResource.Name).SingleOrDefaultAsync();

                return(existsWithSameName == null);
            }
            else
            {
                var existsWithSameName = await DbContext.ApiResources.Where(x => x.Name == apiResource.Name && x.Id != apiResource.Id).SingleOrDefaultAsync();

                return(existsWithSameName == null);
            }
        }
        public async Task <IActionResult> OnPostAsync()
        {
            try
            {
                var existingEntity = await _adminServices.GetApiResourceByNameAsync(TenantId, Input.Name);

                if (existingEntity != null)
                {
                    ModelState.AddModelError(string.Empty, $"ApiResource {Input.Name} already exists, please pick another name.");
                    return(Page());
                }
                var entity = new Duende.IdentityServer.EntityFramework.Entities.ApiResource()
                {
                    Name        = Input.Name,
                    Description = Input.Description,
                    Enabled     = Input.Enabled,
                    Scopes      = new List <ApiResourceScope>()
                    {
                        new ApiResourceScope()
                        {
                            Scope = Input.Name
                        }
                    }
                };
                await _adminServices.UpsertApiResourceAsync(TenantId, entity);

                var entityInDb = await _adminServices.GetApiResourceByNameAsync(TenantId, Input.Name);

                await _adminServices.UpsertApiResourceScopeAsync(TenantId, entityInDb.Id, new ApiResourceScope()
                {
                    Scope = Input.Name
                });

                return(RedirectToPage("./ApiResource/Index", new { id = entityInDb.Id }));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return(Page());
            }
        }