backend: frontend: add file load feature
added a feature to load existing json files and load the containing monsters
This commit is contained in:
parent
32a37eea32
commit
88cfe035e0
3 changed files with 70 additions and 1 deletions
46
handlers/load_file_handler.go
Normal file
46
handlers/load_file_handler.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
1
main.go
1
main.go
|
|
@ -37,6 +37,7 @@ func main() {
|
||||||
routes.HandleFunc("/contact", handlers.ContactHandler(content))
|
routes.HandleFunc("/contact", handlers.ContactHandler(content))
|
||||||
routes.HandleFunc("/monsterTable", handlers.MonsterTableHandler(content, &Monsters))
|
routes.HandleFunc("/monsterTable", handlers.MonsterTableHandler(content, &Monsters))
|
||||||
routes.HandleFunc("/calculate-skills", handlers.SkillCalculationHandler(content))
|
routes.HandleFunc("/calculate-skills", handlers.SkillCalculationHandler(content))
|
||||||
|
routes.HandleFunc("/loadFile", handlers.LoadFileHandler(&Monsters))
|
||||||
// Print the message indicating that 'static' has been included.
|
// Print the message indicating that 'static' has been included.
|
||||||
log.Printf("Eingebunden is %v\n", static)
|
log.Printf("Eingebunden is %v\n", static)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,28 @@
|
||||||
<p class="title is-4">Monster Form</p>
|
<p class="title is-4">Monster Form</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
|
<form hx-post="/loadFile" hx-encoding="multipart/form-data"
|
||||||
|
_='on htmx:xhr:progress(loaded, total) set #progress.value to (loaded/total)*100'>
|
||||||
|
<div class="file">
|
||||||
|
<label class="file-label">
|
||||||
|
<input class="file-input" type="file" name="uploadFile">
|
||||||
|
<span class="file-cta">
|
||||||
|
<span class="file-icon">
|
||||||
|
<i class="fas fa-upload"></i>
|
||||||
|
</span>
|
||||||
|
<span class="file-label">
|
||||||
|
Choose a file…
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<div class="control">
|
||||||
|
<button class="button" type="button" hx-post="/loadFile" hx-trigger="click"
|
||||||
|
hx-target="#monster-table" hx-swap="outerHTML">Press Me</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
<form action="/submit" method="post" class="">
|
<form action="/submit" method="post" class="">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<td><label for="filename">Filename:</label></td>
|
<td><label for="filename">Filename:</label></td>
|
||||||
|
|
@ -13,6 +35,7 @@
|
||||||
class="input input-bordered w-full max-w-xs">
|
class="input input-bordered w-full max-w-xs">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{{ template "monsterform" . }}
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<div class="control">
|
<div class="control">
|
||||||
<button type="button" hx-post="/addMonster" hx-trigger="click" hx-target="#monster-table"
|
<button type="button" hx-post="/addMonster" hx-trigger="click" hx-target="#monster-table"
|
||||||
|
|
@ -20,7 +43,6 @@
|
||||||
Monster</button>
|
Monster</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{ template "monsterform" . }}
|
|
||||||
<input type="hidden" name="filename" value="{{.Filename}}">
|
<input type="hidden" name="filename" value="{{.Filename}}">
|
||||||
<input type="submit" value="Submit" class="button is-primary">
|
<input type="submit" value="Submit" class="button is-primary">
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue