2017-11-20 00:10:04 -05:00
|
|
|
// 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.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package storage // import "miniflux.app/storage"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-11-21 17:57:27 -05:00
|
|
|
|
2022-01-02 22:45:12 -05:00
|
|
|
"github.com/lib/pq"
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/model"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// AnotherCategoryExists checks if another category exists with the same title.
|
2017-11-21 17:57:27 -05:00
|
|
|
func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string) bool {
|
2019-10-30 01:48:07 -04:00
|
|
|
var result bool
|
2021-01-04 01:28:04 -05:00
|
|
|
query := `SELECT true FROM categories WHERE user_id=$1 AND id != $2 AND lower(title)=lower($3) LIMIT 1`
|
2017-11-21 17:57:27 -05:00
|
|
|
s.db.QueryRow(query, userID, categoryID, title).Scan(&result)
|
2019-10-30 01:48:07 -04:00
|
|
|
return result
|
2017-11-21 17:57:27 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 01:28:04 -05:00
|
|
|
// 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 {
|
2019-10-30 01:48:07 -04:00
|
|
|
var result bool
|
|
|
|
query := `SELECT true FROM categories WHERE user_id=$1 AND id=$2`
|
2017-11-20 00:10:04 -05:00
|
|
|
s.db.QueryRow(query, userID, categoryID).Scan(&result)
|
2019-10-30 01:48:07 -04:00
|
|
|
return result
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// Category returns a category from the database.
|
|
|
|
func (s *Storage) Category(userID, categoryID int64) (*model.Category, error) {
|
2017-11-20 00:10:04 -05:00
|
|
|
var category model.Category
|
|
|
|
|
2021-06-02 20:39:47 -04:00
|
|
|
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)
|
2019-10-30 01:48:07 -04:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil, nil
|
2019-10-30 01:48:07 -04:00
|
|
|
case err != nil:
|
|
|
|
return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
|
|
|
|
default:
|
|
|
|
return &category, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// FirstCategory returns the first category for the given user.
|
|
|
|
func (s *Storage) FirstCategory(userID int64) (*model.Category, error) {
|
2021-06-02 20:39:47 -04:00
|
|
|
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 ORDER BY title ASC LIMIT 1`
|
2019-10-30 01:48:07 -04:00
|
|
|
|
|
|
|
var category model.Category
|
2021-06-02 20:39:47 -04:00
|
|
|
err := s.db.QueryRow(query, userID).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
|
2019-10-30 01:48:07 -04:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil, nil
|
2019-10-30 01:48:07 -04:00
|
|
|
case err != nil:
|
|
|
|
return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
|
|
|
|
default:
|
|
|
|
return &category, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// CategoryByTitle finds a category by the title.
|
|
|
|
func (s *Storage) CategoryByTitle(userID int64, title string) (*model.Category, error) {
|
2017-11-20 00:10:04 -05:00
|
|
|
var category model.Category
|
|
|
|
|
2021-06-02 20:39:47 -04:00
|
|
|
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)
|
2019-10-30 01:48:07 -04:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil, nil
|
2019-10-30 01:48:07 -04:00
|
|
|
case err != nil:
|
|
|
|
return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
|
|
|
|
default:
|
|
|
|
return &category, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// Categories returns all categories that belongs to the given user.
|
|
|
|
func (s *Storage) Categories(userID int64) (model.Categories, error) {
|
2021-06-02 20:39:47 -04:00
|
|
|
query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 ORDER BY title ASC`
|
2017-11-20 00:10:04 -05:00
|
|
|
rows, err := s.db.Query(query, userID)
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
categories := make(model.Categories, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var category model.Category
|
2021-06-02 20:39:47 -04:00
|
|
|
if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally); err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
categories = append(categories, &category)
|
|
|
|
}
|
|
|
|
|
|
|
|
return categories, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// CategoriesWithFeedCount returns all categories with the number of feeds.
|
|
|
|
func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error) {
|
2019-10-30 01:48:07 -04:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
c.id,
|
|
|
|
c.user_id,
|
|
|
|
c.title,
|
2021-06-02 20:39:47 -04:00
|
|
|
c.hide_globally,
|
2021-04-25 07:47:59 -04:00
|
|
|
(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')
|
2019-10-30 01:48:07 -04:00
|
|
|
FROM categories c
|
|
|
|
WHERE
|
|
|
|
user_id=$1
|
|
|
|
ORDER BY c.title ASC
|
|
|
|
`
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
rows, err := s.db.Query(query, userID)
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
categories := make(model.Categories, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var category model.Category
|
2021-06-02 20:39:47 -04:00
|
|
|
if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally, &category.FeedCount, &category.TotalUnread); err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
categories = append(categories, &category)
|
|
|
|
}
|
|
|
|
|
|
|
|
return categories, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// CreateCategory creates a new category.
|
2021-01-04 01:28:04 -05:00
|
|
|
func (s *Storage) CreateCategory(userID int64, request *model.CategoryRequest) (*model.Category, error) {
|
|
|
|
var category model.Category
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
query := `
|
|
|
|
INSERT INTO categories
|
2019-10-30 01:48:07 -04:00
|
|
|
(user_id, title)
|
2017-11-20 00:10:04 -05:00
|
|
|
VALUES
|
2019-10-30 01:48:07 -04:00
|
|
|
($1, $2)
|
|
|
|
RETURNING
|
2021-01-04 01:28:04 -05:00
|
|
|
id,
|
|
|
|
user_id,
|
|
|
|
title
|
2017-11-20 00:10:04 -05:00
|
|
|
`
|
|
|
|
err := s.db.QueryRow(
|
|
|
|
query,
|
2021-01-04 01:28:04 -05:00
|
|
|
userID,
|
|
|
|
request.Title,
|
|
|
|
).Scan(
|
|
|
|
&category.ID,
|
|
|
|
&category.UserID,
|
|
|
|
&category.Title,
|
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2021-01-04 01:28:04 -05:00
|
|
|
return nil, fmt.Errorf(`store: unable to create category %q: %v`, request.Title, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 01:28:04 -05:00
|
|
|
return &category, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// UpdateCategory updates an existing category.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) UpdateCategory(category *model.Category) error {
|
2021-06-02 20:39:47 -04:00
|
|
|
query := `UPDATE categories SET title=$1, hide_globally = $2 WHERE id=$3 AND user_id=$4`
|
2017-11-20 00:10:04 -05:00
|
|
|
_, err := s.db.Exec(
|
|
|
|
query,
|
|
|
|
category.Title,
|
2021-06-02 20:39:47 -04:00
|
|
|
category.HideGlobally,
|
2017-11-20 00:10:04 -05:00
|
|
|
category.ID,
|
|
|
|
category.UserID,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to update category: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// RemoveCategory deletes a category.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) RemoveCategory(userID, categoryID int64) error {
|
2019-10-30 01:48:07 -04:00
|
|
|
query := `DELETE FROM categories WHERE id = $1 AND user_id = $2`
|
|
|
|
result, err := s.db.Exec(query, categoryID, userID)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to remove this category: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
count, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to remove this category: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
2019-10-30 01:48:07 -04:00
|
|
|
return errors.New(`store: no category has been removed`)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-02 22:45:12 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|