From 31caf8496ada3837da199bba3b1c27f5d6462f9a Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 26 Apr 2021 23:29:02 +0200 Subject: Remodeled battle storage --- derelict.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'derelict.go') diff --git a/derelict.go b/derelict.go index d72bfb8..cf72dd8 100644 --- a/derelict.go +++ b/derelict.go @@ -4,7 +4,6 @@ import ( "embed" "encoding/json" "html/template" - "io/ioutil" "log" "net/http" "os" @@ -57,24 +56,18 @@ func handleBattlesPost(w http.ResponseWriter, r *http.Request) { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } - var wrecks []Wreck - data, err := ioutil.ReadAll(r.Body) - if err != nil { - http.Error(w, "Unexpected error 2", http.StatusInternalServerError) - return - } - err = json.Unmarshal(data, &wrecks) - if err != nil { - http.Error(w, "Error in body", http.StatusBadRequest) + decoder := json.NewDecoder(r.Body) + var battle Battle + if err := decoder.Decode(&battle); err != nil { + http.Error(w, "Error reading battle", http.StatusBadRequest) return } - name, err := storage.AddBattle(data) + err = storage.AddBattle(&battle) if err != nil { - http.Error(w, "Error writing", http.StatusInternalServerError) + http.Error(w, "Error writing battle", http.StatusInternalServerError) return } - w.WriteHeader(http.StatusOK) - w.Write([]byte(name)) + w.Write([]byte(battle.Id)) } func handleBattlesId(w http.ResponseWriter, r *http.Request) { -- cgit v1.1