Add the ability to edit groups

This commit is contained in:
Ivan R. 2023-04-09 11:55:08 +05:00
parent c43b63fbd6
commit c668af5001
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
4 changed files with 93 additions and 12 deletions

View file

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

12
main.go
View file

@ -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)

View file

@ -10,17 +10,24 @@
{{range .groups}}
<h2>Group "{{.Name}}"</h2>
<form class="row">
<input
value="{{.Name}}"
placeholder="Name"
name="groupName"
required
/>
<button type="submit">
<img src="/assets/svg/floppy-disk-solid.svg" width="16px" height="16px" />
</button>
</form>
<div class="row">
<form method="POST" action="/groups/{{.ID}}/put" class="innerForm">
<input
value="{{.Name}}"
placeholder="Name"
name="groupName"
required
/>
<button type="submit">
<img src="/assets/svg/floppy-disk-solid.svg" width="16px" height="16px" />
</button>
</form>
<form method="POST" action="/groups/{{.ID}}/delete">
<button type="submit">
<img src="/assets/svg/trash-solid.svg" width="16px" height="16px" />
</button>
</form>
</div>
{{range .Links}}
<div class="row">

View file

@ -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")
}