summaryrefslogtreecommitdiffhomepage
path: root/derelict.go
diff options
context:
space:
mode:
Diffstat (limited to 'derelict.go')
-rw-r--r--derelict.go21
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) {