Ejemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] CreateViewBody body)
        {
            if (FromArgumentIsNullOrContainsPlaceholder(body.Query))
            {
                return(BadRequest("Data type (FROM-argument) must be specified in query and cannot be a placeholder"));
            }
            var dataType = DetermineViewCollection(body.Query);

            // Authroize
            var loggedInUsername    = UsernameNormalizer.Normalize(HttpContext.User.Identity.Name);
            var resourceDescription = new CreateViewResourceDescription(dataType);
            var authorizationResult = await authorizationModule.AuthorizeAsync(resourceDescription, loggedInUsername);

            if (!authorizationResult.IsAuthorized)
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, "Not authorized"));
            }

            try
            {
                var viewInformation = await viewManager.CreateViewAsync(body, authorizationResult.User.UserName);

                apiEventLogger.Log(LogLevel.Info, $"User '{authorizationResult.User.UserName}' added view with ID '{viewInformation.ViewId}'");
                return(new ContentResult
                {
                    ContentType = Conventions.JsonContentType,
                    Content = JsonConvert.SerializeObject(viewInformation),
                    StatusCode = (int)HttpStatusCode.OK
                });
            }
            catch (DocumentAlreadyExistsException)
            {
                return(Conflict($"View with name '{body.ViewId}' already exists"));
            }
            catch (Exception e)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message));
            }
        }