04d8eaf52c
Client Entity * repolevel * servicefunctions * openapiclientget * openapiupdate * client * vendor
136 lines
3.7 KiB
Go
136 lines
3.7 KiB
Go
package queryservice
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"queryorchestration/internal/query"
|
|
queryprocessor "queryorchestration/internal/query/processor"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func (s *Controllers) ListQueries(ctx echo.Context) error {
|
|
filters := query.ListFilters{}
|
|
|
|
queries, err := s.svc.Query.List(ctx.Request().Context(), filters)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to list query: %s", err))
|
|
}
|
|
|
|
outQueries, err := parseQueries(queries)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to list query: %s", err))
|
|
}
|
|
|
|
return ctx.JSON(http.StatusOK, ListQueries{
|
|
Queries: outQueries,
|
|
})
|
|
}
|
|
|
|
func (s *Controllers) GetQuery(ctx echo.Context, id string) error {
|
|
uid, err := uuid.Parse(id)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
|
|
}
|
|
|
|
query, err := s.svc.Query.Get(ctx.Request().Context(), uid)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to get query: %s", err))
|
|
}
|
|
|
|
qt, err := parseQuery(query)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, 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)
|
|
}
|
|
|
|
requiredQueryIDs, err := parseStringToUUIDArray(req.RequiredQueries)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Required IDs")
|
|
}
|
|
|
|
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(), &queryprocessor.Create{
|
|
Type: qt,
|
|
RequiredQueryIDs: requiredQueryIDs,
|
|
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.String(),
|
|
})
|
|
}
|
|
|
|
func (s *Controllers) UpdateQuery(ctx echo.Context, id string) error {
|
|
req := QueryUpdate{}
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err)
|
|
}
|
|
|
|
uid, err := uuid.Parse(id)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
|
|
}
|
|
|
|
requiredQueryIDs, err := parseStringToUUIDArray(req.RequiredQueries)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Required IDs")
|
|
}
|
|
|
|
err = s.svc.Query.Update(ctx.Request().Context(), &queryprocessor.Update{
|
|
ActiveVersion: req.ActiveVersion,
|
|
Config: req.Config,
|
|
RequiredQueryIDs: requiredQueryIDs,
|
|
ID: uid,
|
|
})
|
|
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 string) error {
|
|
queryId, err := uuid.Parse(id)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Query ID")
|
|
}
|
|
req := QueryTestRequest{}
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err)
|
|
}
|
|
docId, err := uuid.Parse(req.DocumentId)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Doc ID")
|
|
}
|
|
|
|
value, err := s.svc.Query.Test(ctx.Request().Context(), query.Test{
|
|
QueryID: queryId,
|
|
QueryVersion: req.QueryVersion,
|
|
DocumentID: docId,
|
|
})
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to test query: %s", err))
|
|
}
|
|
|
|
return ctx.JSON(http.StatusOK, QueryTestResponse{
|
|
Value: value,
|
|
})
|
|
}
|