-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocationUpdateHandler.go
More file actions
38 lines (30 loc) · 1.07 KB
/
locationUpdateHandler.go
File metadata and controls
38 lines (30 loc) · 1.07 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
package main
import (
"encoding/json"
"net/http"
)
func locationUpdateHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
req := struct {
APIToken string `json:"api_token"`
Lat float64 `json:"latitude"`
Lng float64 `json:"longitude"`
}{"", 0, 0}
err := decoder.Decode(&req)
if err != nil {
failWithStatusCode(err, http.StatusText(http.StatusBadRequest), w, http.StatusBadRequest)
return
}
LocJSON := formatGeoSON(req.Lng, req.Lat)
queryString := "INSERT INTO location (u_id, help_location) " +
"SELECT u_id, ST_GeomFromGeoJSON($2) " +
"FROM users where api_token LIKE $1 " +
"ON CONFLICT (u_id) " +
"DO UPDATE SET help_location = ST_GeomFromGeoJSON($2);"
stmt, err := db.Prepare(queryString)
_, err = stmt.Exec(req.APIToken, LocJSON)
if err != nil {
failWithStatusCode(err, "failed to update location", w, http.StatusInternalServerError)
return
}
}