feed.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. "github.com/miniflux/miniflux2/model"
  10. )
  11. // FeedForm represents a feed form in the UI
  12. type FeedForm struct {
  13. FeedURL string
  14. SiteURL string
  15. Title string
  16. CategoryID int64
  17. }
  18. // ValidateModification validates FeedForm fields
  19. func (f FeedForm) ValidateModification() error {
  20. if f.FeedURL == "" || f.SiteURL == "" || f.Title == "" || f.CategoryID == 0 {
  21. return errors.NewLocalizedError("All fields are mandatory.")
  22. }
  23. return nil
  24. }
  25. // Merge updates the fields of the given feed.
  26. func (f FeedForm) Merge(feed *model.Feed) *model.Feed {
  27. feed.Category.ID = f.CategoryID
  28. feed.Title = f.Title
  29. feed.SiteURL = f.SiteURL
  30. feed.FeedURL = f.FeedURL
  31. feed.ParsingErrorCount = 0
  32. feed.ParsingErrorMsg = ""
  33. return feed
  34. }
  35. // NewFeedForm parses the HTTP request and returns a FeedForm
  36. func NewFeedForm(r *http.Request) *FeedForm {
  37. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  38. if err != nil {
  39. categoryID = 0
  40. }
  41. return &FeedForm{
  42. FeedURL: r.FormValue("feed_url"),
  43. SiteURL: r.FormValue("site_url"),
  44. Title: r.FormValue("title"),
  45. CategoryID: int64(categoryID),
  46. }
  47. }