Skip to content

Commit

Permalink
feat: Added activity laps endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bahattincinic committed May 22, 2024
1 parent e775adb commit baab35e
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 20 deletions.
46 changes: 28 additions & 18 deletions api/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"

"github.com/bahattincinic/fitwave/models"
"github.com/bahattincinic/fitwave/strava"
"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -45,19 +46,12 @@ func (a *API) listActivities(c echo.Context) error {
// @Success 200 {object} models.Activity
// @Router /activities/{id} [get]
func (a *API) getActivity(c echo.Context) error {
act, err := a.db.GetActivity(c.Param("id"))
if err != nil {
return err
}

if act == nil {
return echo.NewHTTPError(http.StatusNotFound, "activity Not Found")
}
act := c.Get(activityContextKey).(*models.Activity)

return c.JSON(http.StatusOK, act)
}

// getActivity godoc
// exportActivityGPS godoc
//
// @Summary Export Activity GPX
// @Tags activity
Expand All @@ -68,15 +62,7 @@ func (a *API) getActivity(c echo.Context) error {
// @Router /activities/{id}/gpx [get]
func (a *API) exportActivityGPS(c echo.Context) error {
user := c.Get(userContextKey).(*strava.User)

act, err := a.db.GetActivity(c.Param("id"))
if err != nil {
return err
}

if act == nil {
return echo.NewHTTPError(http.StatusNotFound, "activity Not Found")
}
act := c.Get(activityContextKey).(*models.Activity)

gpx, err := a.st.ExportGPX(user, act.Id)
if err != nil {
Expand All @@ -93,3 +79,27 @@ func (a *API) exportActivityGPS(c echo.Context) error {
"application/gpx+xml")
return c.Blob(http.StatusOK, "application/gpx+xml", []byte(xml.Header+gpx))
}

// getActivityLaps godoc
//
// @Summary Get Activity Laps
// @Tags activity
// @Accept json
// @Param id path string true "Activity ID"
// @Param Authorization header string true "Strava Access Token"
// @Success 200 {object} PaginatedResponse{Results=[]strava.LapEffortSummary, count=int}
// @Router /activities/{id}/laps [get]
func (a *API) getActivityLaps(c echo.Context) error {
user := c.Get(userContextKey).(*strava.User)
act := c.Get(activityContextKey).(*models.Activity)

laps, err := a.st.GetActivityLaps(user, act.Id)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

return c.JSON(http.StatusOK, PaginatedResponse{
Results: laps,
Count: int64(len(laps)),
})
}
125 changes: 125 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,59 @@ const docTemplate = `{
}
}
},
"/activities/{id}/laps": {
"get": {
"consumes": [
"application/json"
],
"tags": [
"activity"
],
"summary": "Get Activity Laps",
"parameters": [
{
"type": "string",
"description": "Activity ID",
"name": "id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Strava Access Token",
"name": "Authorization",
"in": "header",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/api.PaginatedResponse"
},
{
"type": "object",
"properties": {
" count": {
"type": "integer"
},
"Results": {
"type": "array",
"items": {
"$ref": "#/definitions/strava.LapEffortSummary"
}
}
}
}
]
}
}
}
}
},
"/athletes": {
"get": {
"consumes": [
Expand Down Expand Up @@ -1485,6 +1538,78 @@ const docTemplate = `{
}
}
},
"strava.LapEffortSummary": {
"type": "object",
"properties": {
"activity": {
"type": "object",
"properties": {
"id": {
"type": "integer"
}
}
},
"athlete": {
"type": "object",
"properties": {
"id": {
"type": "integer"
}
}
},
"average_cadence": {
"type": "number"
},
"average_heartrate": {
"type": "number"
},
"average_speed": {
"type": "number"
},
"average_watts": {
"type": "number"
},
"distance": {
"type": "number"
},
"elapsed_time": {
"type": "integer"
},
"end_index": {
"type": "integer"
},
"id": {
"type": "integer"
},
"lap_index": {
"type": "integer"
},
"max_heartrate": {
"type": "number"
},
"max_speed": {
"type": "number"
},
"moving_time": {
"type": "integer"
},
"name": {
"type": "string"
},
"start_date": {
"type": "string"
},
"start_date_local": {
"type": "string"
},
"start_index": {
"type": "integer"
},
"total_elevation_gain": {
"type": "number"
}
}
},
"strava.User": {
"type": "object",
"properties": {
Expand Down
125 changes: 125 additions & 0 deletions api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,59 @@
}
}
},
"/activities/{id}/laps": {
"get": {
"consumes": [
"application/json"
],
"tags": [
"activity"
],
"summary": "Get Activity Laps",
"parameters": [
{
"type": "string",
"description": "Activity ID",
"name": "id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Strava Access Token",
"name": "Authorization",
"in": "header",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/api.PaginatedResponse"
},
{
"type": "object",
"properties": {
" count": {
"type": "integer"
},
"Results": {
"type": "array",
"items": {
"$ref": "#/definitions/strava.LapEffortSummary"
}
}
}
}
]
}
}
}
}
},
"/athletes": {
"get": {
"consumes": [
Expand Down Expand Up @@ -1474,6 +1527,78 @@
}
}
},
"strava.LapEffortSummary": {
"type": "object",
"properties": {
"activity": {
"type": "object",
"properties": {
"id": {
"type": "integer"
}
}
},
"athlete": {
"type": "object",
"properties": {
"id": {
"type": "integer"
}
}
},
"average_cadence": {
"type": "number"
},
"average_heartrate": {
"type": "number"
},
"average_speed": {
"type": "number"
},
"average_watts": {
"type": "number"
},
"distance": {
"type": "number"
},
"elapsed_time": {
"type": "integer"
},
"end_index": {
"type": "integer"
},
"id": {
"type": "integer"
},
"lap_index": {
"type": "integer"
},
"max_heartrate": {
"type": "number"
},
"max_speed": {
"type": "number"
},
"moving_time": {
"type": "integer"
},
"name": {
"type": "string"
},
"start_date": {
"type": "string"
},
"start_date_local": {
"type": "string"
},
"start_index": {
"type": "integer"
},
"total_elevation_gain": {
"type": "number"
}
}
},
"strava.User": {
"type": "object",
"properties": {
Expand Down
Loading

0 comments on commit baab35e

Please sign in to comment.