From 18449ed0a5e827109984b0e301b3c42cde96f36a Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Mon, 25 Jul 2022 18:03:35 +0000 Subject: [PATCH] switched to bindings + validators for forms --- main.go | 75 ++++++++-------------------------------- models.go | 24 ++++++------- templates/editQuery.html | 60 ++++++++++---------------------- templates/newWatch.html | 3 +- templates/viewWatch.html | 60 ++++++++++++++------------------ util.go | 10 ++++++ 6 files changed, 81 insertions(+), 151 deletions(-) diff --git a/main.go b/main.go index f3d507f..5ec30a4 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,7 @@ func (web Web) createWatch(c *gin.Context) { errMap, err := bindAndValidateWatch(&watch, c) if err != nil { c.HTML(http.StatusBadRequest, "500", errMap) + return } web.db.Create(&watch) c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/view/%d", watch.ID)) @@ -74,83 +75,37 @@ func (web Web) createURL(c *gin.Context) { c.HTML(http.StatusInternalServerError, "500", errMap) return } - web.db.Create(url) + web.db.Create(&url) c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/view/%d", url.WatchID)) } func (web Web) createQuery(c *gin.Context) { - watch_id, err := strconv.ParseUint(c.PostForm("watch_id"), 10, 64) + watch_id, err := strconv.ParseUint(c.PostForm("w_id"), 10, 64) if err != nil { - c.Redirect(http.StatusSeeOther, "/watch/new") - return // TODO response + log.Print(err) + c.HTML(http.StatusInternalServerError, "500", gin.H{}) + return } - url_id, err := strconv.ParseUint(c.PostForm("url_id"), 10, 64) + var query Query + errMap, err := bindAndValidateQuery(&query, c) if err != nil { - c.Redirect(http.StatusSeeOther, "/watch/new") - return // TODO response - } - name := c.PostForm("name") - if name == "" { - c.Redirect(http.StatusSeeOther, "/watch/new") + c.HTML(http.StatusBadRequest, "500", errMap) return } - typ := c.PostForm("type") - if typ == "" { - c.Redirect(http.StatusSeeOther, "/watch/new") - return - } - query := c.PostForm("query") - if query == "" { - c.Redirect(http.StatusSeeOther, "/watch/new") - return - } - - query_model := &Query{ - URLID: uint(url_id), - Name: name, - Type: typ, - Query: query, - } - web.db.Create(query_model) + web.db.Create(&query) c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/view/%d", watch_id)) } func (web Web) createFilter(c *gin.Context) { - query_id, err := strconv.ParseUint(c.PostForm("query_id"), 10, 64) + var filter Filter + errMap, err := bindAndValidateFilter(&filter, c) if err != nil { log.Print(err) - c.Redirect(http.StatusSeeOther, "/watch/new") - return // TODO response - } - name := c.PostForm("name") - if name == "" { - log.Print(name) - c.Redirect(http.StatusSeeOther, "/watch/new") + c.HTML(http.StatusBadRequest, "500", errMap) return } - typ := c.PostForm("type") - if typ == "" { - log.Print(typ) - c.Redirect(http.StatusSeeOther, "/watch/new") - return - } - from := c.PostForm("from") - if from == "" { - log.Print(from) - c.Redirect(http.StatusSeeOther, "/watch/new") - return - } - to := c.PostForm("to") - log.Print("To:", to) - filter_model := &Filter{ - QueryID: uint(query_id), - Name: name, - Type: typ, - From: from, - To: to, - } - web.db.Create(filter_model) - c.Redirect(http.StatusSeeOther, fmt.Sprintf("/query/edit/%d", query_id)) + web.db.Create(&filter) + c.Redirect(http.StatusSeeOther, fmt.Sprintf("/query/edit/%d", filter.QueryID)) } func (web Web) editQuery(c *gin.Context) { diff --git a/models.go b/models.go index ff88d1a..b625276 100644 --- a/models.go +++ b/models.go @@ -6,36 +6,36 @@ import ( type Watch struct { gorm.Model - Name string `form:"name" yaml:"name" binding:"required" validate:"min=1"` + Name string `form:"watch_name" yaml:"watch_name" binding:"required" validate:"min=1"` Interval int `form:"interval" yaml:"interval" binding:"required"` URLs []URL } type URL struct { gorm.Model - WatchID uint `form:"watch_id" yaml:"watch_id" binding:"required"` + WatchID uint `form:"url_watch_id" yaml:"url_watch_id" binding:"required"` Watch *Watch `form:"watch" yaml:"watch" validate:"omitempty"` - Name string `form:"name" yaml:"name" binding:"required"` - URL string `form:"url" yaml:"url" binding:"required,url"` + Name string `form:"url_name" yaml:"url_name" binding:"required" validate:"min=1"` + URL string `form:"url" yaml:"url" binding:"required,url" validate:"min=1"` Queries []Query } type Query struct { gorm.Model - URLID uint `form:"url_id" yaml:"url_id" binding:"required"` - URL URL - Name string `form:"name" yaml:"name" binding:"required"` - Type string `form:"type" yaml:"type" binding:"required"` + URLID uint `form:"query_url_id" yaml:"query_url_id" binding:"required"` + URL *URL + Name string `form:"query_name" yaml:"query_name" binding:"required" validate:"min=1"` + Type string `form:"query_type" yaml:"query_type" binding:"required" validate:"oneof=css xpath regex json"` Query string `form:"query" yaml:"query" binding:"required"` Filters []Filter } type Filter struct { gorm.Model - QueryID uint `form:"query_id" yaml:"query_id" binding:"required"` - Query Query - Name string `form:"name" yaml:"name" binding:"required"` - Type string `form:"type" yaml:"type" binding:"required"` + QueryID uint `form:"filter_query_id" yaml:"filter_query_id" binding:"required"` + Query *Query + Name string `form:"filter_name" yaml:"filter_name" binding:"required" validate:"min=1"` + Type string `form:"filter_type" yaml:"filter_type" binding:"required" validate:"oneof=replace regex substring"` From string `form:"from" yaml:"from" binding:"required"` To string `form:"to" yaml:"to" binding:"required"` } diff --git a/templates/editQuery.html b/templates/editQuery.html index 256f8ab..89b2e3c 100644 --- a/templates/editQuery.html +++ b/templates/editQuery.html @@ -66,30 +66,27 @@