Cleanup api responses

This commit is contained in:
simon987
2019-02-16 19:44:03 -05:00
parent 8784b536d3
commit 71e05ecdd6
21 changed files with 809 additions and 888 deletions

View File

@@ -1,24 +1,17 @@
package api
import (
"github.com/simon987/task_tracker/storage"
"math"
"strconv"
)
type MonitoringSnapshotResponse struct {
Ok bool `json:"ok"`
Message string `json:"message,omitempty"`
Snapshots *[]storage.ProjectMonitoringSnapshot `json:"snapshots,omitempty"`
}
func (api *WebAPI) GetSnapshotsBetween(r *Request) {
func (api *WebAPI) GetSnapshotsWithinRange(r *Request) {
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
from := r.Ctx.Request.URI().QueryArgs().GetUintOrZero("from")
to := r.Ctx.Request.URI().QueryArgs().GetUintOrZero("to")
if err != nil || id <= 0 || from <= 0 || to <= 0 || from >= math.MaxInt32 || to >= math.MaxInt32 {
r.Json(MonitoringSnapshotResponse{
r.Json(JsonResponse{
Ok: false,
Message: "Invalid request",
}, 400)
@@ -26,9 +19,11 @@ func (api *WebAPI) GetSnapshotsBetween(r *Request) {
}
snapshots := api.Database.GetMonitoringSnapshotsBetween(id, from, to)
r.OkJson(MonitoringSnapshotResponse{
Ok: true,
Snapshots: snapshots,
r.OkJson(JsonResponse{
Ok: true,
Content: GetSnapshotsResponse{
Snapshots: snapshots,
},
})
}
@@ -37,7 +32,7 @@ func (api *WebAPI) GetNSnapshots(r *Request) {
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
count := r.Ctx.Request.URI().QueryArgs().GetUintOrZero("count")
if err != nil || id <= 0 || count <= 0 || count >= 1000 {
r.Json(MonitoringSnapshotResponse{
r.Json(JsonResponse{
Ok: false,
Message: "Invalid request",
}, 400)
@@ -45,8 +40,10 @@ func (api *WebAPI) GetNSnapshots(r *Request) {
}
snapshots := api.Database.GetNMonitoringSnapshots(id, count)
r.OkJson(MonitoringSnapshotResponse{
Ok: true,
Snapshots: snapshots,
r.OkJson(JsonResponse{
Ok: true,
Content: GetSnapshotsResponse{
Snapshots: snapshots,
},
})
}