diff --git a/api/characteristics.go b/api/characteristics.go index 3d05002..872d97f 100644 --- a/api/characteristics.go +++ b/api/characteristics.go @@ -175,9 +175,7 @@ func (c CharacteristicService) Create(e *types.Entity, genus string, claims *typ } payload.Characteristic.CharacteristicTypeID = id - // TODO: fix this - err = models.DBH.Insert(payload.Characteristic.CharacteristicBase) - if err != nil { + if err := models.Create(payload.Characteristic.CharacteristicBase); err != nil { return newJSONError(err, http.StatusInternalServerError) } diff --git a/api/measurements.go b/api/measurements.go index eb88bf5..3408923 100644 --- a/api/measurements.go +++ b/api/measurements.go @@ -129,8 +129,7 @@ func (m MeasurementService) Create(e *types.Entity, genus string, claims *types. payload.Measurement.CreatedBy = claims.Sub payload.Measurement.UpdatedBy = claims.Sub - // TODO: fix this - if err := models.DBH.Insert(payload.Measurement.MeasurementBase); err != nil { + if err := models.Create(payload.Measurement.MeasurementBase); err != nil { return newJSONError(err, http.StatusInternalServerError) } diff --git a/api/species.go b/api/species.go index 72daf06..268ef1d 100644 --- a/api/species.go +++ b/api/species.go @@ -132,9 +132,7 @@ func (s SpeciesService) Create(e *types.Entity, genus string, claims *types.Clai } payload.Species.SpeciesBase.GenusID = genusID - // TODO: fix this - err = models.DBH.Insert(payload.Species.SpeciesBase) - if err != nil { + if err := models.Create(payload.Species.SpeciesBase); err != nil { return newJSONError(err, http.StatusInternalServerError) } diff --git a/api/strains.go b/api/strains.go index 7268f57..1f11b8d 100644 --- a/api/strains.go +++ b/api/strains.go @@ -189,8 +189,7 @@ func (s StrainService) Create(e *types.Entity, genus string, claims *types.Claim payload.Strain.CreatedBy = claims.Sub payload.Strain.UpdatedBy = claims.Sub - // TODO: fix this - if err := models.DBH.Insert(payload.Strain.StrainBase); err != nil { + if err := models.Create(payload.Strain.StrainBase); err != nil { return newJSONError(err, http.StatusInternalServerError) } diff --git a/api/users.go b/api/users.go index 2a20e43..a2591d6 100644 --- a/api/users.go +++ b/api/users.go @@ -138,8 +138,7 @@ func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims) user.Role = "R" user.Verified = false - // TODO: fix this - if err := models.DBH.Insert(user.UserBase); err != nil { + if err := models.Create(user.UserBase); err != nil { if err, ok := err.(*pq.Error); ok { if err.Code == "23505" { return newJSONError(errors.ErrEmailAddressTaken, http.StatusInternalServerError) diff --git a/models/interfaces.go b/models/interfaces.go index 2bae70c..22a772e 100644 --- a/models/interfaces.go +++ b/models/interfaces.go @@ -3,11 +3,20 @@ package models import "github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/jmoiron/modl" type base interface { - PreCreate(modl.SqlExecutor) error + PreInsert(modl.SqlExecutor) error PreUpdate(modl.SqlExecutor) error UpdateError() error } +// Create will create a new DB record of a model. +func Create(b base) error { + if err := DBH.Insert(b); err != nil { + return nil + } + return nil +} + +// Update runs a DB update on a model. func Update(b base) error { count, err := DBH.Update(b) if err != nil {