subscription.go 989 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 form
  5. import (
  6. "net/http"
  7. "strconv"
  8. "github.com/miniflux/miniflux/errors"
  9. )
  10. // SubscriptionForm represents the subscription form.
  11. type SubscriptionForm struct {
  12. URL string
  13. CategoryID int64
  14. Crawler bool
  15. }
  16. // Validate makes sure the form values are valid.
  17. func (s *SubscriptionForm) Validate() error {
  18. if s.URL == "" || s.CategoryID == 0 {
  19. return errors.NewLocalizedError("The URL and the category are mandatory.")
  20. }
  21. return nil
  22. }
  23. // NewSubscriptionForm returns a new SubscriptionForm.
  24. func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
  25. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  26. if err != nil {
  27. categoryID = 0
  28. }
  29. return &SubscriptionForm{
  30. URL: r.FormValue("url"),
  31. Crawler: r.FormValue("crawler") == "1",
  32. CategoryID: int64(categoryID),
  33. }
  34. }