category.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package validator // import "miniflux.app/v2/internal/validator"
  4. import (
  5. "miniflux.app/v2/internal/locale"
  6. "miniflux.app/v2/internal/model"
  7. "miniflux.app/v2/internal/storage"
  8. )
  9. // ValidateCategoryCreation validates category creation.
  10. func ValidateCategoryCreation(store *storage.Storage, userID int64, request *model.CategoryCreationRequest) *locale.LocalizedError {
  11. if request.Title == "" {
  12. return locale.NewLocalizedError("error.title_required")
  13. }
  14. if store.CategoryTitleExists(userID, request.Title) {
  15. return locale.NewLocalizedError("error.category_already_exists")
  16. }
  17. return nil
  18. }
  19. // ValidateCategoryModification validates category modification.
  20. func ValidateCategoryModification(store *storage.Storage, userID, categoryID int64, request *model.CategoryModificationRequest) *locale.LocalizedError {
  21. if request.Title != nil {
  22. if *request.Title == "" {
  23. return locale.NewLocalizedError("error.title_required")
  24. }
  25. if store.AnotherCategoryExists(userID, categoryID, *request.Title) {
  26. return locale.NewLocalizedError("error.category_already_exists")
  27. }
  28. }
  29. return nil
  30. }