-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrud.go
72 lines (59 loc) · 1.89 KB
/
crud.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
package main
import (
"net/http"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
)
func saveLocation(e *core.RequestEvent, app *pocketbase.PocketBase) error {
//validate auth token before anything
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.")
}
//get location and id from request.
location := e.Request.FormValue("location")
id_cookie, err := e.Request.Cookie("id")
id := id_cookie.Value
//specfiy collection
collection, err := app.FindCollectionByNameOrId("favorite_locations")
if err != nil {
return err
}
//create record programtically instead of with http request
record := core.NewRecord(collection)
record.Set("location", location)
record.Set("user_id", id)
err = app.Save(record)
if err != nil {
return err
}
//return data. TODO return everything in collection with places view.
return e.Redirect(303, "/my_places")
}
func deleteLocation(e *core.RequestEvent, app *pocketbase.PocketBase) error {
//validate auth token before anything
err := validateToken(e, app)
if err != nil {
return e.String(http.StatusOK, "Looks like you do not have a valid token, please login to delete a location")
}
//get the location from the request
location := e.Request.URL.Query().Get("location")
if location == "" {
return e.String(http.StatusBadRequest, "Location not provided")
}
//get user's id from the request
id_cookie, err := e.Request.Cookie("id")
id := id_cookie.Value
record, err := app.FindFirstRecordByFilter(
"favorite_locations",
"location = {:location} && user_id = {:user_id}",
dbx.Params{"location": location, "user_id": id},
)
err = app.Delete(record)
if err != nil {
return err
}
//return data. TODO return everything in collection with places view.
return e.Redirect(303, "/my_places")
}