category_remove.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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"
  10. "github.com/miniflux/miniflux/http/response/html"
  11. "github.com/miniflux/miniflux/http/route"
  12. )
  13. // RemoveCategory deletes a category from the database.
  14. func (c *Controller) RemoveCategory(w http.ResponseWriter, r *http.Request) {
  15. ctx := context.New(r)
  16. user, err := c.store.UserByID(ctx.UserID())
  17. if err != nil {
  18. html.ServerError(w, err)
  19. return
  20. }
  21. categoryID, err := request.IntParam(r, "categoryID")
  22. if err != nil {
  23. html.BadRequest(w, err)
  24. return
  25. }
  26. category, err := c.store.Category(ctx.UserID(), categoryID)
  27. if err != nil {
  28. html.ServerError(w, err)
  29. return
  30. }
  31. if category == nil {
  32. html.NotFound(w)
  33. return
  34. }
  35. if err := c.store.RemoveCategory(user.ID, category.ID); err != nil {
  36. html.ServerError(w, err)
  37. return
  38. }
  39. response.Redirect(w, r, route.Path(c.router, "categories"))
  40. }