aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/ui/form
diff options
context:
space:
mode:
Diffstat (limited to 'server/ui/form')
-rw-r--r--server/ui/form/auth.go30
-rw-r--r--server/ui/form/category.go34
-rw-r--r--server/ui/form/feed.go53
-rw-r--r--server/ui/form/settings.go62
-rw-r--r--server/ui/form/subscription.go36
-rw-r--r--server/ui/form/user.go80
6 files changed, 295 insertions, 0 deletions
diff --git a/server/ui/form/auth.go b/server/ui/form/auth.go
new file mode 100644
index 0000000..3cfc217
--- /dev/null
+++ b/server/ui/form/auth.go
@@ -0,0 +1,30 @@
+// Copyright 2017 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package form
+
+import (
+ "errors"
+ "net/http"
+)
+
+type AuthForm struct {
+ Username string
+ Password string
+}
+
+func (a AuthForm) Validate() error {
+ if a.Username == "" || a.Password == "" {
+ return errors.New("All fields are mandatory.")
+ }
+
+ return nil
+}
+
+func NewAuthForm(r *http.Request) *AuthForm {
+ return &AuthForm{
+ Username: r.FormValue("username"),
+ Password: r.FormValue("password"),
+ }
+}
diff --git a/server/ui/form/category.go b/server/ui/form/category.go
new file mode 100644
index 0000000..510d1b4
--- /dev/null
+++ b/server/ui/form/category.go
@@ -0,0 +1,34 @@
+// Copyright 2017 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package form
+
+import (
+ "errors"
+ "github.com/miniflux/miniflux2/model"
+ "net/http"
+)
+
+// CategoryForm represents a feed form in the UI
+type CategoryForm struct {
+ Title string
+}
+
+func (c CategoryForm) Validate() error {
+ if c.Title == "" {
+ return errors.New("The title is mandatory.")
+ }
+ return nil
+}
+
+func (c CategoryForm) Merge(category *model.Category) *model.Category {
+ category.Title = c.Title
+ return category
+}
+
+func NewCategoryForm(r *http.Request) *CategoryForm {
+ return &CategoryForm{
+ Title: r.FormValue("title"),
+ }
+}
diff --git a/server/ui/form/feed.go b/server/ui/form/feed.go
new file mode 100644
index 0000000..e21e6ca
--- /dev/null
+++ b/server/ui/form/feed.go
@@ -0,0 +1,53 @@
+// Copyright 2017 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package form
+
+import (
+ "errors"
+ "github.com/miniflux/miniflux2/model"
+ "net/http"
+ "strconv"
+)
+
+// FeedForm represents a feed form in the UI
+type FeedForm struct {
+ FeedURL string
+ SiteURL string
+ Title string
+ CategoryID int64
+}
+
+// ValidateModification validates FeedForm fields
+func (f FeedForm) ValidateModification() error {
+ if f.FeedURL == "" || f.SiteURL == "" || f.Title == "" || f.CategoryID == 0 {
+ return errors.New("All fields are mandatory.")
+ }
+ return nil
+}
+
+func (f FeedForm) Merge(feed *model.Feed) *model.Feed {
+ feed.Category.ID = f.CategoryID
+ feed.Title = f.Title
+ feed.SiteURL = f.SiteURL
+ feed.FeedURL = f.FeedURL
+ feed.ParsingErrorCount = 0
+ feed.ParsingErrorMsg = ""
+ return feed
+}
+
+// NewFeedForm parses the HTTP request and returns a FeedForm
+func NewFeedForm(r *http.Request) *FeedForm {
+ categoryID, err := strconv.Atoi(r.FormValue("category_id"))
+ if err != nil {
+ categoryID = 0
+ }
+
+ return &FeedForm{
+ FeedURL: r.FormValue("feed_url"),
+ SiteURL: r.FormValue("site_url"),
+ Title: r.FormValue("title"),
+ CategoryID: int64(categoryID),
+ }
+}
diff --git a/server/ui/form/settings.go b/server/ui/form/settings.go
new file mode 100644
index 0000000..1e40b97
--- /dev/null
+++ b/server/ui/form/settings.go
@@ -0,0 +1,62 @@
+// Copyright 2017 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package form
+
+import (
+ "errors"
+ "github.com/miniflux/miniflux2/model"
+ "net/http"
+)
+
+type SettingsForm struct {
+ Username string
+ Password string
+ Confirmation string
+ Theme string
+ Language string
+ Timezone string
+}
+
+func (s *SettingsForm) Merge(user *model.User) *model.User {
+ user.Username = s.Username
+ user.Theme = s.Theme
+ user.Language = s.Language
+ user.Timezone = s.Timezone
+
+ if s.Password != "" {
+ user.Password = s.Password
+ }
+
+ return user
+}
+
+func (s *SettingsForm) Validate() error {
+ if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" {
+ return errors.New("The username, theme, language and timezone fields are mandatory.")
+ }
+
+ if s.Password != "" {
+ if s.Password != s.Confirmation {
+ return errors.New("Passwords are not the same.")
+ }
+
+ if len(s.Password) < 6 {
+ return errors.New("You must use at least 6 characters")
+ }
+ }
+
+ return nil
+}
+
+func NewSettingsForm(r *http.Request) *SettingsForm {
+ return &SettingsForm{
+ Username: r.FormValue("username"),
+ Password: r.FormValue("password"),
+ Confirmation: r.FormValue("confirmation"),
+ Theme: r.FormValue("theme"),
+ Language: r.FormValue("language"),
+ Timezone: r.FormValue("timezone"),
+ }
+}
diff --git a/server/ui/form/subscription.go b/server/ui/form/subscription.go
new file mode 100644
index 0000000..6696b22
--- /dev/null
+++ b/server/ui/form/subscription.go
@@ -0,0 +1,36 @@
+// Copyright 2017 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package form
+
+import (
+ "errors"
+ "net/http"
+ "strconv"
+)
+
+type SubscriptionForm struct {
+ URL string
+ CategoryID int64
+}
+
+func (s *SubscriptionForm) Validate() error {
+ if s.URL == "" || s.CategoryID == 0 {
+ return errors.New("The URL and the category are mandatory.")
+ }
+
+ return nil
+}
+
+func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
+ categoryID, err := strconv.Atoi(r.FormValue("category_id"))
+ if err != nil {
+ categoryID = 0
+ }
+
+ return &SubscriptionForm{
+ URL: r.FormValue("url"),
+ CategoryID: int64(categoryID),
+ }
+}
diff --git a/server/ui/form/user.go b/server/ui/form/user.go
new file mode 100644
index 0000000..1197b48
--- /dev/null
+++ b/server/ui/form/user.go
@@ -0,0 +1,80 @@
+// Copyright 2017 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package form
+
+import (
+ "errors"
+ "github.com/miniflux/miniflux2/model"
+ "net/http"
+)
+
+type UserForm struct {
+ Username string
+ Password string
+ Confirmation string
+ IsAdmin bool
+}
+
+func (u UserForm) ValidateCreation() error {
+ if u.Username == "" || u.Password == "" || u.Confirmation == "" {
+ return errors.New("All fields are mandatory.")
+ }
+
+ if u.Password != u.Confirmation {
+ return errors.New("Passwords are not the same.")
+ }
+
+ if len(u.Password) < 6 {
+ return errors.New("You must use at least 6 characters.")
+ }
+
+ return nil
+}
+
+func (u UserForm) ValidateModification() error {
+ if u.Username == "" {
+ return errors.New("The username is mandatory.")
+ }
+
+ if u.Password != "" {
+ if u.Password != u.Confirmation {
+ return errors.New("Passwords are not the same.")
+ }
+
+ if len(u.Password) < 6 {
+ return errors.New("You must use at least 6 characters.")
+ }
+ }
+
+ return nil
+}
+
+func (u UserForm) ToUser() *model.User {
+ return &model.User{
+ Username: u.Username,
+ Password: u.Password,
+ IsAdmin: u.IsAdmin,
+ }
+}
+
+func (u UserForm) Merge(user *model.User) *model.User {
+ user.Username = u.Username
+ user.IsAdmin = u.IsAdmin
+
+ if u.Password != "" {
+ user.Password = u.Password
+ }
+
+ return user
+}
+
+func NewUserForm(r *http.Request) *UserForm {
+ return &UserForm{
+ Username: r.FormValue("username"),
+ Password: r.FormValue("password"),
+ Confirmation: r.FormValue("confirmation"),
+ IsAdmin: r.FormValue("is_admin") == "1",
+ }
+}