-
Notifications
You must be signed in to change notification settings - Fork 19
/
share.go
57 lines (49 loc) · 1.19 KB
/
share.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
package main
import (
"encoding/json"
"log"
"net/http"
"time"
)
func getShare(w http.ResponseWriter, r *http.Request) {
u, _, ok := findUser(r)
if !ok {
http.Error(w, "you are not logged in", http.StatusUnauthorized)
return
}
var share bool
if err := db.QueryRow("SELECT share_info FROM users WHERE id = $1", u.UserID).Scan(&share); err != nil {
log.Println(err)
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(share); err != nil {
log.Println(err)
}
}
func updateShare(w http.ResponseWriter, r *http.Request) {
u, _, ok := findUser(r)
if !ok {
http.Error(w, "you are not logged in", http.StatusUnauthorized)
return
}
var share bool
if err := json.NewDecoder(r.Body).Decode(&share); err != nil {
log.Println(err)
http.Error(w, "invalid request", http.StatusBadRequest)
return
}
_, err := db.Exec(
"UPDATE users SET share_info = $1, updated_at = $2 WHERE id = $3",
share,
time.Now(),
u.UserID,
)
if err != nil {
log.Println(err)
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}