category.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package storage // import "miniflux.app/storage"
  5. import (
  6. "database/sql"
  7. "errors"
  8. "fmt"
  9. "miniflux.app/model"
  10. )
  11. // AnotherCategoryExists checks if another category exists with the same title.
  12. func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string) bool {
  13. var result bool
  14. query := `SELECT true FROM categories WHERE user_id=$1 AND id != $2 AND title=$3`
  15. s.db.QueryRow(query, userID, categoryID, title).Scan(&result)
  16. return result
  17. }
  18. // CategoryExists checks if the given category exists into the database.
  19. func (s *Storage) CategoryExists(userID, categoryID int64) bool {
  20. var result bool
  21. query := `SELECT true FROM categories WHERE user_id=$1 AND id=$2`
  22. s.db.QueryRow(query, userID, categoryID).Scan(&result)
  23. return result
  24. }
  25. // Category returns a category from the database.
  26. func (s *Storage) Category(userID, categoryID int64) (*model.Category, error) {
  27. var category model.Category
  28. query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 AND id=$2`
  29. err := s.db.QueryRow(query, userID, categoryID).Scan(&category.ID, &category.UserID, &category.Title)
  30. switch {
  31. case err == sql.ErrNoRows:
  32. return nil, nil
  33. case err != nil:
  34. return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
  35. default:
  36. return &category, nil
  37. }
  38. }
  39. // FirstCategory returns the first category for the given user.
  40. func (s *Storage) FirstCategory(userID int64) (*model.Category, error) {
  41. query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 ORDER BY title ASC LIMIT 1`
  42. var category model.Category
  43. err := s.db.QueryRow(query, userID).Scan(&category.ID, &category.UserID, &category.Title)
  44. switch {
  45. case err == sql.ErrNoRows:
  46. return nil, nil
  47. case err != nil:
  48. return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
  49. default:
  50. return &category, nil
  51. }
  52. }
  53. // CategoryByTitle finds a category by the title.
  54. func (s *Storage) CategoryByTitle(userID int64, title string) (*model.Category, error) {
  55. var category model.Category
  56. query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 AND title=$2`
  57. err := s.db.QueryRow(query, userID, title).Scan(&category.ID, &category.UserID, &category.Title)
  58. switch {
  59. case err == sql.ErrNoRows:
  60. return nil, nil
  61. case err != nil:
  62. return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
  63. default:
  64. return &category, nil
  65. }
  66. }
  67. // Categories returns all categories that belongs to the given user.
  68. func (s *Storage) Categories(userID int64) (model.Categories, error) {
  69. query := `SELECT id, user_id, title FROM categories WHERE user_id=$1 ORDER BY title ASC`
  70. rows, err := s.db.Query(query, userID)
  71. if err != nil {
  72. return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
  73. }
  74. defer rows.Close()
  75. categories := make(model.Categories, 0)
  76. for rows.Next() {
  77. var category model.Category
  78. if err := rows.Scan(&category.ID, &category.UserID, &category.Title); err != nil {
  79. return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
  80. }
  81. categories = append(categories, &category)
  82. }
  83. return categories, nil
  84. }
  85. // CategoriesWithFeedCount returns all categories with the number of feeds.
  86. func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error) {
  87. query := `
  88. SELECT
  89. c.id,
  90. c.user_id,
  91. c.title,
  92. (SELECT count(*) FROM feeds WHERE feeds.category_id=c.id) AS count
  93. FROM categories c
  94. WHERE
  95. user_id=$1
  96. ORDER BY c.title ASC
  97. `
  98. rows, err := s.db.Query(query, userID)
  99. if err != nil {
  100. return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
  101. }
  102. defer rows.Close()
  103. categories := make(model.Categories, 0)
  104. for rows.Next() {
  105. var category model.Category
  106. if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.FeedCount); err != nil {
  107. return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
  108. }
  109. categories = append(categories, &category)
  110. }
  111. return categories, nil
  112. }
  113. // CreateCategory creates a new category.
  114. func (s *Storage) CreateCategory(category *model.Category) error {
  115. query := `
  116. INSERT INTO categories
  117. (user_id, title)
  118. VALUES
  119. ($1, $2)
  120. RETURNING
  121. id
  122. `
  123. err := s.db.QueryRow(
  124. query,
  125. category.UserID,
  126. category.Title,
  127. ).Scan(&category.ID)
  128. if err != nil {
  129. return fmt.Errorf(`store: unable to create category: %v`, err)
  130. }
  131. return nil
  132. }
  133. // UpdateCategory updates an existing category.
  134. func (s *Storage) UpdateCategory(category *model.Category) error {
  135. query := `UPDATE categories SET title=$1 WHERE id=$2 AND user_id=$3`
  136. _, err := s.db.Exec(
  137. query,
  138. category.Title,
  139. category.ID,
  140. category.UserID,
  141. )
  142. if err != nil {
  143. return fmt.Errorf(`store: unable to update category: %v`, err)
  144. }
  145. return nil
  146. }
  147. // RemoveCategory deletes a category.
  148. func (s *Storage) RemoveCategory(userID, categoryID int64) error {
  149. query := `DELETE FROM categories WHERE id = $1 AND user_id = $2`
  150. result, err := s.db.Exec(query, categoryID, userID)
  151. if err != nil {
  152. return fmt.Errorf(`store: unable to remove this category: %v`, err)
  153. }
  154. count, err := result.RowsAffected()
  155. if err != nil {
  156. return fmt.Errorf(`store: unable to remove this category: %v`, err)
  157. }
  158. if count == 0 {
  159. return errors.New(`store: no category has been removed`)
  160. }
  161. return nil
  162. }