parent
b689c91d1b
commit
a4e5e06b67
@ -0,0 +1,106 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.vdhsn.com/adam/bodytrack/internal/services"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
)
|
||||
|
||||
type Stats struct {
|
||||
Conn services.DB
|
||||
}
|
||||
|
||||
func (s Stats) Subscribe(c chi.Router) error {
|
||||
c.Group(func(r chi.Router) {
|
||||
r.Use(services.NewJWTVerifier())
|
||||
r.Use(jwtauth.Authenticator)
|
||||
|
||||
r.Put("/stats/weight", s.logWeight)
|
||||
r.Get("/stats/weight", s.getWeightLog)
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Stats) getWeightLog(res http.ResponseWriter, req *http.Request) {
|
||||
fail := APIResp{Payload: "Unauthorized", Status: http.StatusUnauthorized}
|
||||
userId, err := services.GetUserIDFromClaims(req)
|
||||
if err != nil {
|
||||
fail.Payload = err.Error()
|
||||
fail.Write(res)
|
||||
return
|
||||
}
|
||||
|
||||
startStr := req.URL.Query().Get("start")
|
||||
endStr := req.URL.Query().Get("end")
|
||||
|
||||
start := time.Now().UTC().Add(-time.Hour * 24 * 30)
|
||||
|
||||
if startStr != "" {
|
||||
start, _ = time.Parse(time.RFC3339, startStr)
|
||||
}
|
||||
|
||||
end := time.Now().UTC()
|
||||
if endStr != "" {
|
||||
end, _ = time.Parse(time.RFC3339, endStr)
|
||||
}
|
||||
|
||||
wlog, err := s.Conn.GetWeightLog(userId, start, end)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
fail.Write(res)
|
||||
return
|
||||
}
|
||||
|
||||
APIResp{
|
||||
Success: true,
|
||||
Payload: struct {
|
||||
Log []services.WeightLog `json:"log"`
|
||||
Count int `json:"count"`
|
||||
Start time.Time `json:"start"`
|
||||
End time.Time `json:"end"`
|
||||
}{
|
||||
Log: wlog,
|
||||
Count: len(wlog),
|
||||
Start: start,
|
||||
End: end,
|
||||
},
|
||||
}.Write(res)
|
||||
|
||||
}
|
||||
|
||||
func (s Stats) logWeight(res http.ResponseWriter, req *http.Request) {
|
||||
fail := APIResp{Payload: "Unauthorized", Status: http.StatusUnauthorized}
|
||||
userId, err := services.GetUserIDFromClaims(req)
|
||||
if err != nil {
|
||||
fail.Payload = err.Error()
|
||||
fail.Write(res)
|
||||
return
|
||||
}
|
||||
|
||||
type logWeightInput struct {
|
||||
services.WeightLog
|
||||
}
|
||||
|
||||
var in logWeightInput
|
||||
|
||||
if err := ParseCall(&in, res, req); err != nil {
|
||||
fail.Payload = "Could not read arguments"
|
||||
fail.Status = http.StatusUnprocessableEntity
|
||||
fail.Write(res)
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.Conn.LogWeight(userId, in.WeightLog); err != nil {
|
||||
log.Println(err)
|
||||
fail.Write(res)
|
||||
return
|
||||
}
|
||||
|
||||
APIResp{Success: true}.Write(res)
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type WeightLog struct {
|
||||
ID uint64 `json:"id"`
|
||||
UserID uint64 `json:"userId"`
|
||||
Value float64 `json:"value"`
|
||||
RecordedTS time.Time `json:"recordedTs"`
|
||||
}
|
Loading…
Reference in new issue