-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtml.go
101 lines (90 loc) · 2.47 KB
/
html.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
package main
import (
"net/http"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/template"
)
func serveIndex(e *core.RequestEvent, registry *template.Registry) error {
//set default status to not logged in.
var isLoggedIn bool
isLoggedIn = false
//if logged in render template with logged in assertion.
cookie, _ := e.Request.Cookie("explore_token")
if cookie != nil {
isLoggedIn = true
}
html, err := registry.LoadFiles(
"views/layout.html",
"views/nav.html",
"views/index.html",
).Render(map[string]any{
"isLoggedIn": isLoggedIn,
})
if err != nil {
return apis.NewNotFoundError("", err)
}
return e.HTML(http.StatusOK, html)
}
func serveLogin(e *core.RequestEvent, registry *template.Registry) error {
html, err := registry.LoadFiles(
"views/layout.html",
"views/login.html",
).Render(map[string]any{
"hello": "hello",
})
if err != nil {
return apis.NewNotFoundError("", err)
}
return e.HTML(http.StatusOK, html)
}
func serveRegister(e *core.RequestEvent, registry *template.Registry) error {
html, err := registry.LoadFiles(
"views/layout.html",
"views/register.html",
).Render(map[string]any{
"hello": "hello",
})
if err != nil {
return apis.NewNotFoundError("", err)
}
return e.HTML(http.StatusOK, html)
}
func servePlaces(e *core.RequestEvent, registry *template.Registry, app *pocketbase.PocketBase) error {
//validate token
err := validateToken(e, app)
if err != nil {
return e.String(http.StatusOK, "Looks like you do not have a valid token, please login to save a location.")
}
//Extract user_id from request
id, err := e.Request.Cookie("id")
if err != nil {
return e.String(http.StatusOK, "Please log in to save a location")
}
id_cookie := id.Value
//Find all places for user.
records, err := app.FindAllRecords("favorite_locations", dbx.NewExp("LOWER(`user_id`) = LOWER({:user_id})", dbx.Params{"user_id": id_cookie}))
if err != nil {
return err
}
var locationList []string
for _, record := range records {
locationName := record.GetString("location")
if locationName != "" {
locationList = append(locationList, locationName)
}
}
//render template
html, err := registry.LoadFiles(
"views/layout.html",
"views/places.html",
).Render(map[string]any{
"places": locationList,
})
if err != nil {
return apis.NewNotFoundError("", err)
}
return e.HTML(http.StatusOK, html)
}