2022-01-28 10:55:09 -06:00
|
|
|
package queryhistory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-02-04 09:14:36 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2022-01-28 10:55:09 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *QueryHistoryService) registerAPIEndpoints() {
|
|
|
|
s.RouteRegister.Group("/api/query-history", func(entities routing.RouteRegister) {
|
|
|
|
entities.Post("/", middleware.ReqSignedIn, routing.Wrap(s.createHandler))
|
2022-02-04 09:14:36 -06:00
|
|
|
entities.Delete("/:uid", middleware.ReqSignedIn, routing.Wrap(s.deleteHandler))
|
2022-01-28 10:55:09 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *QueryHistoryService) createHandler(c *models.ReqContext) response.Response {
|
|
|
|
cmd := CreateQueryInQueryHistoryCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:14:36 -06:00
|
|
|
query, err := s.CreateQueryInQueryHistory(c.Req.Context(), c.SignedInUser, cmd)
|
2022-01-28 10:55:09 -06:00
|
|
|
if err != nil {
|
2022-02-04 09:14:36 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to create query history", err)
|
2022-01-28 10:55:09 -06:00
|
|
|
}
|
|
|
|
|
2022-02-04 09:14:36 -06:00
|
|
|
return response.JSON(http.StatusOK, QueryHistoryResponse{Result: query})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *QueryHistoryService) deleteHandler(c *models.ReqContext) response.Response {
|
|
|
|
queryUID := web.Params(c.Req)[":uid"]
|
|
|
|
if len(queryUID) == 0 {
|
|
|
|
return response.Error(http.StatusNotFound, "Query in query history not found", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !util.IsValidShortUID(queryUID) {
|
|
|
|
return response.Error(http.StatusNotFound, "Query in query history not found", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := s.DeleteQueryFromQueryHistory(c.Req.Context(), c.SignedInUser, queryUID)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to delete query from query history", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(http.StatusOK, DeleteQueryFromQueryHistoryResponse{
|
|
|
|
Message: "Query deleted",
|
|
|
|
ID: id,
|
|
|
|
})
|
2022-01-28 10:55:09 -06:00
|
|
|
}
|