miniflux/storage/category.go

218 lines
6.2 KiB
Go
Raw Normal View History

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