/
workouts_handlers.go
131 lines (97 loc) · 3.53 KB
/
workouts_handlers.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package app
import (
"bytes"
"errors"
"net/http"
"path"
"strconv"
"github.com/jovandeginste/workout-tracker/pkg/database"
"github.com/labstack/echo/v4"
)
func (a *App) workoutsHandler(c echo.Context) error {
data := a.defaultData(c)
if err := a.addWorkouts(a.getCurrentUser(c), data); err != nil {
return a.redirectWithError(c, a.echo.Reverse("dashboard"), err)
}
return c.Render(http.StatusOK, "workouts_list.html", data)
}
func (a *App) workoutsShowHandler(c echo.Context) error {
data := a.defaultData(c)
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return a.redirectWithError(c, "/workouts", err)
}
w, err := database.GetWorkoutDetails(a.db, id)
if err != nil {
return a.redirectWithError(c, "/workouts", err)
}
data["workout"] = w
return c.Render(http.StatusOK, "workouts_show.html", data)
}
func (a *App) workoutsAddHandler(c echo.Context) error {
data := a.defaultData(c)
return c.Render(http.StatusOK, "workouts_add.html", data)
}
func (a *App) workoutsDeleteHandler(c echo.Context) error {
workout, err := a.getWorkout(c)
if err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-show", c.Param("id")), err)
}
if err := workout.Delete(a.db); err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-show", c.Param("id")), err)
}
a.setNotice(c, "The workout '%s' has been deleted.", workout.Name)
return c.Redirect(http.StatusFound, a.echo.Reverse("workouts"))
}
func (a *App) workoutsRefreshHandler(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-show", c.Param("id")), err)
}
workout, err := a.getCurrentUser(c).GetWorkout(a.db.Preload("GPX"), id)
if err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-show", c.Param("id")), err)
}
if err := workout.UpdateData(a.db); err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-show", c.Param("id")), err)
}
a.setNotice(c, "The workout '%s' has been refreshed.", workout.Name)
return c.Redirect(http.StatusFound, a.echo.Reverse("workout-show", c.Param("id")))
}
func (a *App) workoutsUpdateHandler(c echo.Context) error {
workout, err := a.getWorkout(c)
if err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-show", c.Param("id")), err)
}
workout.Name = c.FormValue("name")
workout.Notes = c.FormValue("notes")
workout.Type = database.WorkoutType(c.FormValue("type"))
if err := workout.Save(a.db); err != nil {
return a.redirectWithError(c, a.echo.Reverse("workout-show", c.Param("id")), err)
}
a.setNotice(c, "The workout '%s' has been updated.", workout.Name)
return c.Redirect(http.StatusFound, a.echo.Reverse("workout-show", c.Param("id")))
}
func (a *App) workoutsDownloadHandler(c echo.Context) error {
workout, err := a.getWorkout(c)
if err != nil {
return a.redirectWithError(c, "/workouts", err)
}
if workout.GPX == nil ||
workout.GPX.Filename == "" ||
workout.GPX.Content == nil {
return a.redirectWithError(c, "/workouts", errors.New("workout has no content"))
}
basename := path.Base(workout.GPX.Filename)
c.Response().Header().Set(echo.HeaderContentDisposition, "attachment; filename=\""+basename+"\"")
return c.Stream(http.StatusOK, "application/binary", bytes.NewReader(workout.GPX.Content))
}
func (a *App) workoutsEditHandler(c echo.Context) error {
data := a.defaultData(c)
workout, err := a.getWorkout(c)
if err != nil {
return a.redirectWithError(c, "/workouts", err)
}
data["workout"] = workout
return c.Render(http.StatusOK, "workouts_edit.html", data)
}