category_update.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "net/http"
  6. "miniflux.app/v2/internal/http/request"
  7. "miniflux.app/v2/internal/http/response/html"
  8. "miniflux.app/v2/internal/http/route"
  9. "miniflux.app/v2/internal/model"
  10. "miniflux.app/v2/internal/ui/form"
  11. "miniflux.app/v2/internal/ui/session"
  12. "miniflux.app/v2/internal/ui/view"
  13. "miniflux.app/v2/internal/validator"
  14. )
  15. func (h *handler) updateCategory(w http.ResponseWriter, r *http.Request) {
  16. user, err := h.store.UserByID(request.UserID(r))
  17. if err != nil {
  18. html.ServerError(w, r, err)
  19. return
  20. }
  21. categoryID := request.RouteInt64Param(r, "categoryID")
  22. category, err := h.store.Category(request.UserID(r), categoryID)
  23. if err != nil {
  24. html.ServerError(w, r, err)
  25. return
  26. }
  27. if category == nil {
  28. html.NotFound(w, r)
  29. return
  30. }
  31. categoryForm := form.NewCategoryForm(r)
  32. sess := session.New(h.store, request.SessionID(r))
  33. view := view.New(h.tpl, r, sess)
  34. view.Set("form", categoryForm)
  35. view.Set("category", category)
  36. view.Set("menu", "categories")
  37. view.Set("user", user)
  38. view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
  39. view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
  40. categoryRequest := &model.CategoryModificationRequest{
  41. Title: model.SetOptionalField(categoryForm.Title),
  42. HideGlobally: model.SetOptionalField(categoryForm.HideGlobally),
  43. }
  44. if validationErr := validator.ValidateCategoryModification(h.store, user.ID, category.ID, categoryRequest); validationErr != nil {
  45. view.Set("errorMessage", validationErr.Translate(user.Language))
  46. html.OK(w, r, view.Render("create_category"))
  47. return
  48. }
  49. categoryRequest.Patch(category)
  50. if err := h.store.UpdateCategory(category); err != nil {
  51. html.ServerError(w, r, err)
  52. return
  53. }
  54. html.Redirect(w, r, route.Path(h.router, "categoryFeeds", "categoryID", categoryID))
  55. }