Update observation types.

This commit is contained in:
Matthew Dillon 2014-11-10 15:13:45 -09:00
parent 186edad1db
commit 73f7b580c1
9 changed files with 158 additions and 0 deletions

View file

@ -46,3 +46,26 @@ func (s *observationTypesStore) List(opt *models.ObservationTypeListOptions) ([]
}
return observation_types, nil
}
func (s *observationTypesStore) Update(id int64, observation_type *models.ObservationType) (bool, error) {
_, err := s.Get(id)
if err != nil {
return false, err
}
if id != observation_type.Id {
return false, models.ErrObservationTypeNotFound
}
observation_type.UpdatedAt = time.Now()
changed, err := s.dbh.Update(observation_type)
if err != nil {
return false, err
}
if changed == 0 {
return false, ErrNoRowsUpdated
}
return true, nil
}

View file

@ -85,3 +85,23 @@ func TestObservationTypesStore_List_db(t *testing.T) {
t.Errorf("got observation_types %+v, want %+v", observation_types, want)
}
}
func TestObservationTypesStore_Update_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
observation_type := insertObservationType(t, tx)
d := NewDatastore(tx)
// Tweak it
observation_type.ObservationTypeName = "Updated Obs Type"
updated, err := d.ObservationTypes.Update(observation_type.Id, observation_type)
if err != nil {
t.Fatal(err)
}
if !updated {
t.Error("!updated")
}
}