category.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "miniflux.app/http/request"
  9. "miniflux.app/http/response/json"
  10. )
  11. // CreateCategory is the API handler to create a new category.
  12. func (c *Controller) CreateCategory(w http.ResponseWriter, r *http.Request) {
  13. category, err := decodeCategoryPayload(r.Body)
  14. if err != nil {
  15. json.BadRequest(w, err)
  16. return
  17. }
  18. userID := request.UserID(r)
  19. category.UserID = userID
  20. if err := category.ValidateCategoryCreation(); err != nil {
  21. json.BadRequest(w, err)
  22. return
  23. }
  24. if c, err := c.store.CategoryByTitle(userID, category.Title); err != nil || c != nil {
  25. json.BadRequest(w, errors.New("This category already exists"))
  26. return
  27. }
  28. err = c.store.CreateCategory(category)
  29. if err != nil {
  30. json.ServerError(w, err)
  31. return
  32. }
  33. json.Created(w, category)
  34. }
  35. // UpdateCategory is the API handler to update a category.
  36. func (c *Controller) UpdateCategory(w http.ResponseWriter, r *http.Request) {
  37. categoryID, err := request.IntParam(r, "categoryID")
  38. if err != nil {
  39. json.BadRequest(w, err)
  40. return
  41. }
  42. category, err := decodeCategoryPayload(r.Body)
  43. if err != nil {
  44. json.BadRequest(w, err)
  45. return
  46. }
  47. category.UserID = request.UserID(r)
  48. category.ID = categoryID
  49. if err := category.ValidateCategoryModification(); err != nil {
  50. json.BadRequest(w, err)
  51. return
  52. }
  53. err = c.store.UpdateCategory(category)
  54. if err != nil {
  55. json.ServerError(w, err)
  56. return
  57. }
  58. json.Created(w, category)
  59. }
  60. // GetCategories is the API handler to get a list of categories for a given user.
  61. func (c *Controller) GetCategories(w http.ResponseWriter, r *http.Request) {
  62. categories, err := c.store.Categories(request.UserID(r))
  63. if err != nil {
  64. json.ServerError(w, err)
  65. return
  66. }
  67. json.OK(w, r, categories)
  68. }
  69. // RemoveCategory is the API handler to remove a category.
  70. func (c *Controller) RemoveCategory(w http.ResponseWriter, r *http.Request) {
  71. userID := request.UserID(r)
  72. categoryID, err := request.IntParam(r, "categoryID")
  73. if err != nil {
  74. json.BadRequest(w, err)
  75. return
  76. }
  77. if !c.store.CategoryExists(userID, categoryID) {
  78. json.NotFound(w, errors.New("Category not found"))
  79. return
  80. }
  81. if err := c.store.RemoveCategory(userID, categoryID); err != nil {
  82. json.ServerError(w, err)
  83. return
  84. }
  85. json.NoContent(w)
  86. }