Skip to content

Commit

Permalink
updating item works
Browse files Browse the repository at this point in the history
  • Loading branch information
baijum committed Dec 21, 2014
1 parent a1e283c commit 07e8ab7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions pitracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ <h1 class="page-header">Edit Project</h1>

<script type="text/x-handlebars" data-template-name="project/archive">
Archive {{ name }} ? <br />
<button {{action 'save' model}}>Update Project</button>
<button {{action 'save' model}}>Archive Project</button>
</script>

<script type="text/x-handlebars" data-template-name="items">
Expand Down Expand Up @@ -440,7 +440,7 @@ <h1 class="page-header">Edit Item</h1>
</div>
</div>

<a class="btn btn-success btn-lg" href="#" role="button" {{action save model}}>Create Item</a>
<a class="btn btn-success btn-lg" href="#" role="button" {{action save model}}>Update Item</a>

</form>
</script>
Expand Down

0 comments on commit 07e8ab7

Please sign in to comment.