entry.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "github.com/miniflux/miniflux2/model"
  8. "github.com/miniflux/miniflux2/server/api/payload"
  9. "github.com/miniflux/miniflux2/server/core"
  10. )
  11. // GetEntry is the API handler to get a single feed entry.
  12. func (c *Controller) GetEntry(ctx *core.Context, request *core.Request, response *core.Response) {
  13. userID := ctx.UserID()
  14. feedID, err := request.IntegerParam("feedID")
  15. if err != nil {
  16. response.JSON().BadRequest(err)
  17. return
  18. }
  19. entryID, err := request.IntegerParam("entryID")
  20. if err != nil {
  21. response.JSON().BadRequest(err)
  22. return
  23. }
  24. builder := c.store.GetEntryQueryBuilder(userID, ctx.UserTimezone())
  25. builder.WithFeedID(feedID)
  26. builder.WithEntryID(entryID)
  27. entry, err := builder.GetEntry()
  28. if err != nil {
  29. response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
  30. return
  31. }
  32. if entry == nil {
  33. response.JSON().NotFound(errors.New("Entry not found"))
  34. return
  35. }
  36. response.JSON().Standard(entry)
  37. }
  38. // GetFeedEntries is the API handler to get all feed entries.
  39. func (c *Controller) GetFeedEntries(ctx *core.Context, request *core.Request, response *core.Response) {
  40. userID := ctx.UserID()
  41. feedID, err := request.IntegerParam("feedID")
  42. if err != nil {
  43. response.JSON().BadRequest(err)
  44. return
  45. }
  46. status := request.QueryStringParam("status", "")
  47. if status != "" {
  48. if err := model.ValidateEntryStatus(status); err != nil {
  49. response.JSON().BadRequest(err)
  50. return
  51. }
  52. }
  53. order := request.QueryStringParam("order", "id")
  54. if err := model.ValidateEntryOrder(order); err != nil {
  55. response.JSON().BadRequest(err)
  56. return
  57. }
  58. direction := request.QueryStringParam("direction", "desc")
  59. if err := model.ValidateDirection(direction); err != nil {
  60. response.JSON().BadRequest(err)
  61. return
  62. }
  63. limit := request.QueryIntegerParam("limit", 100)
  64. offset := request.QueryIntegerParam("offset", 0)
  65. builder := c.store.GetEntryQueryBuilder(userID, ctx.UserTimezone())
  66. builder.WithFeedID(feedID)
  67. builder.WithStatus(status)
  68. builder.WithOrder(model.DefaultSortingOrder)
  69. builder.WithDirection(model.DefaultSortingDirection)
  70. builder.WithOffset(offset)
  71. builder.WithLimit(limit)
  72. entries, err := builder.GetEntries()
  73. if err != nil {
  74. response.JSON().ServerError(errors.New("Unable to fetch the list of entries"))
  75. return
  76. }
  77. count, err := builder.CountEntries()
  78. if err != nil {
  79. response.JSON().ServerError(errors.New("Unable to count the number of entries"))
  80. return
  81. }
  82. response.JSON().Standard(&payload.EntriesResponse{Total: count, Entries: entries})
  83. }
  84. // SetEntryStatus is the API handler to change the status of entries.
  85. func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, response *core.Response) {
  86. userID := ctx.UserID()
  87. entryIDs, status, err := payload.DecodeEntryStatusPayload(request.Body())
  88. if err != nil {
  89. response.JSON().BadRequest(errors.New("Invalid JSON payload"))
  90. return
  91. }
  92. if err := model.ValidateEntryStatus(status); err != nil {
  93. response.JSON().BadRequest(err)
  94. return
  95. }
  96. if err := c.store.SetEntriesStatus(userID, entryIDs, status); err != nil {
  97. response.JSON().ServerError(errors.New("Unable to change entries status"))
  98. return
  99. }
  100. response.JSON().NoContent()
  101. }