subscription.go 724 B

123456789101112131415161718192021222324252627282930313233343536
  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. "errors"
  7. "net/http"
  8. "strconv"
  9. )
  10. type SubscriptionForm struct {
  11. URL string
  12. CategoryID int64
  13. }
  14. func (s *SubscriptionForm) Validate() error {
  15. if s.URL == "" || s.CategoryID == 0 {
  16. return errors.New("The URL and the category are mandatory.")
  17. }
  18. return nil
  19. }
  20. func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
  21. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  22. if err != nil {
  23. categoryID = 0
  24. }
  25. return &SubscriptionForm{
  26. URL: r.FormValue("url"),
  27. CategoryID: int64(categoryID),
  28. }
  29. }