-
Notifications
You must be signed in to change notification settings - Fork 0
/
segments.go
85 lines (71 loc) · 2.51 KB
/
segments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package handlers
import (
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/jrzimmerman/bestrida-server-go/models"
log "github.com/sirupsen/logrus"
"github.com/strava/go.strava"
)
// GetSegmentByID returns segment by ID from the database
func GetSegmentByID(w http.ResponseWriter, r *http.Request) {
res := New(w)
id := chi.URLParam(r, "id")
numID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
log.WithField("id", numID).Errorf("unable to convert segment ID param")
res.Render(http.StatusInternalServerError, map[string]interface{}{
"error": "unable to convert segment ID param",
})
return
}
log.WithField("id", numID).Info("looking for segment by ID")
segment, err := models.GetSegmentByID(numID)
if err != nil {
log.WithField("id", numID).Errorf("unable to retrieve segment by ID from database")
res.Render(http.StatusInternalServerError, map[string]interface{}{
"error": "unable to retrieve segment by ID from database",
})
return
}
res.Render(http.StatusOK, segment)
}
// GetSegmentByIDWithUserID gets a segment by ID from database using the users api key
func GetSegmentByIDWithUserID(w http.ResponseWriter, r *http.Request) {
return
}
// GetSegmentByIDFromStrava returns the strava segment with the specified ID
func GetSegmentByIDFromStrava(w http.ResponseWriter, r *http.Request) {
res := New(w)
id := chi.URLParam(r, "id")
numID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
log.WithField("ID", numID).Error("unable to convert segment ID param")
res.Render(http.StatusInternalServerError, map[string]interface{}{
"error": "unable to convert segment ID param",
})
return
}
// use our access token to grab generic segment info
client := strava.NewClient(accessToken)
log.Infof("Fetching segment %v info from strava...", id)
segment, err := strava.NewSegmentsService(client).Get(numID).Do()
if err != nil {
res.Render(http.StatusInternalServerError, map[string]interface{}{"error": "Unable to retrieve segment info"})
return
}
log.Infof("rate limit percent: %v", strava.RateLimiting.FractionReached()*100)
log.Infof("segment %v retrieved from strava", segment.Id)
res.Render(http.StatusOK, segment)
// store segment in background after render
s, err := models.GetSegmentByID(segment.Id)
if err != nil {
models.SaveSegment(segment)
} else {
s.UpdateSegment(segment)
}
}
// GetSegmentByIDFromStravaWithUserID gets a segment by ID from strava using the users api key
func GetSegmentByIDFromStravaWithUserID(w http.ResponseWriter, r *http.Request) {
return
}