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}}