phoenix/views/pages/groups.go
Ivan R. 5aa2cee5b1
chore: migrate to database/sql
Since I started simplifying, I decided to abandon ORM.

I won’t say that this makes much sense, everything works more or less as before.
Except that the size of the program has decreased slightly again, by about a megabyte.
2024-03-26 00:40:52 +05:00

57 lines
1.4 KiB
Go

package pages
import (
"fmt"
"net/http"
"strconv"
"github.com/ordinary-dev/phoenix/database"
)
func CreateGroup(w http.ResponseWriter, r *http.Request) {
// Save new group to the database.
group := database.Group{
Name: r.FormValue("groupName"),
}
if err := database.CreateGroup(&group); err != nil {
ShowError(w, http.StatusInternalServerError, err)
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) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
ShowError(w, http.StatusBadRequest, err)
return
}
if err := database.UpdateGroup(int(id), r.FormValue("groupName")); err != nil {
ShowError(w, http.StatusInternalServerError, err)
return
}
// This page is called from the settings, return the user back.
http.Redirect(w, r, fmt.Sprintf("/settings#group-%v", id), http.StatusFound)
}
func DeleteGroup(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
ShowError(w, http.StatusBadRequest, err)
return
}
if err := database.DeleteGroup(int(id)); err != nil {
ShowError(w, http.StatusInternalServerError, err)
return
}
// Redirect to settings.
http.Redirect(w, r, "/settings", http.StatusFound)
}