aboutsummaryrefslogtreecommitdiffhomepage
path: root/storage/category.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage/category.go')
-rw-r--r--storage/category.go96
1 files changed, 55 insertions, 41 deletions
diff --git a/storage/category.go b/storage/category.go
index 72ba24b..07206c0 100644
--- a/storage/category.go
+++ b/storage/category.go
@@ -14,18 +14,18 @@ import (
// AnotherCategoryExists checks if another category exists with the same title.
func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string) bool {
- var result int
- query := `SELECT count(*) as c FROM categories WHERE user_id=$1 AND id != $2 AND title=$3`
+ var result bool
+ query := `SELECT true FROM categories WHERE user_id=$1 AND id != $2 AND title=$3`
s.db.QueryRow(query, userID, categoryID, title).Scan(&result)
- return result >= 1
+ return result
}
// CategoryExists checks if the given category exists into the database.
func (s *Storage) CategoryExists(userID, categoryID int64) bool {
- var result int
- query := `SELECT count(*) as c FROM categories WHERE user_id=$1 AND id=$2`
+ 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 >= 1
+ return result
}
// Category returns a category from the database.
@@ -34,28 +34,32 @@ func (s *Storage) Category(userID, categoryID int64) (*model.Category, error) {
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 {
+
+ switch {
+ case err == sql.ErrNoRows:
return nil, nil
- } else if err != nil {
- return nil, fmt.Errorf("unable to fetch category: %v", err)
+ case err != nil:
+ return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
+ default:
+ return &category, nil
}
-
- return &category, nil
}
// FirstCategory returns the first category for the given user.
func (s *Storage) FirstCategory(userID int64) (*model.Category, error) {
- var category model.Category
-
query := `SELECT id, user_id, title 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)
- if err == sql.ErrNoRows {
+
+ switch {
+ case err == sql.ErrNoRows:
return nil, nil
- } else if err != nil {
- return nil, fmt.Errorf("unable to fetch category: %v", err)
+ case err != nil:
+ return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
+ default:
+ return &category, nil
}
-
- return &category, nil
}
// CategoryByTitle finds a category by the title.
@@ -64,13 +68,15 @@ func (s *Storage) CategoryByTitle(userID int64, title string) (*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 {
+
+ switch {
+ case err == sql.ErrNoRows:
return nil, nil
- } else if err != nil {
- return nil, fmt.Errorf("Unable to fetch category: %v", err)
+ case err != nil:
+ return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
+ default:
+ return &category, nil
}
-
- return &category, nil
}
// Categories returns all categories that belongs to the given user.
@@ -78,7 +84,7 @@ 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`
rows, err := s.db.Query(query, userID)
if err != nil {
- return nil, fmt.Errorf("Unable to fetch categories: %v", err)
+ return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
}
defer rows.Close()
@@ -86,7 +92,7 @@ func (s *Storage) Categories(userID int64) (model.Categories, error) {
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)
+ return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
}
categories = append(categories, &category)
@@ -97,15 +103,21 @@ func (s *Storage) Categories(userID int64) (model.Categories, error) {
// CategoriesWithFeedCount returns all categories with the number of feeds.
func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error) {
- 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`
+ 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
+ `
rows, err := s.db.Query(query, userID)
if err != nil {
- return nil, fmt.Errorf("Unable to fetch categories: %v", err)
+ return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
}
defer rows.Close()
@@ -113,7 +125,7 @@ func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error
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)
+ return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
}
categories = append(categories, &category)
@@ -126,10 +138,11 @@ func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error
func (s *Storage) CreateCategory(category *model.Category) error {
query := `
INSERT INTO categories
- (user_id, title)
+ (user_id, title)
VALUES
- ($1, $2)
- RETURNING id
+ ($1, $2)
+ RETURNING
+ id
`
err := s.db.QueryRow(
query,
@@ -138,7 +151,7 @@ func (s *Storage) CreateCategory(category *model.Category) error {
).Scan(&category.ID)
if err != nil {
- return fmt.Errorf("Unable to create category: %v", err)
+ return fmt.Errorf(`store: unable to create category: %v`, err)
}
return nil
@@ -155,7 +168,7 @@ func (s *Storage) UpdateCategory(category *model.Category) error {
)
if err != nil {
- return fmt.Errorf("Unable to update category: %v", err)
+ return fmt.Errorf(`store: unable to update category: %v`, err)
}
return nil
@@ -163,18 +176,19 @@ func (s *Storage) UpdateCategory(category *model.Category) error {
// RemoveCategory deletes a category.
func (s *Storage) RemoveCategory(userID, categoryID int64) error {
- result, err := s.db.Exec("DELETE FROM categories WHERE id = $1 AND user_id = $2", categoryID, userID)
+ 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("Unable to remove this category: %v", err)
+ return fmt.Errorf(`store: unable to remove this category: %v`, err)
}
count, err := result.RowsAffected()
if err != nil {
- return fmt.Errorf("Unable to remove this category: %v", err)
+ return fmt.Errorf(`store: unable to remove this category: %v`, err)
}
if count == 0 {
- return errors.New("no category has been removed")
+ return errors.New(`store: no category has been removed`)
}
return nil