Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

45 righe
1.1 KiB

  1. package controller
  2. import (
  3. "context"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. "github.com/AFASystems/presence/internal/pkg/api/response"
  8. "github.com/AFASystems/presence/internal/pkg/model"
  9. "github.com/gorilla/mux"
  10. "gorm.io/gorm"
  11. )
  12. func TracksListController(db *gorm.DB, context context.Context) http.HandlerFunc {
  13. return func(w http.ResponseWriter, r *http.Request) {
  14. id := mux.Vars(r)["id"]
  15. var tracks []model.Tracks
  16. query := r.URL.Query()
  17. lStr := query.Get("limit")
  18. parseTime := func(key string, defaultTime time.Time) time.Time {
  19. t, err := time.Parse(time.RFC3339, query.Get(key))
  20. if err != nil {
  21. return defaultTime
  22. }
  23. return t
  24. }
  25. limit, _ := strconv.Atoi(lStr)
  26. if limit == 0 {
  27. limit = 10
  28. }
  29. from := parseTime("from", time.Now().AddDate(0, 0, -1))
  30. to := parseTime("to", time.Now())
  31. if err := db.WithContext(context).Where("uuid = ? AND timestamp BETWEEN ? AND ?", id, from, to).Order("timestamp DESC").Limit(limit).Find(&tracks).Error; err != nil {
  32. response.InternalError(w, "failed to list tracks", err)
  33. return
  34. }
  35. response.JSON(w, http.StatusOK, tracks)
  36. }
  37. }