Exemplo n.º 1
0
        public async Task <ActionResult <QuerySaveResponseDTO> > Save(
            string id,
            [FromBody] QuerySaveDTO querySave,
            CancellationToken cancelToken
            )
        {
            try
            {
                // federated user submitting an initial save request
                if (!user.IsInstutional && QueryUrn.From(querySave.UniversalId) == null)
                {
                    return(BadRequest("Initial save requests must be made to home node."));
                }

                var result = await manager.SaveAsync(new Guid(id), querySave, QueryDefinitionDTO.JSON, cancelToken);

                switch (result.State)
                {
                case QueryManager.SaveState.Preflight:
                    return(BadRequest(new QuerySaveResponseDTO {
                        Preflight = new PreflightCheckDTO(result.Preflight)
                    }));

                case QueryManager.SaveState.NotFound:
                    return(NotFound());
                }

                return(Ok(new QuerySaveResponseDTO
                {
                    Preflight = new PreflightCheckDTO(result.Preflight),
                    Query = new QuerySaveResultDTO(result.Result)
                }));
            }
            catch (InvalidOperationException ie)
            {
                log.LogError("Unrecoverable validation error in query. Error:{Error}", ie.Message);
                return(BadRequest());
            }
            catch (FormatException fe)
            {
                log.LogError("Malformed query identifier. Id:{Id} UniversalId:{UniversalId} Error:{Error}", id, querySave.UniversalId, fe.Message);
                return(BadRequest());
            }
            catch (OperationCanceledException)
            {
                log.LogInformation("Request cancelled.");
                return(NoContent());
            }
            catch (LeafRPCException lde)
            {
                return(StatusCode(lde.StatusCode));
            }
            catch (Exception e)
            {
                log.LogError("Could not save query. QueryId:{QueryId} Query:{@Query} Error:{Error}", id, querySave, e.ToString());
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <QuerySaveResponseDTO> > Save(
            string id,
            [FromBody] QuerySaveDTO querySave,
            [FromServices] IPanelConverterService panelConverter,
            [FromServices] IPanelValidator panelValidator,
            CancellationToken cancelToken
            )
        {
            try
            {
                // federated user submitting an initial save request
                if (!user.IsInstutional && QueryUrn.From(querySave.UniversalId) == null)
                {
                    return(BadRequest("Initial save requests must be made to home node."));
                }

                var ctx = await panelConverter.GetPanelsAsync(querySave, cancelToken);

                if (!ctx.PreflightPassed)
                {
                    return(BadRequest(new QuerySaveResponseDTO {
                        Preflight = new PreflightCheckDTO(ctx.PreflightCheck)
                    }));
                }
                var query = panelValidator.Validate(ctx);

                cancelToken.ThrowIfCancellationRequested();

                if (!user.IsInstutional)
                {
                    panelConverter.LocalizeDefinition(querySave, query);
                }


                var toSave = new QuerySave
                {
                    QueryId     = new Guid(id),
                    UniversalId = ctx.UniversalId,
                    Name        = querySave.Name,
                    Category    = querySave.Category,
                    Definition  = QueryDefinitionDTO.JSON(querySave),
                    Resources   = query.Panels.GetResources()
                };
                if (querySave.Ver.HasValue)
                {
                    toSave.Ver = querySave.Ver.Value;
                }

                var saved = await queryService.Save(toSave);

                if (saved == null)
                {
                    return(NotFound());
                }

                var response = new QuerySaveResponseDTO
                {
                    Preflight = new PreflightCheckDTO(ctx.PreflightCheck),
                    Query     = new QuerySaveResultDTO(saved)
                };

                return(Ok(response));
            }
            catch (InvalidOperationException ie)
            {
                log.LogError("Unrecoverable validation error in query. Error:{Error}", ie.Message);
                return(BadRequest());
            }
            catch (FormatException fe)
            {
                log.LogError("Malformed query identifier. Id:{Id} UniversalId:{UniversalId} Error:{Error}", id, querySave.UniversalId, fe.Message);
                return(BadRequest());
            }
            catch (OperationCanceledException)
            {
                log.LogInformation("Request cancelled.");
                return(NoContent());
            }
            catch (LeafDbException lde)
            {
                return(StatusCode(lde.StatusCode));
            }
            catch (Exception e)
            {
                log.LogError("Could not save query. QueryId:{QueryId} Query:{@Query} Error:{Error}", id, querySave, e.ToString());
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }