diff --git a/api/handler.go b/api/handler.go index 98c8952..b7999d8 100644 --- a/api/handler.go +++ b/api/handler.go @@ -32,6 +32,7 @@ func Handler() *mux.Router { m.Get(router.Species).Handler(handler(serveSpecies)) m.Get(router.CreateSpecies).Handler(handler(serveCreateSpecies)) m.Get(router.SpeciesList).Handler(handler(serveSpeciesList)) + m.Get(router.UpdateSpecies).Handler(handler(serveUpdateSpecies)) return m } diff --git a/api/species.go b/api/species.go index 7ff94ff..1e218a4 100644 --- a/api/species.go +++ b/api/species.go @@ -57,3 +57,22 @@ func serveSpeciesList(w http.ResponseWriter, r *http.Request) error { return writeJSON(w, species) } + +func serveUpdateSpecies(w http.ResponseWriter, r *http.Request) error { + id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) + var species models.Species + err := json.NewDecoder(r.Body).Decode(&species) + if err != nil { + return err + } + + updated, err := store.Species.Update(id, &species) + if err != nil { + return err + } + if updated { + w.WriteHeader(http.StatusOK) + } + + return writeJSON(w, species) +} diff --git a/api/species_test.go b/api/species_test.go index 490c62c..a0c003a 100644 --- a/api/species_test.go +++ b/api/species_test.go @@ -88,3 +88,33 @@ func TestSpecies_List(t *testing.T) { t.Errorf("got species %+v but wanted species %+v", species, want) } } + +func TestSpecies_Update(t *testing.T) { + setup() + + want := &models.Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"} + + calledPut := false + store.Species.(*models.MockSpeciesService).Update_ = func(id int64, species *models.Species) (bool, error) { + if id != want.Id { + t.Errorf("wanted request for species %d but got %d", want.Id, id) + } + if !normalizeDeepEqual(want, species) { + t.Errorf("wanted request for species %d but got %d", want, species) + } + calledPut = true + return true, nil + } + + success, err := apiClient.Species.Update(1, want) + if err != nil { + t.Fatal(err) + } + + if !calledPut { + t.Error("!calledPut") + } + if !success { + t.Error("!success") + } +} diff --git a/datastore/species.go b/datastore/species.go index cccdd07..c3087c4 100644 --- a/datastore/species.go +++ b/datastore/species.go @@ -39,3 +39,25 @@ func (s *speciesStore) List(opt *models.SpeciesListOptions) ([]*models.Species, } return species, nil } + +func (s *speciesStore) Update(id int64, species *models.Species) (bool, error) { + _, err := s.Get(id) + if err != nil { + return false, err + } + + if id != species.Id { + return false, models.ErrSpeciesNotFound + } + + changed, err := s.dbh.Update(species) + if err != nil { + return false, err + } + + if changed == 0 { + return false, ErrNoRowsUpdated + } + + return true, nil +} diff --git a/datastore/species_test.go b/datastore/species_test.go index 62235ba..1d7c3cd 100644 --- a/datastore/species_test.go +++ b/datastore/species_test.go @@ -111,8 +111,8 @@ func TestSpeciesStore_Update_db(t *testing.T) { if err != nil { t.Fatal(err) } - created := &model.Species{GenusId: genus.Id, SpeciesName: "Test Species"} - _, err := d.Species.Create(created) + species := &models.Species{GenusId: genus.Id, SpeciesName: "Test Species"} + created, err := d.Species.Create(species) if err != nil { t.Fatal(err) }