1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

Major refactorings.

Introduce summaries.
This commit is contained in:
Ferdinand Mütsch
2019-05-19 19:49:27 +02:00
parent 62e94f6635
commit be906805e7
14 changed files with 194 additions and 282 deletions

View File

@@ -1,49 +0,0 @@
package routes
import (
"net/http"
"time"
"github.com/n1try/wakapi/models"
"github.com/n1try/wakapi/services"
"github.com/n1try/wakapi/utils"
)
type AggregationHandler struct {
AggregationSrvc *services.AggregationService
}
func (h *AggregationHandler) Get(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(415)
return
}
user := r.Context().Value(models.UserKey).(*models.User)
params := r.URL.Query()
from, err := utils.ParseDate(params.Get("from"))
if err != nil {
w.WriteHeader(400)
w.Write([]byte("Missing 'from' parameter"))
return
}
to, err := utils.ParseDate(params.Get("to"))
if err != nil {
to = time.Now()
}
aggregations, err := h.AggregationSrvc.FindOrAggregate(from, to, user)
if err != nil {
w.WriteHeader(500)
return
}
for i := 0; i < len(aggregations); i++ {
if err := h.AggregationSrvc.SaveAggregation(aggregations[i]); err != nil {
w.WriteHeader(500)
return
}
}
w.WriteHeader(200)
}

View File

@@ -16,8 +16,8 @@ type HeartbeatHandler struct {
}
func (h *HeartbeatHandler) Post(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(415)
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
@@ -27,7 +27,7 @@ func (h *HeartbeatHandler) Post(w http.ResponseWriter, r *http.Request) {
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&heartbeats); err != nil {
w.WriteHeader(400)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
@@ -39,17 +39,17 @@ func (h *HeartbeatHandler) Post(w http.ResponseWriter, r *http.Request) {
h.UserID = user.ID
if !h.Valid() {
w.WriteHeader(400)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid heartbeat object."))
return
}
}
if err := h.HeartbeatSrvc.InsertBatch(heartbeats); err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
os.Stderr.WriteString(err.Error())
return
}
w.WriteHeader(200)
w.WriteHeader(http.StatusOK)
}

41
routes/summary.go Normal file
View File

@@ -0,0 +1,41 @@
package routes
import (
"net/http"
"time"
"github.com/n1try/wakapi/models"
"github.com/n1try/wakapi/services"
"github.com/n1try/wakapi/utils"
)
type SummaryHandler struct {
SummarySrvc *services.SummaryService
}
func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
user := r.Context().Value(models.UserKey).(*models.User)
params := r.URL.Query()
from, err := utils.ParseDate(params.Get("from"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Missing 'from' parameter"))
return
}
now := time.Now()
to := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) // Start of current day
summary, err := h.SummarySrvc.GetSummary(from, to, user)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
utils.RespondJSON(w, http.StatusOK, summary)
}