phoenix/views/pages/links.go
Ivan R. 6d25c4e8af
feat!: migrate to net/http
With the release of Go 1.22, the standard library now has
all the necessary functions that allow us to abandon Gin.

I hope this rewrite will lower the entry barrier for new developers.
As a nice bonus, the size of the program has decreased from 20 to 15.4 MB.

To solve issue #81, request logging has been improved.
Now all errors are displayed in the logs.
2024-03-25 15:52:18 +05:00

83 lines
1.9 KiB
Go

package pages
import (
"fmt"
"net/http"
"strconv"
"github.com/ordinary-dev/phoenix/database"
)
func CreateLink(w http.ResponseWriter, r *http.Request) {
groupID, err := strconv.ParseUint(r.FormValue("groupID"), 10, 32)
if err != nil {
ShowError(w, http.StatusBadRequest, err)
return
}
link := database.Link{
Name: r.FormValue("linkName"),
Href: r.FormValue("href"),
GroupID: groupID,
}
icon := r.FormValue("icon")
if icon == "" {
link.Icon = nil
} else {
link.Icon = &icon
}
if result := database.DB.Create(&link); result.Error != nil {
ShowError(w, http.StatusInternalServerError, result.Error)
return
}
// Redirect to settings.
http.Redirect(w, r, fmt.Sprintf("/settings#link-%v", link.ID), http.StatusFound)
}
func UpdateLink(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
if err != nil {
ShowError(w, http.StatusBadRequest, err)
return
}
var link database.Link
if result := database.DB.First(&link, id); result.Error != nil {
ShowError(w, http.StatusInternalServerError, result.Error)
return
}
link.Name = r.FormValue("linkName")
link.Href = r.FormValue("href")
icon := r.FormValue("icon")
if icon == "" {
link.Icon = nil
} else {
link.Icon = &icon
}
if result := database.DB.Save(&link); result.Error != nil {
ShowError(w, http.StatusInternalServerError, result.Error)
return
}
// Redirect to settings.
http.Redirect(w, r, fmt.Sprintf("/settings#link-%v", link.ID), http.StatusFound)
}
func DeleteLink(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
if err != nil {
ShowError(w, http.StatusBadRequest, err)
return
}
if result := database.DB.Delete(&database.Link{}, id); result.Error != nil {
ShowError(w, http.StatusInternalServerError, result.Error)
return
}
// Redirect to settings.
http.Redirect(w, r, "/settings", http.StatusFound)
}