diff options
author | Aki <please@ignore.pl> | 2021-04-26 23:29:02 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-04-26 23:29:02 +0200 |
commit | 31caf8496ada3837da199bba3b1c27f5d6462f9a (patch) | |
tree | 6a068923adc34dbcd1a387223d5c852fa95607bf /derelict.go | |
parent | 60a2f794f1e04f9cc106d0df555d35c14afb3082 (diff) | |
download | field-31caf8496ada3837da199bba3b1c27f5d6462f9a.zip field-31caf8496ada3837da199bba3b1c27f5d6462f9a.tar.gz field-31caf8496ada3837da199bba3b1c27f5d6462f9a.tar.bz2 |
Remodeled battle storage
Diffstat (limited to 'derelict.go')
-rw-r--r-- | derelict.go | 21 |
1 files changed, 7 insertions, 14 deletions
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) { |