feed.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "github.com/miniflux/miniflux2/model"
  8. "net/http"
  9. "strconv"
  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.New("All fields are mandatory.")
  22. }
  23. return nil
  24. }
  25. func (f FeedForm) Merge(feed *model.Feed) *model.Feed {
  26. feed.Category.ID = f.CategoryID
  27. feed.Title = f.Title
  28. feed.SiteURL = f.SiteURL
  29. feed.FeedURL = f.FeedURL
  30. feed.ParsingErrorCount = 0
  31. feed.ParsingErrorMsg = ""
  32. return feed
  33. }
  34. // NewFeedForm parses the HTTP request and returns a FeedForm
  35. func NewFeedForm(r *http.Request) *FeedForm {
  36. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  37. if err != nil {
  38. categoryID = 0
  39. }
  40. return &FeedForm{
  41. FeedURL: r.FormValue("feed_url"),
  42. SiteURL: r.FormValue("site_url"),
  43. Title: r.FormValue("title"),
  44. CategoryID: int64(categoryID),
  45. }
  46. }