entry.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 api
  5. import (
  6. "errors"
  7. "net/http"
  8. "github.com/miniflux/miniflux/http/context"
  9. "github.com/miniflux/miniflux/http/request"
  10. "github.com/miniflux/miniflux/http/response/json"
  11. "github.com/miniflux/miniflux/model"
  12. )
  13. // GetFeedEntry is the API handler to get a single feed entry.
  14. func (c *Controller) GetFeedEntry(w http.ResponseWriter, r *http.Request) {
  15. feedID, err := request.IntParam(r, "feedID")
  16. if err != nil {
  17. json.BadRequest(w, err)
  18. return
  19. }
  20. entryID, err := request.IntParam(r, "entryID")
  21. if err != nil {
  22. json.BadRequest(w, err)
  23. return
  24. }
  25. ctx := context.New(r)
  26. userID := ctx.UserID()
  27. builder := c.store.NewEntryQueryBuilder(userID)
  28. builder.WithFeedID(feedID)
  29. builder.WithEntryID(entryID)
  30. entry, err := builder.GetEntry()
  31. if err != nil {
  32. json.ServerError(w, errors.New("Unable to fetch this entry from the database"))
  33. return
  34. }
  35. if entry == nil {
  36. json.NotFound(w, errors.New("Entry not found"))
  37. return
  38. }
  39. json.OK(w, entry)
  40. }
  41. // GetEntry is the API handler to get a single entry.
  42. func (c *Controller) GetEntry(w http.ResponseWriter, r *http.Request) {
  43. entryID, err := request.IntParam(r, "entryID")
  44. if err != nil {
  45. json.BadRequest(w, err)
  46. return
  47. }
  48. builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
  49. builder.WithEntryID(entryID)
  50. entry, err := builder.GetEntry()
  51. if err != nil {
  52. json.ServerError(w, errors.New("Unable to fetch this entry from the database"))
  53. return
  54. }
  55. if entry == nil {
  56. json.NotFound(w, errors.New("Entry not found"))
  57. return
  58. }
  59. json.OK(w, entry)
  60. }
  61. // GetFeedEntries is the API handler to get all feed entries.
  62. func (c *Controller) GetFeedEntries(w http.ResponseWriter, r *http.Request) {
  63. feedID, err := request.IntParam(r, "feedID")
  64. if err != nil {
  65. json.BadRequest(w, err)
  66. return
  67. }
  68. status := request.QueryParam(r, "status", "")
  69. if status != "" {
  70. if err := model.ValidateEntryStatus(status); err != nil {
  71. json.BadRequest(w, err)
  72. return
  73. }
  74. }
  75. order := request.QueryParam(r, "order", model.DefaultSortingOrder)
  76. if err := model.ValidateEntryOrder(order); err != nil {
  77. json.BadRequest(w, err)
  78. return
  79. }
  80. direction := request.QueryParam(r, "direction", model.DefaultSortingDirection)
  81. if err := model.ValidateDirection(direction); err != nil {
  82. json.BadRequest(w, err)
  83. return
  84. }
  85. limit := request.QueryIntParam(r, "limit", 100)
  86. offset := request.QueryIntParam(r, "offset", 0)
  87. if err := model.ValidateRange(offset, limit); err != nil {
  88. json.BadRequest(w, err)
  89. return
  90. }
  91. builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
  92. builder.WithFeedID(feedID)
  93. builder.WithStatus(status)
  94. builder.WithOrder(order)
  95. builder.WithDirection(direction)
  96. builder.WithOffset(offset)
  97. builder.WithLimit(limit)
  98. entries, err := builder.GetEntries()
  99. if err != nil {
  100. json.ServerError(w, errors.New("Unable to fetch the list of entries"))
  101. return
  102. }
  103. count, err := builder.CountEntries()
  104. if err != nil {
  105. json.ServerError(w, errors.New("Unable to count the number of entries"))
  106. return
  107. }
  108. json.OK(w, &entriesResponse{Total: count, Entries: entries})
  109. }
  110. // GetEntries is the API handler to fetch entries.
  111. func (c *Controller) GetEntries(w http.ResponseWriter, r *http.Request) {
  112. status := request.QueryParam(r, "status", "")
  113. if status != "" {
  114. if err := model.ValidateEntryStatus(status); err != nil {
  115. json.BadRequest(w, err)
  116. return
  117. }
  118. }
  119. order := request.QueryParam(r, "order", model.DefaultSortingOrder)
  120. if err := model.ValidateEntryOrder(order); err != nil {
  121. json.BadRequest(w, err)
  122. return
  123. }
  124. direction := request.QueryParam(r, "direction", model.DefaultSortingDirection)
  125. if err := model.ValidateDirection(direction); err != nil {
  126. json.BadRequest(w, err)
  127. return
  128. }
  129. limit := request.QueryIntParam(r, "limit", 100)
  130. offset := request.QueryIntParam(r, "offset", 0)
  131. if err := model.ValidateRange(offset, limit); err != nil {
  132. json.BadRequest(w, err)
  133. return
  134. }
  135. builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
  136. builder.WithStatus(status)
  137. builder.WithOrder(order)
  138. builder.WithDirection(direction)
  139. builder.WithOffset(offset)
  140. builder.WithLimit(limit)
  141. entries, err := builder.GetEntries()
  142. if err != nil {
  143. json.ServerError(w, errors.New("Unable to fetch the list of entries"))
  144. return
  145. }
  146. count, err := builder.CountEntries()
  147. if err != nil {
  148. json.ServerError(w, errors.New("Unable to count the number of entries"))
  149. return
  150. }
  151. json.OK(w, &entriesResponse{Total: count, Entries: entries})
  152. }
  153. // SetEntryStatus is the API handler to change the status of entries.
  154. func (c *Controller) SetEntryStatus(w http.ResponseWriter, r *http.Request) {
  155. entryIDs, status, err := decodeEntryStatusPayload(r.Body)
  156. if err != nil {
  157. json.BadRequest(w, errors.New("Invalid JSON payload"))
  158. return
  159. }
  160. if err := model.ValidateEntryStatus(status); err != nil {
  161. json.BadRequest(w, err)
  162. return
  163. }
  164. if err := c.store.SetEntriesStatus(context.New(r).UserID(), entryIDs, status); err != nil {
  165. json.ServerError(w, errors.New("Unable to change entries status"))
  166. return
  167. }
  168. json.NoContent(w)
  169. }
  170. // ToggleBookmark is the API handler to toggle bookmark status.
  171. func (c *Controller) ToggleBookmark(w http.ResponseWriter, r *http.Request) {
  172. entryID, err := request.IntParam(r, "entryID")
  173. if err != nil {
  174. json.BadRequest(w, err)
  175. return
  176. }
  177. if err := c.store.ToggleBookmark(context.New(r).UserID(), entryID); err != nil {
  178. json.ServerError(w, errors.New("Unable to toggle bookmark value"))
  179. return
  180. }
  181. json.NoContent(w)
  182. }