diff --git a/backend/groups.go b/backend/groups.go index 9818a5b..aa84e0d 100644 --- a/backend/groups.go +++ b/backend/groups.go @@ -7,7 +7,7 @@ import ( type Group struct { ID uint64 `gorm:"primaryKey"` Name string `gorm:"unique,notNull"` - Links []Link + Links []Link `gorm:"constraint:OnDelete:CASCADE;"` } func GetGroups(db *gorm.DB) ([]Group, error) { @@ -30,3 +30,24 @@ func CreateGroup(db *gorm.DB, groupName string) (Group, error) { return group, nil } + +func UpdateGroup(db *gorm.DB, id uint64, groupName string) (Group, error) { + var group Group + db.First(&group, id) + + group.Name = groupName + result := db.Save(&group) + if result.Error != nil { + return Group{}, result.Error + } + + return group, nil +} + +func DeleteGroup(db *gorm.DB, id uint64) error { + result := db.Delete(&Group{}, id) + if result.Error != nil { + return result.Error + } + return nil +} diff --git a/main.go b/main.go index 1dbfc6a..4c54323 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,18 @@ func main() { views.CreateGroup(c, db) }) + // Update group + // HTML forms cannot be submitted using PUT or PATCH methods without javascript. + r.POST("/groups/:id/put", func(c *gin.Context) { + views.UpdateGroup(c, db) + }) + + // Delete group + // HTML forms cannot be submitted using the DELETE method without javascript. + r.POST("/groups/:id/delete", func(c *gin.Context) { + views.DeleteGroup(c, db) + }) + // Create new link r.POST("/links", func(c *gin.Context) { views.CreateLink(c, db) diff --git a/templates/settings.html.tmpl b/templates/settings.html.tmpl index dc07442..ad22e98 100644 --- a/templates/settings.html.tmpl +++ b/templates/settings.html.tmpl @@ -10,17 +10,24 @@ {{range .groups}}

Group "{{.Name}}"

-
- - -
+
+
+ + +
+
+ +
+
{{range .Links}}
diff --git a/views/groups.go b/views/groups.go index 9e627bf..35e98cb 100644 --- a/views/groups.go +++ b/views/groups.go @@ -5,6 +5,7 @@ import ( "github.com/ordinary-dev/phoenix/backend" "gorm.io/gorm" "net/http" + "strconv" ) func CreateGroup(c *gin.Context, db *gorm.DB) { @@ -22,3 +23,43 @@ func CreateGroup(c *gin.Context, db *gorm.DB) { // This page is called from the settings, return the user back. c.Redirect(http.StatusFound, "/settings") } + +func UpdateGroup(c *gin.Context, db *gorm.DB) { + if err := RequireAuth(c, db); err != nil { + return + } + + id, err := strconv.ParseUint(c.Param("id"), 10, 64) + if err != nil { + ShowError(c, err) + return + } + groupName := c.PostForm("groupName") + if _, err := backend.UpdateGroup(db, id, groupName); err != nil { + ShowError(c, err) + return + } + + // This page is called from the settings, return the user back. + c.Redirect(http.StatusFound, "/settings") +} + +func DeleteGroup(c *gin.Context, db *gorm.DB) { + if err := RequireAuth(c, db); err != nil { + return + } + + id, err := strconv.ParseUint(c.Param("id"), 10, 64) + if err != nil { + ShowError(c, err) + return + } + + if err = backend.DeleteGroup(db, id); err != nil { + ShowError(c, err) + return + } + + // Redirect to settings. + c.Redirect(http.StatusFound, "/settings") +}