subscription.go 928 B

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