subscription.go 1.1 KB

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