mirror of
https://github.com/ordinary-dev/phoenix
synced 2024-09-20 03:40:29 +05:00
Add the ability to edit groups
This commit is contained in:
parent
c43b63fbd6
commit
c668af5001
|
@ -7,7 +7,7 @@ import (
|
||||||
type Group struct {
|
type Group struct {
|
||||||
ID uint64 `gorm:"primaryKey"`
|
ID uint64 `gorm:"primaryKey"`
|
||||||
Name string `gorm:"unique,notNull"`
|
Name string `gorm:"unique,notNull"`
|
||||||
Links []Link
|
Links []Link `gorm:"constraint:OnDelete:CASCADE;"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGroups(db *gorm.DB) ([]Group, error) {
|
func GetGroups(db *gorm.DB) ([]Group, error) {
|
||||||
|
@ -30,3 +30,24 @@ func CreateGroup(db *gorm.DB, groupName string) (Group, error) {
|
||||||
|
|
||||||
return group, nil
|
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
12
main.go
|
@ -40,6 +40,18 @@ func main() {
|
||||||
views.CreateGroup(c, db)
|
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
|
// Create new link
|
||||||
r.POST("/links", func(c *gin.Context) {
|
r.POST("/links", func(c *gin.Context) {
|
||||||
views.CreateLink(c, db)
|
views.CreateLink(c, db)
|
||||||
|
|
|
@ -10,7 +10,8 @@
|
||||||
|
|
||||||
{{range .groups}}
|
{{range .groups}}
|
||||||
<h2>Group "{{.Name}}"</h2>
|
<h2>Group "{{.Name}}"</h2>
|
||||||
<form class="row">
|
<div class="row">
|
||||||
|
<form method="POST" action="/groups/{{.ID}}/put" class="innerForm">
|
||||||
<input
|
<input
|
||||||
value="{{.Name}}"
|
value="{{.Name}}"
|
||||||
placeholder="Name"
|
placeholder="Name"
|
||||||
|
@ -21,6 +22,12 @@
|
||||||
<img src="/assets/svg/floppy-disk-solid.svg" width="16px" height="16px" />
|
<img src="/assets/svg/floppy-disk-solid.svg" width="16px" height="16px" />
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</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}}
|
{{range .Links}}
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/ordinary-dev/phoenix/backend"
|
"github.com/ordinary-dev/phoenix/backend"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateGroup(c *gin.Context, db *gorm.DB) {
|
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.
|
// This page is called from the settings, return the user back.
|
||||||
c.Redirect(http.StatusFound, "/settings")
|
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")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue