/
favorite.go
129 lines (101 loc) · 2.87 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package db
import (
cryptoRand "crypto/rand"
"database/sql"
"encoding/binary"
"time"
)
type Favorite struct {
id int
UserId int64
UserName string
TweetId int64
Status string
FavDate time.Time
UnfavDate time.Time
LastAction time.Time
}
const (
_TABLE_FAVORITE = "favorite"
)
func GenerateCryptoRandIntForFavorite(max int) (int, error) {
var n uint64
err := binary.Read(cryptoRand.Reader, binary.LittleEndian, &n)
if err != nil {
return 0, err
}
return int(n % uint64(max)), nil
}
func (fav Favorite) Persist() error {
var stmtIns *sql.Stmt
var err error
if fav.id == 0 {
stmtIns, err = database.Prepare("INSERT INTO " + _TABLE_FAVORITE + "(userId, userName, tweetId, status, favDate, unfavDate, lastAction) VALUES( $1, $2, $3, $4, $5, $6, $7)")
} else {
stmtIns, err = database.Prepare("UPDATE " + _TABLE_FAVORITE + " SET userId = $1, userName = $2, tweetId = $3, status = $4, favDate = $5, unfavDate = $6, lastAction = $7 WHERE id = $8")
}
if err != nil {
return err
}
defer stmtIns.Close()
unfavDate := sql.NullTime{Time: fav.UnfavDate, Valid: !fav.UnfavDate.IsZero()}
if fav.id == 0 {
_, err = stmtIns.Exec(fav.UserId, fav.UserName, fav.TweetId, fav.Status, fav.FavDate, unfavDate, time.Now())
} else {
_, err = stmtIns.Exec(fav.UserId, fav.UserName, fav.TweetId, fav.Status, fav.FavDate, unfavDate, fav.LastAction, fav.id)
}
return err
}
func HasAlreadyFav(tweetId int64) (bool, error) {
stmtOut, err := database.Prepare("SELECT count(*) FROM " + _TABLE_FAVORITE + " WHERE tweetId = $1 LIMIT 1")
if err != nil {
return true, err
}
defer stmtOut.Close()
var size int
err = stmtOut.QueryRow(tweetId).Scan(&size)
if err != nil {
return true, err
}
return size > 0, nil
}
func GetNotUnfavorite(maxFavDate time.Time, limit int) ([]Favorite, error) {
favs := make([]Favorite, 0)
stmtOut, err := database.Prepare("SELECT * FROM " + _TABLE_FAVORITE + " WHERE unfavDate IS NULL AND favDate <= $1 ORDER BY lastAction LIMIT $2")
if err != nil {
return favs, err
}
defer stmtOut.Close()
rows, err := stmtOut.Query(maxFavDate, limit)
if err != nil {
return favs, err
}
defer rows.Close()
for rows.Next() {
fav, err := mapFav(rows)
if err != nil {
return favs, err
}
favs = append(favs, fav)
}
return favs, nil
}
func mapFav(rows *sql.Rows) (Favorite, error) {
var id int
var userId int64
var userName string
var tweetId int64
var status string
var favDate time.Time
var unfavDate sql.NullTime
var lastAction time.Time
err := rows.Scan(&id, &userId, &userName, &tweetId, &status, &favDate, &unfavDate, &lastAction)
if err != nil {
return Favorite{}, err
}
var unfavTime time.Time
if unfavDate.Valid {
unfavTime = unfavDate.Time
}
return Favorite{id: id, UserId: userId, UserName: userName, TweetId: tweetId, Status: status, FavDate: favDate, UnfavDate: unfavTime, LastAction: lastAction}, nil
}