backend: frontend: add file load feature

added a feature to load existing json files and load the containing monsters
This commit is contained in:
Patryk Hegenberg 2023-12-15 15:38:43 +01:00
parent 32a37eea32
commit 88cfe035e0
3 changed files with 70 additions and 1 deletions

View file

@ -0,0 +1,46 @@
package handlers
import (
"ddServer/model"
"encoding/json"
"fmt"
"log"
"net/http"
)
func LoadFileHandler(monsters *[]model.Monster) http.HandlerFunc {
log.Print("LoadFileHandler called")
return func(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(10 << 20) // 10 MB limit
// Get the file from the request
file, _, err := r.FormFile("uploadFile")
if err != nil {
http.Error(w, "Error retrieving file", http.StatusBadRequest)
return
}
defer file.Close()
// Parse the file content
decoder := json.NewDecoder(file)
var loadedChars model.Character
err = decoder.Decode(&loadedChars)
if err != nil {
http.Error(w, "Error decoding file content", http.StatusInternalServerError)
return
}
// Lock the Monsters slice and append the loaded monsters, then unlock the slice
mu.Lock()
defer mu.Unlock()
// Assuming 'loadedChars' contains an array of Monster objects
for _, monster := range loadedChars.Monster {
*monsters = append(*monsters, monster)
}
fmt.Printf("%v\n", monsters)
// Send a success response
http.Redirect(w, r, "/monsterTable", http.StatusTemporaryRedirect)
}
}