-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavorite.go
59 lines (42 loc) · 944 Bytes
/
favorite.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
package main;
import (
"log"
"github.com/husobee/vestigo"
"net/http"
)
func FavoriteCB(w http.ResponseWriter,r *http.Request) {
pledge()
dbh := ctx.dbh;
id := vestigo.Param(r, "id");
str := auth(r.Header.Get("X-Up-Auth"));
if (str == "") {
tx, err := dbh.Begin()
if (err != nil) {
log.Fatal(err);
}
insert, err := tx.Prepare("update posts set favorites = favorites + 1 where id = ?");
if (err != nil) {
log.Fatal(err)
}
result, err := insert.Exec(id);
if (err != nil) {
log.Fatal(err);
}
affected,_ := result.RowsAffected();
if (affected != 1) {
w.WriteHeader(400)
w.Write(([]byte)("Bad post ID"))
return;
}
err = tx.Commit()
if (err != nil) {
log.Fatal(err)
}
insert.Close()
post := NewPost(config.Owner + " liked the post https://" + config.Server + "/post/" + id);
Put(ctx.dbh, &post);
DistributePost(post);
}
w.WriteHeader(401)
w.Write(([]byte)(str))
}