phoenix/views/pages/export.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

30 lines
587 B
Go

package pages
import (
"encoding/json"
"net/http"
"github.com/ordinary-dev/phoenix/database"
)
type ExportFile struct {
Groups []database.Group `json:"groups"`
}
func Export(w http.ResponseWriter, _ *http.Request) {
groups, err := database.GetGroupsWithLinks()
if err != nil {
ShowError(w, http.StatusInternalServerError, err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Disposition", "attachment; filename=phoenix.json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.Encode(&ExportFile{
Groups: groups,
})
}