phoenix/views/links.go

90 lines
1.8 KiB
Go
Raw Normal View History

2023-04-09 11:22:48 +05:00
package views
import (
"fmt"
2023-04-09 11:22:48 +05:00
"github.com/gin-gonic/gin"
2023-07-22 15:24:01 +05:00
"github.com/ordinary-dev/phoenix/database"
2023-04-09 11:22:48 +05:00
"gorm.io/gorm"
"net/http"
"strconv"
)
2023-11-01 22:40:15 +05:00
func CreateLink(db *gorm.DB) gin.HandlerFunc {
return func(ctx *gin.Context) {
groupID, err := strconv.ParseUint(ctx.PostForm("groupID"), 10, 32)
if err != nil {
ShowError(ctx, err)
return
}
2023-04-09 11:22:48 +05:00
2023-11-01 22:40:15 +05:00
link := database.Link{
Name: ctx.PostForm("linkName"),
Href: ctx.PostForm("href"),
GroupID: groupID,
}
icon := ctx.PostForm("icon")
if icon == "" {
link.Icon = nil
} else {
link.Icon = &icon
}
if result := db.Create(&link); result.Error != nil {
ShowError(ctx, result.Error)
return
}
2023-04-09 11:22:48 +05:00
2023-11-01 22:40:15 +05:00
// Redirect to settings.
ctx.Redirect(http.StatusFound, fmt.Sprintf("/settings#link-%v", link.ID))
2023-11-01 22:40:15 +05:00
}
2023-04-09 11:22:48 +05:00
}
2023-11-01 22:40:15 +05:00
func UpdateLink(db *gorm.DB) gin.HandlerFunc {
return func(ctx *gin.Context) {
id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
if err != nil {
ShowError(ctx, err)
return
}
2023-04-09 11:22:48 +05:00
2023-11-01 22:40:15 +05:00
var link database.Link
if result := db.First(&link, id); result.Error != nil {
ShowError(ctx, err)
return
}
2023-04-09 11:22:48 +05:00
2023-11-01 22:40:15 +05:00
link.Name = ctx.PostForm("linkName")
link.Href = ctx.PostForm("href")
icon := ctx.PostForm("icon")
if icon == "" {
link.Icon = nil
} else {
link.Icon = &icon
}
if result := db.Save(&link); result.Error != nil {
ShowError(ctx, result.Error)
return
}
2023-11-01 22:40:15 +05:00
// Redirect to settings.
ctx.Redirect(http.StatusFound, fmt.Sprintf("/settings#link-%v", link.ID))
2023-11-01 22:40:15 +05:00
}
2023-04-09 11:22:48 +05:00
}
2023-11-01 22:40:15 +05:00
func DeleteLink(db *gorm.DB) gin.HandlerFunc {
return func(ctx *gin.Context) {
id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
if err != nil {
ShowError(ctx, err)
return
}
2023-04-09 11:22:48 +05:00
2023-11-01 22:40:15 +05:00
if result := db.Delete(&database.Link{}, id); result.Error != nil {
ShowError(ctx, result.Error)
return
}
2023-04-09 11:22:48 +05:00
2023-11-01 22:40:15 +05:00
// Redirect to settings.
ctx.Redirect(http.StatusFound, "/settings")
}
2023-04-09 11:22:48 +05:00
}