-
Notifications
You must be signed in to change notification settings - Fork 33
/
article_get.go
177 lines (152 loc) · 4.56 KB
/
article_get.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package postgres
import (
"strings"
sq "github.com/Masterminds/squirrel"
"github.com/ncarlier/readflow/pkg/model"
)
// CountArticles count articles
func (pg *DB) CountArticles(status string) (uint, error) {
counter := pg.psql.Select("count(*)").From(
"articles",
)
if status != "" {
counter = counter.Where(sq.Eq{"status": status})
}
query, args, _ := counter.ToSql()
var count uint
if err := pg.db.QueryRow(query, args...).Scan(&count); err != nil {
return 0, err
}
return count, nil
}
// CountArticlesByUser returns total nb of articles of an user from the DB
func (pg *DB) CountArticlesByUser(uid uint, req model.ArticlesPageRequest) (uint, error) {
counter := pg.psql.Select("count(*)").From(
"articles",
).Where(sq.Eq{"user_id": uid})
if req.Category != nil {
counter = counter.Where(sq.Eq{"category_id": *req.Category})
}
if req.Status != nil {
counter = counter.Where(sq.Eq{"status": *req.Status})
}
if req.Starred != nil {
if *req.Starred {
counter = counter.Where(sq.Gt{"stars": 0})
} else {
counter = counter.Where(sq.Eq{"stars": 0})
}
}
if req.Query != nil && strings.TrimSpace(*req.Query) != "" {
counter = counter.Where(sq.Expr("search_vectors @@ websearch_to_tsquery(?)", *req.Query))
}
query, args, _ := counter.ToSql()
var count uint
if err := pg.db.QueryRow(query, args...).Scan(&count); err != nil {
return 0, err
}
return count, nil
}
// GetPaginatedArticlesByUser returns a paginated list of user's articles from the DB
func (pg *DB) GetPaginatedArticlesByUser(uid uint, req model.ArticlesPageRequest) (*model.ArticlesPageResponse, error) {
// Set defaults
limit := 20
if req.Limit != nil {
limit = *req.Limit
}
sortOrder := "asc"
if req.SortOrder != nil {
// Note that sort order is ignored when full-text query is used
sortOrder = *req.SortOrder
}
// Count articles
total, err := pg.CountArticlesByUser(uid, req)
if err != nil {
return nil, err
}
result := model.ArticlesPageResponse{
TotalCount: total,
HasNext: false,
}
selectBuilder := pg.psql.Select(articleColumns...).From(
"articles",
).Where(sq.Eq{"user_id": uid})
if req.Category != nil {
selectBuilder = selectBuilder.Where(sq.Eq{"category_id": *req.Category})
}
if req.Status != nil {
selectBuilder = selectBuilder.Where(sq.Eq{"status": *req.Status})
}
if req.Starred != nil {
if *req.Starred {
selectBuilder = selectBuilder.Where(sq.Gt{"stars": 0})
} else {
selectBuilder = selectBuilder.Where(sq.Eq{"stars": 0})
}
}
var offset uint
isFullText := req.Query != nil && strings.TrimSpace(*req.Query) != ""
isSortedBy := req.SortBy != nil && strings.TrimSpace(*req.SortBy) != "key"
if isFullText {
// Full-text search query:
// Classic Limit-Offset pagination (beware of performance issue)
selectBuilder = selectBuilder.Where(sq.Expr("search_vectors @@ websearch_to_tsquery(?)", *req.Query))
selectBuilder = selectBuilder.OrderByClause("ts_rank(search_vectors, websearch_to_tsquery(?)) DESC", *req.Query)
if req.AfterCursor != nil {
offset = *req.AfterCursor
selectBuilder = selectBuilder.Offset(uint64(offset))
}
} else if isSortedBy {
// Search query with order by:
// Classic Limit-Offset pagination (beware of performance issue)
by := strings.TrimSpace(*req.SortBy)
selectBuilder = selectBuilder.OrderBy(by + " " + strings.ToUpper(sortOrder))
if req.AfterCursor != nil {
offset = *req.AfterCursor
selectBuilder = selectBuilder.Offset(uint64(offset))
}
} else {
// Standard search:
// Keyset Pagination using id as ordered column
if req.AfterCursor != nil {
if sortOrder == "asc" {
selectBuilder = selectBuilder.Where(sq.Gt{"id": *req.AfterCursor})
} else {
selectBuilder = selectBuilder.Where(sq.Lt{"id": *req.AfterCursor})
}
}
selectBuilder = selectBuilder.OrderBy("id " + strings.ToUpper(sortOrder))
}
// Limit the query by "plus-one" in order to known if the query has more results
selectBuilder = selectBuilder.Limit(uint64(limit + 1))
query, args, _ := selectBuilder.ToSql()
rows, err := pg.db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
// Read resultset...
var index int
for rows.Next() {
index++
article := &model.Article{}
if err := mapRowsToArticle(rows, article); err != nil {
return nil, err
}
if index <= limit {
result.Entries = append(result.Entries, article)
if isFullText || isSortedBy {
result.EndCursor = offset + uint(index)
} else {
result.EndCursor = article.ID
}
} else {
result.HasNext = true
}
}
err = rows.Err()
if err != nil {
return nil, err
}
return &result, nil
}