Refactor delete handlers

Fixes #18.
This commit is contained in:
Matthew Dillon 2015-10-13 13:42:19 -07:00
parent 62734899e2
commit d46e721063
15 changed files with 71 additions and 14 deletions

View file

@ -32,6 +32,10 @@ func (c *CharacteristicBase) UpdateError() error {
return errors.ErrCharacteristicNotUpdated
}
func (c *CharacteristicBase) DeleteError() error {
return errors.ErrCharacteristicNotDeleted
}
// CharacteristicBase is what the DB expects for write operations
type CharacteristicBase struct {
ID int64 `json:"id,omitempty"`

View file

@ -6,6 +6,7 @@ type base interface {
PreInsert(modl.SqlExecutor) error
PreUpdate(modl.SqlExecutor) error
UpdateError() error
DeleteError() error
}
// Create will create a new DB record of a model.
@ -27,3 +28,15 @@ func Update(b base) error {
}
return nil
}
// Delete runs a DB delete on a model.
func Delete(b base) error {
count, err := DBH.Delete(b)
if err != nil {
return err
}
if count != 1 {
return b.DeleteError()
}
return nil
}

View file

@ -33,6 +33,10 @@ func (m *MeasurementBase) UpdateError() error {
return errors.ErrMeasurementNotUpdated
}
func (m *MeasurementBase) DeleteError() error {
return errors.ErrMeasurementNotDeleted
}
// MeasurementBase is what the DB expects for write operations
// There are three types of supported measurements: fixed-text, free-text,
// & numerical. The table has a constraint that will allow at most one

View file

@ -33,6 +33,10 @@ func (s *SpeciesBase) UpdateError() error {
return errors.ErrSpeciesNotUpdated
}
func (s *SpeciesBase) DeleteError() error {
return errors.ErrSpeciesNotDeleted
}
// SpeciesBase is what the DB expects for write operations.
type SpeciesBase struct {
ID int64 `db:"id" json:"id"`

View file

@ -33,6 +33,10 @@ func (s *StrainBase) UpdateError() error {
return errors.ErrStrainNotUpdated
}
func (s *StrainBase) DeleteError() error {
return errors.ErrStrainNotDeleted
}
// StrainBase is what the DB expects for write operations.
type StrainBase struct {
ID int64 `db:"id" json:"id"`

View file

@ -34,6 +34,10 @@ func (u *UserBase) UpdateError() error {
return errors.ErrUserNotUpdated
}
func (u *UserBase) DeleteError() error {
return errors.ErrUserNotDeleted
}
// UserBase is what the DB expects to see for write operations.
type UserBase struct {
ID int64 `json:"id,omitempty"`