mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Add intervals for summary requests.
This commit is contained in:
@ -9,6 +9,13 @@ import (
|
||||
"github.com/n1try/wakapi/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
IntervalLastDay string = "day"
|
||||
IntervalLastWeek string = "week"
|
||||
IntervalLastMonth string = "month"
|
||||
IntervalLastYear string = "year"
|
||||
)
|
||||
|
||||
type SummaryHandler struct {
|
||||
SummarySrvc *services.SummaryService
|
||||
}
|
||||
@ -23,15 +30,24 @@ func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
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
|
||||
interval := params.Get("interval")
|
||||
switch interval {
|
||||
case IntervalLastDay:
|
||||
from = utils.StartOfDay().Add(-24 * time.Hour)
|
||||
case IntervalLastWeek:
|
||||
from = utils.StartOfWeek()
|
||||
case IntervalLastMonth:
|
||||
from = utils.StartOfMonth()
|
||||
case IntervalLastYear:
|
||||
from = utils.StartOfYear()
|
||||
default:
|
||||
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)
|
||||
summary, err := h.SummarySrvc.GetSummary(from, utils.StartOfDay(), user)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
Reference in New Issue
Block a user