new folder strucure and file path

This commit is contained in:
Adrian Zürcher
2026-01-21 07:28:10 +01:00
parent 84abf6b820
commit 9150550609

View File

@@ -4,20 +4,36 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"os" "os"
"slideshowApp/utils"
) )
type Schedule map[string]interface{} var scheduleFile = "./data/schedule.json"
type Schedule map[string]any
func SaveSchedule(w http.ResponseWriter, r *http.Request) { func SaveSchedule(w http.ResponseWriter, r *http.Request) {
var s Schedule var s Schedule
json.NewDecoder(r.Body).Decode(&s) if err := json.NewDecoder(r.Body).Decode(&s); err != nil {
data, _ := json.Marshal(s) utils.SendJSONError(w, "Invalid input", http.StatusBadRequest)
os.WriteFile("schedule.json", data, 0644) return
}
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
utils.SendJSONError(w, "Encoding failed", http.StatusInternalServerError)
return
}
err = os.WriteFile(scheduleFile, data, 0644)
if err != nil {
utils.SendJSONError(w, "write file failed", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func GetSchedule(w http.ResponseWriter, r *http.Request) { func GetSchedule(w http.ResponseWriter, r *http.Request) {
data, err := os.ReadFile("schedule.json") data, err := os.ReadFile(scheduleFile)
if err != nil { if err != nil {
json.NewEncoder(w).Encode(Schedule{}) json.NewEncoder(w).Encode(Schedule{})
return return