Merged in feature/docresult (pull request #105)
Document Result Endpoint * testing * cleantesting * progress * query * lint * readme * dockerclient * api * passtest * tests * test
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package queryapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"queryorchestration/internal/query/result"
|
||||
resultprocessor "queryorchestration/internal/query/result/processor"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func (s *Controllers) ListQueries(ctx echo.Context) error {
|
||||
queries, err := s.svc.Query.List(ctx.Request().Context())
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to list query: %s", err))
|
||||
}
|
||||
|
||||
outQueries, err := parseQueries(queries)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to list query: %s", err))
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, ListQueries{
|
||||
Queries: outQueries,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Controllers) GetQuery(ctx echo.Context, id QueryID) error {
|
||||
query, err := s.svc.Query.Get(ctx.Request().Context(), id)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to get query: %s", err))
|
||||
}
|
||||
|
||||
qt, err := parseQuery(query)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to get query: %s", err))
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, qt)
|
||||
}
|
||||
|
||||
func (s *Controllers) CreateQuery(ctx echo.Context) error {
|
||||
req := QueryCreate{}
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
qt, err := parseSpecQueryType(req.Type)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Query Type")
|
||||
}
|
||||
|
||||
id, err := s.svc.Query.Create(ctx.Request().Context(), &resultprocessor.Create{
|
||||
Type: qt,
|
||||
RequiredQueryIDs: req.RequiredQueries,
|
||||
Config: req.Config,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to create query: %s", err))
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusCreated, IdMessage{
|
||||
Id: id,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Controllers) UpdateQuery(ctx echo.Context, id QueryID) error {
|
||||
req := QueryUpdate{}
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
err := s.svc.QueryUpdate.Update(ctx.Request().Context(), &resultprocessor.Update{
|
||||
ActiveVersion: req.ActiveVersion,
|
||||
Config: req.Config,
|
||||
RequiredQueryIDs: req.RequiredQueries,
|
||||
ID: id,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to update query: %s", err))
|
||||
}
|
||||
|
||||
return ctx.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (s *Controllers) TestQuery(ctx echo.Context, id QueryID) error {
|
||||
req := QueryTestRequest{}
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
value, err := s.svc.QueryTest.Test(ctx.Request().Context(), result.Process{
|
||||
QueryID: id,
|
||||
QueryVersion: req.QueryVersion,
|
||||
DocumentID: req.DocumentId,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to test query: %s", err))
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, QueryTestResponse{
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user