From 07e8ab7b7a223fbef5a6a6ad5720e09703b9ef34 Mon Sep 17 00:00:00 2001 From: Baiju Muthukadan Date: Sun, 21 Dec 2014 23:44:54 +0530 Subject: [PATCH] updating item works --- pitracker.go | 42 ++++++++++++++++++++++++++++++++++++++++++ public/index.html | 4 ++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pitracker.go b/pitracker.go index 064510b..230c17e 100644 --- a/pitracker.go +++ b/pitracker.go @@ -475,6 +475,48 @@ func GetItemHandler(w http.ResponseWriter, r *http.Request) { } func UpdateItemHandler(w http.ResponseWriter, r *http.Request) { + + type itm struct { + Id int `json:"id"` + Item + } + var il []itm + t1 := make(map[string][]itm) + + vars := mux.Vars(r) + id := vars["item"] + + i, err := strconv.Atoi(id) + if err != nil { + // handle error + log.Println(err) + log.Fatal("Wrong ID") + } + + decoder := json.NewDecoder(r.Body) + var t map[string]Item + err = decoder.Decode(&t) + if err != nil { + log.Fatal("Unable to decode body") + } + item := t["item"] + log.Printf("Item: %+v", item) + + DB.QueryRow(`UPDATE "item" + SET title = $1, description = $2 WHERE id = $3`, + item.Title, item.Description, id) + + it := itm{i, Item{item.Title, item.Description}} + il = append(il, it) + + t1["items"] = il + w.Header().Set("Content-Type", "application/json") + out, err := json.Marshal(t1) + if err != nil { + log.Fatal("Unable to marhal") + } + log.Printf("Out: %s", out) + w.Write(out) } var DB *sql.DB diff --git a/public/index.html b/public/index.html index 6566043..9d0d9de 100644 --- a/public/index.html +++ b/public/index.html @@ -372,7 +372,7 @@

Edit Project