diff --git a/api/handler.go b/api/handler.go index fcd6d59..cd5afc2 100644 --- a/api/handler.go +++ b/api/handler.go @@ -63,6 +63,7 @@ func Handler() *mux.Router { m.Get(router.CreateUnitType).Handler(handler(serveCreateUnitType)) m.Get(router.UnitTypes).Handler(handler(serveUnitTypeList)) m.Get(router.UpdateUnitType).Handler(handler(serveUpdateUnitType)) + m.Get(router.DeleteUnitType).Handler(handler(serveDeleteUnitType)) return m } diff --git a/api/unit_types.go b/api/unit_types.go index 306be97..9a4be14 100644 --- a/api/unit_types.go +++ b/api/unit_types.go @@ -76,3 +76,17 @@ func serveUpdateUnitType(w http.ResponseWriter, r *http.Request) error { 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{}) +} diff --git a/api/unit_types_test.go b/api/unit_types_test.go index 09229e6..db39d97 100644 --- a/api/unit_types_test.go +++ b/api/unit_types_test.go @@ -124,3 +124,30 @@ func TestUnitType_Update(t *testing.T) { 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") + } +} diff --git a/datastore/unit_types.go b/datastore/unit_types.go index a53c816..c779572 100644 --- a/datastore/unit_types.go +++ b/datastore/unit_types.go @@ -69,3 +69,19 @@ func (s *unitTypesStore) Update(id int64, unit_type *models.UnitType) (bool, err 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 +} diff --git a/datastore/unit_types_test.go b/datastore/unit_types_test.go index 3675785..a22165b 100644 --- a/datastore/unit_types_test.go +++ b/datastore/unit_types_test.go @@ -105,3 +105,22 @@ func TestUnitTypesStore_Update_db(t *testing.T) { 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") + } +} diff --git a/models/unit_types.go b/models/unit_types.go index 8fb3478..f1ca92b 100644 --- a/models/unit_types.go +++ b/models/unit_types.go @@ -39,6 +39,9 @@ type UnitTypesService interface { // Update a unit type Update(id int64, UnitType *UnitType) (bool, error) + + // Delete a unit type + Delete(id int64) (deleted bool, err error) } var ( @@ -135,11 +138,34 @@ func (s *unitTypesService) Update(id int64, unit_type *UnitType) (bool, error) { 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 { Get_ func(id int64) (*UnitType, error) List_ func(opt *UnitTypeListOptions) ([]*UnitType, error) Create_ func(unit_type *UnitType) (bool, error) Update_ func(id int64, unit_type *UnitType) (bool, error) + Delete_ func(id int64) (bool, error) } var _ UnitTypesService = &MockUnitTypesService{} @@ -171,3 +197,10 @@ func (s *MockUnitTypesService) Update(id int64, unit_type *UnitType) (bool, erro } 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) +} diff --git a/models/unit_types_test.go b/models/unit_types_test.go index 1e76a43..968b240 100644 --- a/models/unit_types_test.go +++ b/models/unit_types_test.go @@ -143,3 +143,32 @@ func TestUnitTypeService_Update(t *testing.T) { 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") + } +} diff --git a/router/api.go b/router/api.go index 9c9954f..7144f39 100644 --- a/router/api.go +++ b/router/api.go @@ -57,6 +57,7 @@ func API() *mux.Router { m.Path("/unit_types/").Methods("POST").Name(CreateUnitType) m.Path("/unit_types/{Id:.+}").Methods("GET").Name(UnitType) m.Path("/unit_types/{Id:.+}").Methods("PUT").Name(UpdateUnitType) + m.Path("/unit_types/{Id:.+}").Methods("DELETE").Name(DeleteUnitType) return m } diff --git a/router/routes.go b/router/routes.go index 29e7317..0966496 100644 --- a/router/routes.go +++ b/router/routes.go @@ -45,4 +45,5 @@ const ( CreateUnitType = "unit_type:create" UnitTypes = "unit_type:list" UpdateUnitType = "unit_type:update" + DeleteUnitType = "unit_type:delete" )