Chore: Moves common and response into separate packages (#30298)

* Chore: moves common and response into separate packages

* Chore: moves common and response into separate packages

* Update pkg/api/utils/common.go

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Chore: changes after PR comments

* Chore: move wrap to routing package

* Chore: move functions in common to response package

* Chore: move functions in common to response package

* Chore: formats imports

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Hugo Häggmark
2021-01-15 14:43:20 +01:00
committed by GitHub
co-authored by Arve Knudsen
parent a94c256516
commit 3d41267fc4
51 changed files with 1321 additions and 1248 deletions
+29 -28
View File
@@ -4,13 +4,14 @@ import (
"strings"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/util"
)
func GetAnnotations(c *models.ReqContext) Response {
func GetAnnotations(c *models.ReqContext) response.Response {
query := &annotations.ItemQuery{
From: c.QueryInt64("from"),
To: c.QueryInt64("to"),
@@ -29,7 +30,7 @@ func GetAnnotations(c *models.ReqContext) Response {
items, err := repo.Find(query)
if err != nil {
return Error(500, "Failed to get annotations", err)
return response.Error(500, "Failed to get annotations", err)
}
for _, item := range items {
@@ -38,7 +39,7 @@ func GetAnnotations(c *models.ReqContext) Response {
}
}
return JSON(200, items)
return response.JSON(200, items)
}
type CreateAnnotationError struct {
@@ -49,7 +50,7 @@ func (e *CreateAnnotationError) Error() string {
return e.message
}
func PostAnnotation(c *models.ReqContext, cmd dtos.PostAnnotationsCmd) Response {
func PostAnnotation(c *models.ReqContext, cmd dtos.PostAnnotationsCmd) response.Response {
if canSave, err := canSaveByDashboardID(c, cmd.DashboardId); err != nil || !canSave {
return dashboardGuardianResponse(err)
}
@@ -58,7 +59,7 @@ func PostAnnotation(c *models.ReqContext, cmd dtos.PostAnnotationsCmd) Response
if cmd.Text == "" {
err := &CreateAnnotationError{"text field should not be empty"}
return Error(500, "Failed to save annotation", err)
return response.Error(500, "Failed to save annotation", err)
}
item := annotations.Item{
@@ -74,12 +75,12 @@ func PostAnnotation(c *models.ReqContext, cmd dtos.PostAnnotationsCmd) Response
}
if err := repo.Save(&item); err != nil {
return Error(500, "Failed to save annotation", err)
return response.Error(500, "Failed to save annotation", err)
}
startID := item.Id
return JSON(200, util.DynMap{
return response.JSON(200, util.DynMap{
"message": "Annotation added",
"id": startID,
})
@@ -93,12 +94,12 @@ func formatGraphiteAnnotation(what string, data string) string {
return text
}
func PostGraphiteAnnotation(c *models.ReqContext, cmd dtos.PostGraphiteAnnotationsCmd) Response {
func PostGraphiteAnnotation(c *models.ReqContext, cmd dtos.PostGraphiteAnnotationsCmd) response.Response {
repo := annotations.GetRepository()
if cmd.What == "" {
err := &CreateAnnotationError{"what field should not be empty"}
return Error(500, "Failed to save Graphite annotation", err)
return response.Error(500, "Failed to save Graphite annotation", err)
}
text := formatGraphiteAnnotation(cmd.What, cmd.Data)
@@ -118,12 +119,12 @@ func PostGraphiteAnnotation(c *models.ReqContext, cmd dtos.PostGraphiteAnnotatio
tagsArray = append(tagsArray, tagStr)
} else {
err := &CreateAnnotationError{"tag should be a string"}
return Error(500, "Failed to save Graphite annotation", err)
return response.Error(500, "Failed to save Graphite annotation", err)
}
}
default:
err := &CreateAnnotationError{"unsupported tags format"}
return Error(500, "Failed to save Graphite annotation", err)
return response.Error(500, "Failed to save Graphite annotation", err)
}
item := annotations.Item{
@@ -135,16 +136,16 @@ func PostGraphiteAnnotation(c *models.ReqContext, cmd dtos.PostGraphiteAnnotatio
}
if err := repo.Save(&item); err != nil {
return Error(500, "Failed to save Graphite annotation", err)
return response.Error(500, "Failed to save Graphite annotation", err)
}
return JSON(200, util.DynMap{
return response.JSON(200, util.DynMap{
"message": "Graphite annotation added",
"id": item.Id,
})
}
func UpdateAnnotation(c *models.ReqContext, cmd dtos.UpdateAnnotationsCmd) Response {
func UpdateAnnotation(c *models.ReqContext, cmd dtos.UpdateAnnotationsCmd) response.Response {
annotationID := c.ParamsInt64(":annotationId")
repo := annotations.GetRepository()
@@ -164,13 +165,13 @@ func UpdateAnnotation(c *models.ReqContext, cmd dtos.UpdateAnnotationsCmd) Respo
}
if err := repo.Update(&item); err != nil {
return Error(500, "Failed to update annotation", err)
return response.Error(500, "Failed to update annotation", err)
}
return Success("Annotation updated")
return response.Success("Annotation updated")
}
func PatchAnnotation(c *models.ReqContext, cmd dtos.PatchAnnotationsCmd) Response {
func PatchAnnotation(c *models.ReqContext, cmd dtos.PatchAnnotationsCmd) response.Response {
annotationID := c.ParamsInt64(":annotationId")
repo := annotations.GetRepository()
@@ -182,7 +183,7 @@ func PatchAnnotation(c *models.ReqContext, cmd dtos.PatchAnnotationsCmd) Respons
items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationID, OrgId: c.OrgId})
if err != nil || len(items) == 0 {
return Error(404, "Could not find annotation to update", err)
return response.Error(404, "Could not find annotation to update", err)
}
existing := annotations.Item{
@@ -212,13 +213,13 @@ func PatchAnnotation(c *models.ReqContext, cmd dtos.PatchAnnotationsCmd) Respons
}
if err := repo.Update(&existing); err != nil {
return Error(500, "Failed to update annotation", err)
return response.Error(500, "Failed to update annotation", err)
}
return Success("Annotation patched")
return response.Success("Annotation patched")
}
func DeleteAnnotations(c *models.ReqContext, cmd dtos.DeleteAnnotationsCmd) Response {
func DeleteAnnotations(c *models.ReqContext, cmd dtos.DeleteAnnotationsCmd) response.Response {
repo := annotations.GetRepository()
err := repo.Delete(&annotations.DeleteParams{
@@ -229,13 +230,13 @@ func DeleteAnnotations(c *models.ReqContext, cmd dtos.DeleteAnnotationsCmd) Resp
})
if err != nil {
return Error(500, "Failed to delete annotations", err)
return response.Error(500, "Failed to delete annotations", err)
}
return Success("Annotations deleted")
return response.Success("Annotations deleted")
}
func DeleteAnnotationByID(c *models.ReqContext) Response {
func DeleteAnnotationByID(c *models.ReqContext) response.Response {
repo := annotations.GetRepository()
annotationID := c.ParamsInt64(":annotationId")
@@ -248,10 +249,10 @@ func DeleteAnnotationByID(c *models.ReqContext) Response {
Id: annotationID,
})
if err != nil {
return Error(500, "Failed to delete annotation", err)
return response.Error(500, "Failed to delete annotation", err)
}
return Success("Annotation deleted")
return response.Success("Annotation deleted")
}
func canSaveByDashboardID(c *models.ReqContext, dashboardID int64) (bool, error) {
@@ -269,10 +270,10 @@ func canSaveByDashboardID(c *models.ReqContext, dashboardID int64) (bool, error)
return true, nil
}
func canSave(c *models.ReqContext, repo annotations.Repository, annotationID int64) Response {
func canSave(c *models.ReqContext, repo annotations.Repository, annotationID int64) response.Response {
items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationID, OrgId: c.OrgId})
if err != nil || len(items) == 0 {
return Error(500, "Could not find annotation to update", err)
return response.Error(500, "Could not find annotation to update", err)
}
dashboardID := items[0].DashboardId