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
|
|
|
|
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
|
|
|
|
query := `SELECT true FROM categories WHERE user_id=$1 AND id != $2 AND title=$3`
|
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
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// CategoryExists checks if the given category exists into the database.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) CategoryExists(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
|
|
|
|
|
|
|
|
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 AND id=$2`
|
|
|
|
err := s.db.QueryRow(query, userID, categoryID).Scan(&category.ID, &category.UserID, &category.Title)
|
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) {
|
|
|
|
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 ORDER BY title ASC LIMIT 1`
|
2019-10-30 01:48:07 -04:00
|
|
|
|
|
|
|
var category model.Category
|
2017-11-20 00:10:04 -05:00
|
|
|
err := s.db.QueryRow(query, userID).Scan(&category.ID, &category.UserID, &category.Title)
|
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
|
|
|
|
|
|
|
|
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 AND title=$2`
|
|
|
|
err := s.db.QueryRow(query, userID, title).Scan(&category.ID, &category.UserID, &category.Title)
|
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) {
|
|
|
|
query := `SELECT id, user_id, title 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
|
|
|
|
if err := rows.Scan(&category.ID, &category.UserID, &category.Title); 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,
|
|
|
|
(SELECT count(*) FROM feeds WHERE feeds.category_id=c.id) AS count
|
|
|
|
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
|
|
|
|
if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.FeedCount); 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.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) CreateCategory(category *model.Category) error {
|
|
|
|
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
|
|
|
|
id
|
2017-11-20 00:10:04 -05:00
|
|
|
`
|
|
|
|
err := s.db.QueryRow(
|
|
|
|
query,
|
|
|
|
category.UserID,
|
|
|
|
category.Title,
|
|
|
|
).Scan(&category.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to create category: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
query := `UPDATE categories SET title=$1 WHERE id=$2 AND user_id=$3`
|
|
|
|
_, err := s.db.Exec(
|
|
|
|
query,
|
|
|
|
category.Title,
|
|
|
|
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
|
|
|
|
}
|