From bc43d42ae8d764152dcb7ab1c53bc18fda9d0b74 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Tue, 2 Aug 2022 19:08:57 +0000 Subject: [PATCH] added updating/deleting of filters --- forms.go | 14 ++- main.go | 32 ++++++ templates/editGroup.html | 220 +++++++++++++++++++++++++++++++++------ util.go | 5 + 4 files changed, 236 insertions(+), 35 deletions(-) diff --git a/forms.go b/forms.go index 75db0ab..6752276 100644 --- a/forms.go +++ b/forms.go @@ -1,7 +1,19 @@ package main +// todo move bind/validate funcs here also + type FilterGroupUpdate struct { - ID uint `form:"group_id" binding:"required"` + ID uint `form:"group_id" binding:"required"` + // TODO add group_id for ezpz Name string `form:"group_name" binding:"required" validate:"min=1"` Type string `form:"group_type" binding:"required" validate:"oneof=diff enum number"` } + +type FilterUpdate struct { + ID uint `form:"filter_id" binding:"required"` + GroupID uint `form:"filter_group_id" binding:"required"` + Name string `form:"name" binding:"required" validate:"min=1"` + Type string `form:"filter_type" binding:"required"` + From string `form:"from" binding:"required"` + To string `form:"to" binding:"required"` +} diff --git a/main.go b/main.go index 32cc32c..a5142ce 100644 --- a/main.go +++ b/main.go @@ -108,6 +108,36 @@ func (web Web) createFilter(c *gin.Context) { c.Redirect(http.StatusSeeOther, fmt.Sprintf("/group/edit/%d", filter.FilterGroupID)) } +func (web Web) editFilter(c *gin.Context) { + var filterUpdate FilterUpdate + errMap, err := bindAndValidateFilterUpdate(&filterUpdate, c) + if err != nil { + log.Print(err) + c.HTML(http.StatusBadRequest, "500", errMap) + return + } + var filter Filter + web.db.First(&filter, filterUpdate.ID) + filter.Name = filterUpdate.Name + filter.Type = filterUpdate.Type + filter.From = filterUpdate.From + filter.To = filterUpdate.To + web.db.Save(&filter) + c.Redirect(http.StatusSeeOther, fmt.Sprintf("/group/edit/%d", +filter.FilterGroupID)) +} + +func (web Web) deleteFilter(c *gin.Context) { + id, err := strconv.Atoi(c.PostForm("filter_id")) + if err != nil { + c.Redirect(http.StatusSeeOther, "/watch/new") + return + } + + group_id := c.PostForm("group_id") + web.db.Delete(&Filter{}, id) + c.Redirect(http.StatusSeeOther, "/group/edit/"+group_id) +} + func (web Web) editGroup(c *gin.Context) { group_id, err := strconv.ParseUint(c.Param("id"), 10, 64) if err != nil { @@ -219,6 +249,8 @@ func main() { router.GET("/group/edit/:id", web.editGroup) router.POST("/group/update", web.updateGroup) router.POST("/filter/create/", web.createFilter) + router.POST("/filter/edit/", web.editFilter) + router.POST("/filter/delete/", web.deleteFilter) router.Run("0.0.0.0:8080") } diff --git a/templates/editGroup.html b/templates/editGroup.html index 211cc22..b472170 100644 --- a/templates/editGroup.html +++ b/templates/editGroup.html @@ -18,41 +18,43 @@
-
- - - - - {{ range .Group.Filters }} - - - - - - - - {{ end }} +
{{ .Name }}{{ .Type }}{{ .From }}{{ .To }} - - - - -
+ + {{ range .Group.Filters }} - + + + + + + - -
-
-
-
- -
-
-
-
{{ .Name }}{{ .Type }}{{ .From }}{{ .To }} + + +
+ + + +
+
- + {{ end }} + + +
+
+
+ +
+
+
+ + + +
@@ -100,6 +102,11 @@ + - + + +{{ range .Group.Filters }} + + +{{ end }} {{ end }} diff --git a/util.go b/util.go index 8b5abc6..b54906e 100644 --- a/util.go +++ b/util.go @@ -32,6 +32,11 @@ func bindAndValidateGroupUpdate(group *FilterGroupUpdate, c *gin.Context) (map[s return validate(err), err } +func bindAndValidateFilterUpdate(filter *FilterUpdate, c *gin.Context) (map[string]string, error) { + err := c.ShouldBind(filter) + return validate(err), err +} + func prettyError(fieldError validator.FieldError) string { switch fieldError.Tag() { case "required":