category.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 lower(title)=lower($3) LIMIT 1`
  15. s.db.QueryRow(query, userID, categoryID, title).Scan(&result)
  16. return result
  17. }
  18. // CategoryTitleExists checks if the given category exists into the database.
  19. func (s *Storage) CategoryTitleExists(userID int64, title string) bool {
  20. var result bool
  21. query := `SELECT true FROM categories WHERE user_id=$1 AND lower(title)=lower($2) LIMIT 1`
  22. s.db.QueryRow(query, userID, title).Scan(&result)
  23. return result
  24. }
  25. // CategoryIDExists checks if the given category exists into the database.
  26. func (s *Storage) CategoryIDExists(userID, categoryID int64) bool {
  27. var result bool
  28. query := `SELECT true FROM categories WHERE user_id=$1 AND id=$2`
  29. s.db.QueryRow(query, userID, categoryID).Scan(&result)
  30. return result
  31. }
  32. // Category returns a category from the database.
  33. func (s *Storage) Category(userID, categoryID int64) (*model.Category, error) {
  34. var category model.Category
  35. query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 AND id=$2`
  36. err := s.db.QueryRow(query, userID, categoryID).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
  37. switch {
  38. case err == sql.ErrNoRows:
  39. return nil, nil
  40. case err != nil:
  41. return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
  42. default:
  43. return &category, nil
  44. }
  45. }
  46. // FirstCategory returns the first category for the given user.
  47. func (s *Storage) FirstCategory(userID int64) (*model.Category, error) {
  48. query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 ORDER BY title ASC LIMIT 1`
  49. var category model.Category
  50. err := s.db.QueryRow(query, userID).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
  51. switch {
  52. case err == sql.ErrNoRows:
  53. return nil, nil
  54. case err != nil:
  55. return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
  56. default:
  57. return &category, nil
  58. }
  59. }
  60. // CategoryByTitle finds a category by the title.
  61. func (s *Storage) CategoryByTitle(userID int64, title string) (*model.Category, error) {
  62. var category model.Category
  63. query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 AND title=$2`
  64. err := s.db.QueryRow(query, userID, title).Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally)
  65. switch {
  66. case err == sql.ErrNoRows:
  67. return nil, nil
  68. case err != nil:
  69. return nil, fmt.Errorf(`store: unable to fetch category: %v`, err)
  70. default:
  71. return &category, nil
  72. }
  73. }
  74. // Categories returns all categories that belongs to the given user.
  75. func (s *Storage) Categories(userID int64) (model.Categories, error) {
  76. query := `SELECT id, user_id, title, hide_globally FROM categories WHERE user_id=$1 ORDER BY title ASC`
  77. rows, err := s.db.Query(query, userID)
  78. if err != nil {
  79. return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
  80. }
  81. defer rows.Close()
  82. categories := make(model.Categories, 0)
  83. for rows.Next() {
  84. var category model.Category
  85. if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally); err != nil {
  86. return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
  87. }
  88. categories = append(categories, &category)
  89. }
  90. return categories, nil
  91. }
  92. // CategoriesWithFeedCount returns all categories with the number of feeds.
  93. func (s *Storage) CategoriesWithFeedCount(userID int64) (model.Categories, error) {
  94. query := `
  95. SELECT
  96. c.id,
  97. c.user_id,
  98. c.title,
  99. c.hide_globally,
  100. (SELECT count(*) FROM feeds WHERE feeds.category_id=c.id) AS count,
  101. (SELECT count(*)
  102. FROM feeds
  103. JOIN entries ON (feeds.id = entries.feed_id)
  104. WHERE feeds.category_id = c.id AND entries.status = 'unread')
  105. FROM categories c
  106. WHERE
  107. user_id=$1
  108. ORDER BY c.title ASC
  109. `
  110. rows, err := s.db.Query(query, userID)
  111. if err != nil {
  112. return nil, fmt.Errorf(`store: unable to fetch categories: %v`, err)
  113. }
  114. defer rows.Close()
  115. categories := make(model.Categories, 0)
  116. for rows.Next() {
  117. var category model.Category
  118. if err := rows.Scan(&category.ID, &category.UserID, &category.Title, &category.HideGlobally, &category.FeedCount, &category.TotalUnread); err != nil {
  119. return nil, fmt.Errorf(`store: unable to fetch category row: %v`, err)
  120. }
  121. categories = append(categories, &category)
  122. }
  123. return categories, nil
  124. }
  125. // CreateCategory creates a new category.
  126. func (s *Storage) CreateCategory(userID int64, request *model.CategoryRequest) (*model.Category, error) {
  127. var category model.Category
  128. query := `
  129. INSERT INTO categories
  130. (user_id, title)
  131. VALUES
  132. ($1, $2)
  133. RETURNING
  134. id,
  135. user_id,
  136. title
  137. `
  138. err := s.db.QueryRow(
  139. query,
  140. userID,
  141. request.Title,
  142. ).Scan(
  143. &category.ID,
  144. &category.UserID,
  145. &category.Title,
  146. )
  147. if err != nil {
  148. return nil, fmt.Errorf(`store: unable to create category %q: %v`, request.Title, err)
  149. }
  150. return &category, nil
  151. }
  152. // UpdateCategory updates an existing category.
  153. func (s *Storage) UpdateCategory(category *model.Category) error {
  154. query := `UPDATE categories SET title=$1, hide_globally = $2 WHERE id=$3 AND user_id=$4`
  155. _, err := s.db.Exec(
  156. query,
  157. category.Title,
  158. category.HideGlobally,
  159. category.ID,
  160. category.UserID,
  161. )
  162. if err != nil {
  163. return fmt.Errorf(`store: unable to update category: %v`, err)
  164. }
  165. return nil
  166. }
  167. // RemoveCategory deletes a category.
  168. func (s *Storage) RemoveCategory(userID, categoryID int64) error {
  169. query := `DELETE FROM categories WHERE id = $1 AND user_id = $2`
  170. result, err := s.db.Exec(query, categoryID, userID)
  171. if err != nil {
  172. return fmt.Errorf(`store: unable to remove this category: %v`, err)
  173. }
  174. count, err := result.RowsAffected()
  175. if err != nil {
  176. return fmt.Errorf(`store: unable to remove this category: %v`, err)
  177. }
  178. if count == 0 {
  179. return errors.New(`store: no category has been removed`)
  180. }
  181. return nil
  182. }