You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.4 KiB
66 lines
1.4 KiB
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"person-site/internal/models"
|
|
)
|
|
|
|
type ExportHandler struct {
|
|
ProjectStore *models.ProjectStore
|
|
ConfigStore *models.ConfigStore
|
|
}
|
|
|
|
func (h *ExportHandler) Export(w http.ResponseWriter, r *http.Request) {
|
|
projects, err := h.ProjectStore.ListAll()
|
|
if err != nil {
|
|
Error(w, 500, "failed to fetch projects")
|
|
return
|
|
}
|
|
if projects == nil {
|
|
projects = []models.Project{}
|
|
}
|
|
|
|
config, err := h.ConfigStore.GetAll()
|
|
if err != nil {
|
|
Error(w, 500, "failed to fetch config")
|
|
return
|
|
}
|
|
|
|
configRes := buildConfigResponse(config)
|
|
|
|
JSON(w, 200, map[string]interface{}{
|
|
"projects": projects,
|
|
"config": configRes,
|
|
})
|
|
}
|
|
|
|
func (h *ExportHandler) Import(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Projects []models.Project `json:"projects"`
|
|
Config map[string]interface{} `json:"config"`
|
|
}
|
|
if err := Decode(r, &req); err != nil {
|
|
Error(w, 400, "invalid request body")
|
|
return
|
|
}
|
|
|
|
if err := h.ProjectStore.ReplaceAll(req.Projects); err != nil {
|
|
Error(w, 500, "failed to import projects: "+err.Error())
|
|
return
|
|
}
|
|
|
|
for key, value := range req.Config {
|
|
strVal, err := valueToString(value)
|
|
if err != nil {
|
|
Error(w, 400, "invalid config value for key: "+key)
|
|
return
|
|
}
|
|
if err := h.ConfigStore.Set(key, strVal); err != nil {
|
|
Error(w, 500, "failed to save config")
|
|
return
|
|
}
|
|
}
|
|
|
|
JSON(w, 200, map[string]string{"status": "ok"})
|
|
}
|
|
|