Update a unit type

This commit is contained in:
Matthew Dillon 2014-11-30 15:55:02 -09:00
parent dfa2dd2eba
commit 76baee1fa7
9 changed files with 158 additions and 0 deletions

View file

@ -46,3 +46,26 @@ func (s *unitTypesStore) List(opt *models.UnitTypeListOptions) ([]*models.UnitTy
}
return unit_types, nil
}
func (s *unitTypesStore) Update(id int64, unit_type *models.UnitType) (bool, error) {
_, err := s.Get(id)
if err != nil {
return false, err
}
if id != unit_type.Id {
return false, models.ErrUnitTypeNotFound
}
unit_type.UpdatedAt = time.Now()
changed, err := s.dbh.Update(unit_type)
if err != nil {
return false, err
}
if changed == 0 {
return false, ErrNoRowsUpdated
}
return true, nil
}

View file

@ -85,3 +85,23 @@ func TestUnitTypesStore_List_db(t *testing.T) {
t.Errorf("got unit_types %+v, want %+v", unit_types, want)
}
}
func TestUnitTypesStore_Update_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
unit_type := insertUnitType(t, tx)
d := NewDatastore(tx)
// Tweak it
unit_type.Name = "Updated Unit Type"
updated, err := d.UnitTypes.Update(unit_type.Id, unit_type)
if err != nil {
t.Fatal(err)
}
if !updated {
t.Error("!updated")
}
}