backend: frontend: cleaned up the codebase and added more logging for debuging
This commit is contained in:
parent
faf7c2f782
commit
3a77eb1593
11 changed files with 152 additions and 38 deletions
|
|
@ -7,21 +7,28 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
// AddMonster is a http.HandlerFunc that adds a new monster to the Monsters slice.
|
||||
// It expects a POST request with form data containing the details of the monster.
|
||||
// The monster is then appended to the Monsters slice and a redirect response is sent.
|
||||
func AddMonster(Monsters *[]model.Monster) http.HandlerFunc {
|
||||
log.Print("AddMonster called")
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO
|
||||
// Check if the request method is POST
|
||||
if r.Method != http.MethodPost {
|
||||
log.Print("Method not allowed")
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the form data
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing form data: %s", err.Error())
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Create a new monster with the form data
|
||||
monster := model.Monster{
|
||||
Name: r.FormValue("name"),
|
||||
Source: r.FormValue("source"),
|
||||
|
|
@ -73,19 +80,33 @@ func AddMonster(Monsters *[]model.Monster) http.HandlerFunc {
|
|||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Lock the Monsters slice, append the monster, and unlock the slice
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
*Monsters = append(*Monsters, monster)
|
||||
log.Printf("Monster hinzugefügt. Anzahl der Monster jetzt: %d\n", len(*Monsters))
|
||||
|
||||
// Log the number of monsters and redirect to the monster table
|
||||
log.Printf("Monster added. Number of monsters now: %d\n", len(*Monsters))
|
||||
http.Redirect(w, r, "/monsterTable", http.StatusFound)
|
||||
}
|
||||
}
|
||||
|
||||
// parseInt konvertiert einen String zu einem Integer und gibt 0 zurück, wenn die Konvertierung fehlschlägt
|
||||
// parseInt converts a string to an integer and returns 0 if the conversion fails
|
||||
func parseInt(s string) int {
|
||||
// Add logging statement to print the input string
|
||||
log.Println("Input string:", s)
|
||||
|
||||
// Atoi is used to convert the string to an integer
|
||||
i, err := strconv.Atoi(s)
|
||||
// If there is an error in the conversion, return 0 and log the error
|
||||
if err != nil {
|
||||
log.Println("Conversion error:", err)
|
||||
return 0
|
||||
}
|
||||
// Log the converted integer
|
||||
log.Println("Converted integer:", i)
|
||||
|
||||
// Return the converted integer
|
||||
return i
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue