2023-04-09 11:22:48 +05:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2023-04-09 11:55:08 +05:00
|
|
|
"strconv"
|
2023-04-09 11:22:48 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
func CreateGroup(c *gin.Context, db *gorm.DB) {
|
|
|
|
// Save new group to the database.
|
2023-07-22 15:24:01 +05:00
|
|
|
group := database.Group{
|
2023-07-21 09:48:57 +05:00
|
|
|
Name: c.PostForm("groupName"),
|
|
|
|
}
|
|
|
|
if result := db.Create(&group); result.Error != nil {
|
|
|
|
ShowError(c, result.Error)
|
2023-04-09 11:22:48 +05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This page is called from the settings, return the user back.
|
|
|
|
c.Redirect(http.StatusFound, "/settings")
|
|
|
|
}
|
2023-04-09 11:55:08 +05:00
|
|
|
|
|
|
|
func UpdateGroup(c *gin.Context, db *gorm.DB) {
|
|
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
ShowError(c, err)
|
|
|
|
return
|
|
|
|
}
|
2023-07-21 09:48:57 +05:00
|
|
|
|
2023-07-22 15:24:01 +05:00
|
|
|
var group database.Group
|
2023-07-21 09:48:57 +05:00
|
|
|
if result := db.First(&group, id); result.Error != nil {
|
|
|
|
ShowError(c, result.Error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
group.Name = c.PostForm("groupName")
|
|
|
|
if result := db.Save(&group); result.Error != nil {
|
|
|
|
ShowError(c, result.Error)
|
2023-04-09 11:55:08 +05:00
|
|
|
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) {
|
|
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
ShowError(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-22 15:24:01 +05:00
|
|
|
if result := db.Delete(&database.Group{}, id); result.Error != nil {
|
2023-07-21 09:48:57 +05:00
|
|
|
ShowError(c, result.Error)
|
2023-04-09 11:55:08 +05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to settings.
|
|
|
|
c.Redirect(http.StatusFound, "/settings")
|
|
|
|
}
|