category.go 2.7 KB

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