Delete a unit type
This commit is contained in:
parent
76baee1fa7
commit
eccbffb86d
9 changed files with 141 additions and 0 deletions
|
@ -63,6 +63,7 @@ func Handler() *mux.Router {
|
||||||
m.Get(router.CreateUnitType).Handler(handler(serveCreateUnitType))
|
m.Get(router.CreateUnitType).Handler(handler(serveCreateUnitType))
|
||||||
m.Get(router.UnitTypes).Handler(handler(serveUnitTypeList))
|
m.Get(router.UnitTypes).Handler(handler(serveUnitTypeList))
|
||||||
m.Get(router.UpdateUnitType).Handler(handler(serveUpdateUnitType))
|
m.Get(router.UpdateUnitType).Handler(handler(serveUpdateUnitType))
|
||||||
|
m.Get(router.DeleteUnitType).Handler(handler(serveDeleteUnitType))
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,3 +76,17 @@ func serveUpdateUnitType(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
|
||||||
return writeJSON(w, unit_type)
|
return writeJSON(w, unit_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func serveDeleteUnitType(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||||
|
|
||||||
|
deleted, err := store.UnitTypes.Delete(id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if deleted {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
return writeJSON(w, &models.UnitType{})
|
||||||
|
}
|
||||||
|
|
|
@ -124,3 +124,30 @@ func TestUnitType_Update(t *testing.T) {
|
||||||
t.Error("!success")
|
t.Error("!success")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnitType_Delete(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
|
||||||
|
want := newUnitType()
|
||||||
|
|
||||||
|
calledDelete := false
|
||||||
|
store.UnitTypes.(*models.MockUnitTypesService).Delete_ = func(id int64) (bool, error) {
|
||||||
|
if id != want.Id {
|
||||||
|
t.Errorf("wanted request for unit_type %d but got %d", want.Id, id)
|
||||||
|
}
|
||||||
|
calledDelete = true
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
success, err := apiClient.UnitTypes.Delete(want.Id)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !calledDelete {
|
||||||
|
t.Error("!calledDelete")
|
||||||
|
}
|
||||||
|
if !success {
|
||||||
|
t.Error("!success")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -69,3 +69,19 @@ func (s *unitTypesStore) Update(id int64, unit_type *models.UnitType) (bool, err
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *unitTypesStore) Delete(id int64) (bool, error) {
|
||||||
|
unit_type, err := s.Get(id)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
deleted, err := s.dbh.Delete(unit_type)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if deleted == 0 {
|
||||||
|
return false, ErrNoRowsDeleted
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
|
@ -105,3 +105,22 @@ func TestUnitTypesStore_Update_db(t *testing.T) {
|
||||||
t.Error("!updated")
|
t.Error("!updated")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnitTypesStore_Delete_db(t *testing.T) {
|
||||||
|
tx, _ := DB.Begin()
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
unit_type := insertUnitType(t, tx)
|
||||||
|
|
||||||
|
d := NewDatastore(tx)
|
||||||
|
|
||||||
|
// Delete it
|
||||||
|
deleted, err := d.UnitTypes.Delete(unit_type.Id)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !deleted {
|
||||||
|
t.Error("!delete")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -39,6 +39,9 @@ type UnitTypesService interface {
|
||||||
|
|
||||||
// Update a unit type
|
// Update a unit type
|
||||||
Update(id int64, UnitType *UnitType) (bool, error)
|
Update(id int64, UnitType *UnitType) (bool, error)
|
||||||
|
|
||||||
|
// Delete a unit type
|
||||||
|
Delete(id int64) (deleted bool, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -135,11 +138,34 @@ func (s *unitTypesService) Update(id int64, unit_type *UnitType) (bool, error) {
|
||||||
return resp.StatusCode == http.StatusOK, nil
|
return resp.StatusCode == http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *unitTypesService) Delete(id int64) (bool, error) {
|
||||||
|
strId := strconv.FormatInt(id, 10)
|
||||||
|
|
||||||
|
url, err := s.client.url(router.DeleteUnitType, map[string]string{"Id": strId}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := s.client.NewRequest("DELETE", url.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var unit_type *UnitType
|
||||||
|
resp, err := s.client.Do(req, &unit_type)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.StatusCode == http.StatusOK, nil
|
||||||
|
}
|
||||||
|
|
||||||
type MockUnitTypesService struct {
|
type MockUnitTypesService struct {
|
||||||
Get_ func(id int64) (*UnitType, error)
|
Get_ func(id int64) (*UnitType, error)
|
||||||
List_ func(opt *UnitTypeListOptions) ([]*UnitType, error)
|
List_ func(opt *UnitTypeListOptions) ([]*UnitType, error)
|
||||||
Create_ func(unit_type *UnitType) (bool, error)
|
Create_ func(unit_type *UnitType) (bool, error)
|
||||||
Update_ func(id int64, unit_type *UnitType) (bool, error)
|
Update_ func(id int64, unit_type *UnitType) (bool, error)
|
||||||
|
Delete_ func(id int64) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ UnitTypesService = &MockUnitTypesService{}
|
var _ UnitTypesService = &MockUnitTypesService{}
|
||||||
|
@ -171,3 +197,10 @@ func (s *MockUnitTypesService) Update(id int64, unit_type *UnitType) (bool, erro
|
||||||
}
|
}
|
||||||
return s.Update_(id, unit_type)
|
return s.Update_(id, unit_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *MockUnitTypesService) Delete(id int64) (bool, error) {
|
||||||
|
if s.Delete_ == nil {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return s.Delete_(id)
|
||||||
|
}
|
||||||
|
|
|
@ -143,3 +143,32 @@ func TestUnitTypeService_Update(t *testing.T) {
|
||||||
t.Fatal("!called")
|
t.Fatal("!called")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnitTypeService_Delete(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
want := newUnitType()
|
||||||
|
|
||||||
|
var called bool
|
||||||
|
mux.HandleFunc(urlPath(t, router.DeleteUnitType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
called = true
|
||||||
|
testMethod(t, r, "DELETE")
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
writeJSON(w, want)
|
||||||
|
})
|
||||||
|
|
||||||
|
deleted, err := client.UnitTypes.Delete(want.Id)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("UnitTypes.Delete returned error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !deleted {
|
||||||
|
t.Error("!deleted")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !called {
|
||||||
|
t.Fatal("!called")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -57,6 +57,7 @@ func API() *mux.Router {
|
||||||
m.Path("/unit_types/").Methods("POST").Name(CreateUnitType)
|
m.Path("/unit_types/").Methods("POST").Name(CreateUnitType)
|
||||||
m.Path("/unit_types/{Id:.+}").Methods("GET").Name(UnitType)
|
m.Path("/unit_types/{Id:.+}").Methods("GET").Name(UnitType)
|
||||||
m.Path("/unit_types/{Id:.+}").Methods("PUT").Name(UpdateUnitType)
|
m.Path("/unit_types/{Id:.+}").Methods("PUT").Name(UpdateUnitType)
|
||||||
|
m.Path("/unit_types/{Id:.+}").Methods("DELETE").Name(DeleteUnitType)
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,4 +45,5 @@ const (
|
||||||
CreateUnitType = "unit_type:create"
|
CreateUnitType = "unit_type:create"
|
||||||
UnitTypes = "unit_type:list"
|
UnitTypes = "unit_type:list"
|
||||||
UpdateUnitType = "unit_type:update"
|
UpdateUnitType = "unit_type:update"
|
||||||
|
DeleteUnitType = "unit_type:delete"
|
||||||
)
|
)
|
||||||
|
|
Reference in a new issue