forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_topic_view.go
76 lines (71 loc) · 1.81 KB
/
user_topic_view.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
package db
import (
"github.com/jchavannes/jgo/jerr"
"time"
)
type UserTopicView struct {
Id uint `gorm:"primary_key"`
UserPkHash []byte
Topic string
LastPostId uint
CreatedAt time.Time
UpdatedAt time.Time
}
func GetLastTopicPostIds(userPkHash []byte, topics []string) ([]*UserTopicView, error) {
db, err := getDb()
if err != nil {
return nil, jerr.Get("error getting db", err)
}
var userTopicViews []*UserTopicView
result := db.
Where("user_pk_hash = ?", userPkHash).
Where("topic IN (?)", topics).
Find(&userTopicViews)
if result.Error != nil {
return nil, jerr.Get("error running last topic post ids query", result.Error)
}
return userTopicViews, nil
}
func GetLastTopicPostId(userPkHash []byte, topic string) (uint, error) {
var userTopicView UserTopicView
err := find(&userTopicView, UserTopicView{
UserPkHash: userPkHash,
Topic: topic,
})
if err == nil {
return userTopicView.LastPostId, nil
}
if ! IsRecordNotFoundError(err) {
return 0, jerr.Get("error finding last post id", err)
}
return 0, nil
}
func SetLastTopicPostId(userPkHash []byte, topic string, lastPostId uint) error {
var userTopicView UserTopicView
err := find(&userTopicView, UserTopicView{
UserPkHash: userPkHash,
Topic: topic,
})
if err != nil {
if ! IsRecordNotFoundError(err) {
return jerr.Get("error getting last user topic viewed from db", err)
}
userTopicView = UserTopicView{
UserPkHash: userPkHash,
Topic: topic,
LastPostId: lastPostId,
}
err := create(&userTopicView)
if err != nil {
return jerr.Get("error creating user topic viewed", err)
}
return nil
} else {
userTopicView.LastPostId = lastPostId
result := save(userTopicView)
if result.Error != nil {
return jerr.Get("error saving user topic viewed", result.Error)
}
return nil
}
}