category.go 2.8 KB

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