-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupdate_pin_handler.go
More file actions
67 lines (57 loc) · 1.73 KB
/
Copy pathupdate_pin_handler.go
File metadata and controls
67 lines (57 loc) · 1.73 KB
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
package main
import (
"crypto/subtle"
"fmt"
"net/http"
"strconv"
"time"
)
type UpdatePinHandler HandlerWithDBConnection
func (hd *UpdatePinHandler) ServeHTTP(resp http.ResponseWriter, request *http.Request) {
db := hd.db
session, err := sessionFromRequest(request, db)
if err == ErrInvalidSession {
fmt.Println(err)
clearSessionCookie(resp)
}
signedIn := err == nil
if !signedIn {
fmt.Println("updating pins: not signed in")
http.Error(resp, "Unauthorized", http.StatusUnauthorized)
return
}
if subtle.ConstantTimeCompare([]byte(request.PostFormValue("csrf_token")), []byte(session.csrfToken)) != 1 {
fmt.Println("invalid csrf token")
http.Error(resp, "Invalid CSRF token", http.StatusBadRequest)
return
}
keepSessionAlive(resp, db, session)
latStr := request.PostFormValue("lat")
lonStr := request.PostFormValue("lon")
timestamp := time.Now().Unix()
if latStr == "" && lonStr == "" {
_, err = db.Exec("update users set lat = null, lon = null, pin_updated_at = null where user_id = ?", session.userId)
if err != nil {
fmt.Println("updating pin position:", err)
http.Error(resp, "Internal server error", http.StatusInternalServerError)
return
}
} else {
var lat, lon float64
lat, err = strconv.ParseFloat(latStr, 64)
if err == nil {
lon, err = strconv.ParseFloat(lonStr, 64)
}
if err != nil {
fmt.Println("parsing lat/lon values:", err)
http.Error(resp, "Bad request", http.StatusBadRequest)
return
}
_, err = db.Exec("update users set lat = ?, lon = ?, pin_updated_at = ? where user_id = ?", lat, lon, timestamp, session.userId)
if err != nil {
fmt.Println("updating pin position:", err)
http.Error(resp, "Internal server error", http.StatusInternalServerError)
return
}
}
}