category.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 // import "miniflux.app/api"
  5. import (
  6. "errors"
  7. "net/http"
  8. "time"
  9. "miniflux.app/http/request"
  10. "miniflux.app/http/response/json"
  11. )
  12. func (h *handler) createCategory(w http.ResponseWriter, r *http.Request) {
  13. category, err := decodeCategoryPayload(r.Body)
  14. if err != nil {
  15. json.BadRequest(w, r, err)
  16. return
  17. }
  18. userID := request.UserID(r)
  19. category.UserID = userID
  20. if err := category.ValidateCategoryCreation(); err != nil {
  21. json.BadRequest(w, r, err)
  22. return
  23. }
  24. if c, err := h.store.CategoryByTitle(userID, category.Title); err != nil || c != nil {
  25. json.BadRequest(w, r, errors.New("This category already exists"))
  26. return
  27. }
  28. if err := h.store.CreateCategory(category); err != nil {
  29. json.ServerError(w, r, err)
  30. return
  31. }
  32. json.Created(w, r, category)
  33. }
  34. func (h *handler) updateCategory(w http.ResponseWriter, r *http.Request) {
  35. categoryID := request.RouteInt64Param(r, "categoryID")
  36. category, err := decodeCategoryPayload(r.Body)
  37. if err != nil {
  38. json.BadRequest(w, r, err)
  39. return
  40. }
  41. category.UserID = request.UserID(r)
  42. category.ID = categoryID
  43. if err := category.ValidateCategoryModification(); err != nil {
  44. json.BadRequest(w, r, err)
  45. return
  46. }
  47. err = h.store.UpdateCategory(category)
  48. if err != nil {
  49. json.ServerError(w, r, err)
  50. return
  51. }
  52. json.Created(w, r, category)
  53. }
  54. func (h *handler) markCategoryAsRead(w http.ResponseWriter, r *http.Request) {
  55. userID := request.UserID(r)
  56. categoryID := request.RouteInt64Param(r, "categoryID")
  57. category, err := h.store.Category(userID, categoryID)
  58. if err != nil {
  59. json.ServerError(w, r, err)
  60. return
  61. }
  62. if category == nil {
  63. json.NotFound(w, r)
  64. return
  65. }
  66. if err = h.store.MarkCategoryAsRead(userID, categoryID, time.Now()); err != nil {
  67. json.ServerError(w, r, err)
  68. return
  69. }
  70. json.NoContent(w, r)
  71. }
  72. func (h *handler) getCategories(w http.ResponseWriter, r *http.Request) {
  73. categories, err := h.store.Categories(request.UserID(r))
  74. if err != nil {
  75. json.ServerError(w, r, err)
  76. return
  77. }
  78. json.OK(w, r, categories)
  79. }
  80. func (h *handler) removeCategory(w http.ResponseWriter, r *http.Request) {
  81. userID := request.UserID(r)
  82. categoryID := request.RouteInt64Param(r, "categoryID")
  83. if !h.store.CategoryExists(userID, categoryID) {
  84. json.NotFound(w, r)
  85. return
  86. }
  87. if err := h.store.RemoveCategory(userID, categoryID); err != nil {
  88. json.ServerError(w, r, err)
  89. return
  90. }
  91. json.NoContent(w, r)
  92. }