Exemplo n.º 1
0
        public async Task <IActionResult> Add([FromBody] Scope request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            return(!await _scopeRepository.Insert(request, CancellationToken.None).ConfigureAwait(false)
                ? new StatusCodeResult(StatusCodes.Status500InternalServerError)
                : new NoContentResult());
        }
        public async Task <bool> Execute(AddScopeParameter addScopeParameter)
        {
            var json = addScopeParameter == null ? string.Empty : JsonConvert.SerializeObject(addScopeParameter);

            _umaServerEventSource.StartToAddScope(json);
            if (addScopeParameter == null)
            {
                throw new ArgumentNullException(nameof(addScopeParameter));
            }

            Scope scope = null;

            try
            {
                scope = await _scopeRepository.Get(addScopeParameter.Id);
            }
            catch (Exception ex)
            {
                throw new BaseUmaException(ErrorCodes.InternalError,
                                           ErrorDescriptions.TheScopeCannotBeRetrieved,
                                           ex);
            }

            if (scope != null)
            {
                throw new BaseUmaException(ErrorCodes.InvalidRequestCode,
                                           string.Format(ErrorDescriptions.TheScopeAlreadyExists, addScopeParameter.Id));
            }

            scope = new Scope
            {
                Id      = addScopeParameter.Id,
                IconUri = addScopeParameter.IconUri,
                Name    = addScopeParameter.Name
            };
            _scopeParameterValidator.CheckScopeParameter(scope);

            try
            {
                await _scopeRepository.Insert(scope);

                _umaServerEventSource.FinishToAddScope(json);
                return(true);
            }
            catch (Exception ex)
            {
                throw new BaseUmaException(ErrorCodes.InternalError,
                                           ErrorDescriptions.TheScopeCannotBeInserted,
                                           ex);
            }
        }