Commit 89b27b35 by Marcus Efraimsson

fix: Use Response as return type

parent 540d540e
...@@ -49,13 +49,12 @@ func (e *CreateAnnotationError) Error() string { ...@@ -49,13 +49,12 @@ func (e *CreateAnnotationError) Error() string {
return e.message return e.message
} }
func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) { func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response {
repo := annotations.GetRepository() repo := annotations.GetRepository()
if cmd.Text == "" { if cmd.Text == "" {
err := &CreateAnnotationError{"text field should not be empty"} err := &CreateAnnotationError{"text field should not be empty"}
c.JsonApiErr(500, "Failed to save annotation", err) return ApiError(500, "Failed to save annotation", err)
return
} }
item := annotations.Item{ item := annotations.Item{
...@@ -74,8 +73,7 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) { ...@@ -74,8 +73,7 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) {
} }
if err := repo.Save(&item); err != nil { if err := repo.Save(&item); err != nil {
c.JsonApiErr(500, "Failed to save annotation", err) return ApiError(500, "Failed to save annotation", err)
return
} }
startID := item.Id startID := item.Id
...@@ -89,27 +87,24 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) { ...@@ -89,27 +87,24 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) {
} }
if err := repo.Update(&item); err != nil { if err := repo.Update(&item); err != nil {
c.JsonApiErr(500, "Failed set regionId on annotation", err) return ApiError(500, "Failed set regionId on annotation", err)
return
} }
item.Id = 0 item.Id = 0
item.Epoch = cmd.TimeEnd / 1000 item.Epoch = cmd.TimeEnd / 1000
if err := repo.Save(&item); err != nil { if err := repo.Save(&item); err != nil {
c.JsonApiErr(500, "Failed save annotation for region end time", err) return ApiError(500, "Failed save annotation for region end time", err)
return
} }
c.JSON(200, util.DynMap{ return Json(200, util.DynMap{
"message": "Annotation added", "message": "Annotation added",
"id": startID, "id": startID,
"endId": item.Id, "endId": item.Id,
}) })
return
} }
c.JSON(200, util.DynMap{ return Json(200, util.DynMap{
"message": "Annotation added", "message": "Annotation added",
"id": startID, "id": startID,
}) })
...@@ -123,13 +118,12 @@ func formatGraphiteAnnotation(what string, data string) string { ...@@ -123,13 +118,12 @@ func formatGraphiteAnnotation(what string, data string) string {
return text return text
} }
func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotationsCmd) { func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotationsCmd) Response {
repo := annotations.GetRepository() repo := annotations.GetRepository()
if cmd.What == "" { if cmd.What == "" {
err := &CreateAnnotationError{"what field should not be empty"} err := &CreateAnnotationError{"what field should not be empty"}
c.JsonApiErr(500, "Failed to save Graphite annotation", err) return ApiError(500, "Failed to save Graphite annotation", err)
return
} }
if cmd.When == 0 { if cmd.When == 0 {
...@@ -152,14 +146,12 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati ...@@ -152,14 +146,12 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati
tagsArray = append(tagsArray, tagStr) tagsArray = append(tagsArray, tagStr)
} else { } else {
err := &CreateAnnotationError{"tag should be a string"} err := &CreateAnnotationError{"tag should be a string"}
c.JsonApiErr(500, "Failed to save Graphite annotation", err) return ApiError(500, "Failed to save Graphite annotation", err)
return
} }
} }
default: default:
err := &CreateAnnotationError{"unsupported tags format"} err := &CreateAnnotationError{"unsupported tags format"}
c.JsonApiErr(500, "Failed to save Graphite annotation", err) return ApiError(500, "Failed to save Graphite annotation", err)
return
} }
item := annotations.Item{ item := annotations.Item{
...@@ -171,11 +163,10 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati ...@@ -171,11 +163,10 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati
} }
if err := repo.Save(&item); err != nil { if err := repo.Save(&item); err != nil {
c.JsonApiErr(500, "Failed to save Graphite annotation", err) return ApiError(500, "Failed to save Graphite annotation", err)
return
} }
c.JSON(200, util.DynMap{ return Json(200, util.DynMap{
"message": "Graphite annotation added", "message": "Graphite annotation added",
"id": item.Id, "id": item.Id,
}) })
......
...@@ -288,11 +288,11 @@ func (hs *HttpServer) registerRoutes() { ...@@ -288,11 +288,11 @@ func (hs *HttpServer) registerRoutes() {
apiRoute.Post("/annotations/mass-delete", reqOrgAdmin, bind(dtos.DeleteAnnotationsCmd{}), wrap(DeleteAnnotations)) apiRoute.Post("/annotations/mass-delete", reqOrgAdmin, bind(dtos.DeleteAnnotationsCmd{}), wrap(DeleteAnnotations))
apiRoute.Group("/annotations", func(annotationsRoute RouteRegister) { apiRoute.Group("/annotations", func(annotationsRoute RouteRegister) {
annotationsRoute.Post("/", bind(dtos.PostAnnotationsCmd{}), PostAnnotation) annotationsRoute.Post("/", bind(dtos.PostAnnotationsCmd{}), wrap(PostAnnotation))
annotationsRoute.Delete("/:annotationId", wrap(DeleteAnnotationById)) annotationsRoute.Delete("/:annotationId", wrap(DeleteAnnotationById))
annotationsRoute.Put("/:annotationId", bind(dtos.UpdateAnnotationsCmd{}), wrap(UpdateAnnotation)) annotationsRoute.Put("/:annotationId", bind(dtos.UpdateAnnotationsCmd{}), wrap(UpdateAnnotation))
annotationsRoute.Delete("/region/:regionId", wrap(DeleteAnnotationRegion)) annotationsRoute.Delete("/region/:regionId", wrap(DeleteAnnotationRegion))
annotationsRoute.Post("/graphite", bind(dtos.PostGraphiteAnnotationsCmd{}), PostGraphiteAnnotation) annotationsRoute.Post("/graphite", bind(dtos.PostGraphiteAnnotationsCmd{}), wrap(PostGraphiteAnnotation))
}, reqEditorRole) }, reqEditorRole)
// error test // error test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment