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.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-11-21 17:57:27 -05:00
|
|
|
"time"
|
|
|
|
|
2017-12-13 00:48:13 -05:00
|
|
|
"github.com/miniflux/miniflux/model"
|
2018-01-02 22:15:08 -05:00
|
|
|
"github.com/miniflux/miniflux/timer"
|
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 {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:AnotherCategoryExists] userID=%d, categoryID=%d, title=%s", userID, categoryID, title))
|
2017-11-21 17:57:27 -05:00
|
|
|
|
|
|
|
var result int
|
|
|
|
query := `SELECT count(*) as c FROM categories WHERE user_id=$1 AND id != $2 AND title=$3`
|
|
|
|
s.db.QueryRow(query, userID, categoryID, title).Scan(&result)
|
|
|
|
return result >= 1
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CategoryExists] userID=%d, categoryID=%d", userID, categoryID))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
var result int
|
|
|
|
query := `SELECT count(*) as c FROM categories WHERE user_id=$1 AND id=$2`
|
|
|
|
s.db.QueryRow(query, userID, categoryID).Scan(&result)
|
|
|
|
return result >= 1
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:Category] userID=%d, getCategory=%d", userID, categoryID))
|
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)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
2017-11-28 00:30:04 -05:00
|
|
|
return nil, fmt.Errorf("unable to fetch category: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &category, nil
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:FirstCategory] userID=%d", userID))
|
2017-11-20 00:10:04 -05:00
|
|
|
var category model.Category
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 ORDER BY title ASC LIMIT 1`
|
2017-11-20 00:10:04 -05:00
|
|
|
err := s.db.QueryRow(query, userID).Scan(&category.ID, &category.UserID, &category.Title)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
2017-11-28 00:30:04 -05:00
|
|
|
return nil, fmt.Errorf("unable to fetch category: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &category, nil
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CategoryByTitle] userID=%d, title=%s", userID, title))
|
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)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to fetch category: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &category, nil
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:Categories] userID=%d", userID))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("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); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to fetch categories row: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CategoriesWithFeedCount] userID=%d", userID))
|
2017-11-20 00:10:04 -05: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`
|
|
|
|
|
|
|
|
rows, err := s.db.Query(query, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("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.FeedCount); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to fetch categories row: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CreateCategory] title=%s", category.Title))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
query := `
|
|
|
|
INSERT INTO categories
|
|
|
|
(user_id, title)
|
|
|
|
VALUES
|
|
|
|
($1, $2)
|
|
|
|
RETURNING id
|
|
|
|
`
|
|
|
|
err := s.db.QueryRow(
|
|
|
|
query,
|
|
|
|
category.UserID,
|
|
|
|
category.Title,
|
|
|
|
).Scan(&category.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to create category: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UpdateCategory] categoryID=%d", category.ID))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("Unable to update category: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-01-02 22:15:08 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:RemoveCategory] userID=%d, categoryID=%d", userID, categoryID))
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
result, err := s.db.Exec("DELETE FROM categories WHERE id = $1 AND user_id = $2", categoryID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to remove this category: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to remove this category: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
return errors.New("no category has been removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|