Finishing up changes from previous commit
This commit is contained in:
parent
c8d1d0a84f
commit
22d2b8b41d
5 changed files with 74 additions and 2 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Reference in a new issue