phoenix/views/pages/import.go
Ivan R. 6396c160f2
feat: export and import
The list of links can now be exported to a json file,
and later imported back.
2024-04-01 23:35:46 +05:00

39 lines
833 B
Go

package pages
import (
"encoding/json"
"net/http"
"github.com/ordinary-dev/phoenix/database"
)
func ImportPage(w http.ResponseWriter, _ *http.Request) {
Render("import.html.tmpl", w, map[string]any{})
}
func Import(w http.ResponseWriter, r *http.Request) {
var exportFile ExportFile
data := []byte(r.FormValue("exportFile"))
if err := json.Unmarshal(data, &exportFile); err != nil {
ShowError(w, http.StatusBadRequest, err)
return
}
for _, g := range exportFile.Groups {
if err := database.CreateGroup(&g); err != nil {
ShowError(w, http.StatusInternalServerError, err)
return
}
for _, l := range g.Links {
l.GroupID = g.ID
if err := database.CreateLink(&l); err != nil {
ShowError(w, http.StatusInternalServerError, err)
return
}
}
}
http.Redirect(w, r, "/", http.StatusFound)
}