forked from miniflux/v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory.go
283 lines (242 loc) · 7.85 KB
/
category.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package storage // import "miniflux.app/storage"
import (
"database/sql"
"errors"
"fmt"
"github.com/lib/pq"
"miniflux.app/model"
)
// AnotherCategoryExists checks if another category exists with the same title.
func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string) bool {
var result bool
query := `SELECT true FROM categories WHERE user_id=$1 AND id != $2 AND lower(title)=lower($3) LIMIT 1`
s.db.QueryRow(query, userID, categoryID, title).Scan(&result)
return result
}
// CategoryTitleExists checks if the given category exists into the database.
func (s *Storage) CategoryTitleExists(userID int64, title string) bool {
var result bool
query := `SELECT true FROM categories WHERE user_id=$1 AND lower(title)=lower($2) LIMIT 1`
s.db.QueryRow(query, userID, title).Scan(&result)
return result
}
// CategoryIDExists checks if the given category exists into the database.
func (s *Storage) CategoryIDExists(userID, categoryID int64) bool {
var result bool
query := `SELECT true FROM categories WHERE user_id=$1 AND id=$2`
s.db.QueryRow(query, userID, categoryID).Scan(&result)
return result
}
// Category returns a category from the database.
func (s *Storage) Category(userID, categoryID int64) (*model.Category, error) {
var category model.Category
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 AND id=$2`
err := s.db.QueryRow(query, userID, categoryID).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
default:
return &category, nil
}
}
// FirstCategory returns the first category for the given user.
func (s *Storage) FirstCategory(userID int64) (*model.Category, error) {
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 ORDER BY title ASC LIMIT 1`
var category model.Category
err := s.db.QueryRow(query, userID).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
default:
return &category, nil
}
}
// CategoryByTitle finds a category by the title.
func (s *Storage) CategoryByTitle(userID int64, title string) (*model.Category, error) {
var category model.Category
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 AND title=$2`
err := s.db.QueryRow(query, userID, title).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
default:
return &category, nil
}
}
// Categories returns all categories that belongs to the given user.
func (s *Storage) Categories(userID int64) (model.Categories, error) {
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 ORDER BY title ASC`
rows, err := s.db.Query(query, userID)
if err != nil {
return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
}
defer rows.Close()
categories := make(model.Categories, 0)
for rows.Next() {
var category model.Category
if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally); err != nil {
return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
}
categories = append(categories, &category)
}
return categories, nil
}
// CategoriesWithFeedCount returns all categories with the number of feeds.
func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error) {
user, err := s.UserByID(userID)
if err != nil {
return nil, err
}
query := `
SELECT
c.id,
c.user_id,
c.title,
c.hide_globally,
(SELECT count(*) FROM feeds WHERE feeds.category_id=c.id) AS count,
(SELECT count(*)
FROM feeds
JOIN entries ON (feeds.id = entries.feed_id)
WHERE feeds.category_id = c.id AND entries.status = 'unread') AS count_unread
FROM categories c
WHERE
user_id=$1
`
if user.CategoriesSortingOrder == "alphabetical" {
query = query + `
ORDER BY
c.title ASC
`
} else {
query = query + `
ORDER BY
count_unread DESC,
c.title ASC
`
}
rows, err := s.db.Query(query, userID)
if err != nil {
return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
}
defer rows.Close()
categories := make(model.Categories, 0)
for rows.Next() {
var category model.Category
if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally, &category.FeedCount, &category.TotalUnread); err != nil {
return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
}
categories = append(categories, &category)
}
return categories, nil
}
// CreateCategory creates a new category.
func (s *Storage) CreateCategory(userID int64, request *model.CategoryRequest) (*model.Category, error) {
var category model.Category
query := `
INSERT INTO categories
(user_id, title)
VALUES
($1, $2)
RETURNING
id,
user_id,
title
`
err := s.db.QueryRow(
query,
userID,
request.Title,
).Scan(
&category.ID,
&category.UserID,
&category.Title,
)
if err != nil {
return nil, fmt.Errorf(`store: unable to create category %q: %v`, request.Title, err)
}
return &category, nil
}
// UpdateCategory updates an existing category.
func (s *Storage) UpdateCategory(category *model.Category) error {
query := `UPDATE categories SET title=$1, hide_globally = $2 WHERE id=$3 AND user_id=$4`
_, err := s.db.Exec(
query,
category.Title,
category.HideGlobally,
category.ID,
category.UserID,
)
if err != nil {
return fmt.Errorf(`store: unable to update category: %v`, err)
}
return nil
}
// RemoveCategory deletes a category.
func (s *Storage) RemoveCategory(userID, categoryID int64) error {
query := `DELETE FROM categories WHERE id = $1 AND user_id = $2`
result, err := s.db.Exec(query, categoryID, userID)
if err != nil {
return fmt.Errorf(`store: unable to remove this category: %v`, err)
}
count, err := result.RowsAffected()
if err != nil {
return fmt.Errorf(`store: unable to remove this category: %v`, err)
}
if count == 0 {
return errors.New(`store: no category has been removed`)
}
return nil
}
// delete the given categories, replacing those categories with the user's first
// category on affected feeds
func (s *Storage) RemoveAndReplaceCategoriesByName(userid int64, titles []string) error {
tx, err := s.db.Begin()
if err != nil {
return errors.New("unable to begin transaction")
}
titleParam := pq.Array(titles)
var count int
query := "SELECT count(*) FROM categories WHERE user_id = $1 and title != ANY($2)"
err = tx.QueryRow(query, userid, titleParam).Scan(&count)
if err != nil {
tx.Rollback()
return errors.New("unable to retrieve category count")
}
if count < 1 {
tx.Rollback()
return errors.New("at least 1 category must remain after deletion")
}
query = `
WITH d_cats AS (SELECT id FROM categories WHERE user_id = $1 AND title = ANY($2))
UPDATE feeds
SET category_id =
(SELECT id
FROM categories
WHERE user_id = $1 AND id NOT IN (SELECT id FROM d_cats)
ORDER BY title ASC
LIMIT 1)
WHERE user_id = $1 AND category_id IN (SELECT id FROM d_cats)
`
_, err = tx.Exec(query, userid, titleParam)
if err != nil {
tx.Rollback()
return fmt.Errorf("unable to replace categories: %v", err)
}
query = "DELETE FROM categories WHERE user_id = $1 AND title = ANY($2)"
_, err = tx.Exec(query, userid, titleParam)
if err != nil {
tx.Rollback()
return fmt.Errorf("unable to delete categories: %v", err)
}
tx.Commit()
return nil
}