2024-03-25 15:52:18 +05:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2024-03-26 01:15:51 +05:00
|
|
|
"strings"
|
2024-03-25 15:52:18 +05:00
|
|
|
|
|
|
|
"github.com/ordinary-dev/phoenix/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateGroup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Save new group to the database.
|
|
|
|
group := database.Group{
|
2024-03-26 01:15:51 +05:00
|
|
|
Name: strings.TrimSpace(r.FormValue("groupName")),
|
2024-03-25 15:52:18 +05:00
|
|
|
}
|
|
|
|
|
2024-03-26 00:40:52 +05:00
|
|
|
if err := database.CreateGroup(&group); err != nil {
|
|
|
|
ShowError(w, http.StatusInternalServerError, err)
|
2024-03-25 15:52:18 +05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This page is called from the settings, return the user back.
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/settings#group-%v", group.ID), http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateGroup(w http.ResponseWriter, r *http.Request) {
|
2024-03-26 00:40:52 +05:00
|
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
2024-03-25 15:52:18 +05:00
|
|
|
if err != nil {
|
|
|
|
ShowError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-26 01:15:51 +05:00
|
|
|
newName := strings.TrimSpace(r.FormValue("groupName"))
|
|
|
|
if err := database.UpdateGroup(int(id), newName); err != nil {
|
2024-03-26 00:40:52 +05:00
|
|
|
ShowError(w, http.StatusInternalServerError, err)
|
2024-03-25 15:52:18 +05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This page is called from the settings, return the user back.
|
2024-03-26 00:40:52 +05:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("/settings#group-%v", id), http.StatusFound)
|
2024-03-25 15:52:18 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteGroup(w http.ResponseWriter, r *http.Request) {
|
2024-03-26 00:40:52 +05:00
|
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
2024-03-25 15:52:18 +05:00
|
|
|
if err != nil {
|
|
|
|
ShowError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-26 00:40:52 +05:00
|
|
|
if err := database.DeleteGroup(int(id)); err != nil {
|
|
|
|
ShowError(w, http.StatusInternalServerError, err)
|
2024-03-25 15:52:18 +05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to settings.
|
|
|
|
http.Redirect(w, r, "/settings", http.StatusFound)
|
|
|
|
}
|