category_save.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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) saveCategory(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. categoryForm := form.NewCategoryForm(r)
  22. sess := session.New(h.store, request.SessionID(r))
  23. view := view.New(h.tpl, r, sess)
  24. view.Set("form", categoryForm)
  25. view.Set("menu", "categories")
  26. view.Set("user", user)
  27. view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
  28. view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
  29. categoryCreationRequest := &model.CategoryCreationRequest{Title: categoryForm.Title}
  30. if validationErr := validator.ValidateCategoryCreation(h.store, user.ID, categoryCreationRequest); validationErr != nil {
  31. view.Set("errorMessage", validationErr.Translate(user.Language))
  32. html.OK(w, r, view.Render("create_category"))
  33. return
  34. }
  35. if _, err = h.store.CreateCategory(user.ID, categoryCreationRequest); err != nil {
  36. html.ServerError(w, r, err)
  37. return
  38. }
  39. html.Redirect(w, r, route.Path(h.router, "categories"))
  40. }