category_entries.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2018 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 ui
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/http/context"
  8. "github.com/miniflux/miniflux/http/request"
  9. "github.com/miniflux/miniflux/http/response/html"
  10. "github.com/miniflux/miniflux/http/route"
  11. "github.com/miniflux/miniflux/model"
  12. "github.com/miniflux/miniflux/ui/session"
  13. "github.com/miniflux/miniflux/ui/view"
  14. )
  15. // CategoryEntries shows all entries for the given category.
  16. func (c *Controller) CategoryEntries(w http.ResponseWriter, r *http.Request) {
  17. ctx := context.New(r)
  18. user, err := c.store.UserByID(ctx.UserID())
  19. if err != nil {
  20. html.ServerError(w, err)
  21. return
  22. }
  23. categoryID, err := request.IntParam(r, "categoryID")
  24. if err != nil {
  25. html.BadRequest(w, err)
  26. return
  27. }
  28. category, err := c.store.Category(ctx.UserID(), categoryID)
  29. if err != nil {
  30. html.ServerError(w, err)
  31. return
  32. }
  33. if category == nil {
  34. html.NotFound(w)
  35. return
  36. }
  37. offset := request.QueryIntParam(r, "offset", 0)
  38. builder := c.store.NewEntryQueryBuilder(user.ID)
  39. builder.WithCategoryID(category.ID)
  40. builder.WithOrder(model.DefaultSortingOrder)
  41. builder.WithDirection(user.EntryDirection)
  42. builder.WithoutStatus(model.EntryStatusRemoved)
  43. builder.WithOffset(offset)
  44. builder.WithLimit(nbItemsPerPage)
  45. entries, err := builder.GetEntries()
  46. if err != nil {
  47. html.ServerError(w, err)
  48. return
  49. }
  50. count, err := builder.CountEntries()
  51. if err != nil {
  52. html.ServerError(w, err)
  53. return
  54. }
  55. sess := session.New(c.store, ctx)
  56. view := view.New(c.tpl, ctx, sess)
  57. view.Set("category", category)
  58. view.Set("total", count)
  59. view.Set("entries", entries)
  60. view.Set("pagination", c.getPagination(route.Path(c.router, "categoryEntries", "categoryID", category.ID), count, offset))
  61. view.Set("menu", "categories")
  62. view.Set("user", user)
  63. view.Set("countUnread", c.store.CountUnreadEntries(user.ID))
  64. view.Set("hasSaveEntry", c.store.HasSaveEntry(user.ID))
  65. html.OK(w, r, view.Render("category_entries"))
  66. }