Пример #1
0
        public async Task <ClientDTO> AddClientAsync(ClientDTO dto)
        {
            if (!await CanInsertAsync(dto))
            {
                throw new Exception(Constants.Errors.CantInsert);
            }

            PrepareClientTypeForNewClient(dto);
            var entity = dto.ToEntity();

            if (entity.AllowedGrantTypes != null)
            {
                var grantTypes = Constants.Client.GrantTypes;
                var notInScope = entity.AllowedGrantTypes
                                 .Select(a => a.GrantType)
                                 .Except(grantTypes)
                                 .Any();
                if (notInScope)
                {
                    throw new Exception(Constants.Errors.InvalidGrantType);
                }
            }

            await Clients.AddAsync(entity);

            await Context.SaveChangesAsync();

            return(entity.ToDTO());
        }
        /// <summary>
        /// Add new identity resource
        /// </summary>
        /// <param name="identityResource"></param>
        /// <returns>This method return new identity resource id</returns>
        public async Task <IdentityResourceDTO> AddIdentityResourceAsync(IdentityResourceDTO dto)
        {
            var entity    = dto.ToEntity();
            var canInsert = await CanInsertIdentityResourceAsync(entity);

            if (!canInsert)
            {
                throw new Exception(Constants.Errors.CantInsert);
            }

            IdentityResources.Add(entity);

            await Context.SaveChangesAsync();

            return(entity.ToDTO());
        }