-
Notifications
You must be signed in to change notification settings - Fork 23
/
handlers.go
55 lines (39 loc) · 1.24 KB
/
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
package app
import (
"errors"
"net/http"
"github.com/labstack/echo/v4"
)
var ErrUserNotFound = errors.New("user not found")
func (a *App) redirectWithError(c echo.Context, target string, err error) error {
if err != nil {
a.setError(c, "Something went wrong: "+err.Error())
}
return c.Redirect(http.StatusFound, target)
}
func (a *App) statisticsHandler(c echo.Context) error {
data := a.defaultData(c)
return c.Render(http.StatusOK, "user_statistics.html", data)
}
func (a *App) dashboardHandler(c echo.Context) error {
data := a.defaultData(c)
u := a.getCurrentUser(c)
if u == nil {
return a.redirectWithError(c, a.echo.Reverse("user-signout"), ErrUserNotFound)
}
if err := a.addWorkouts(u, data); err != nil {
return a.redirectWithError(c, a.echo.Reverse("user-signout"), err)
}
if err := a.addUsers(data); err != nil {
return a.redirectWithError(c, a.echo.Reverse("user-signout"), err)
}
if err := a.addRecentWorkouts(data); err != nil {
return a.redirectWithError(c, a.echo.Reverse("user-signout"), err)
}
data["user"] = u
return c.Render(http.StatusOK, "user_show.html", data)
}
func (a *App) userLoginHandler(c echo.Context) error {
data := a.defaultData(c)
return c.Render(http.StatusOK, "user_login.html", data)
}