Esempio n. 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());
        }