Refactoring update handlers.

Fixes #16.
This commit is contained in:
Matthew Dillon 2015-10-13 12:38:18 -07:00
parent 7c253d7aa5
commit b87077a1df
12 changed files with 79 additions and 35 deletions

View file

@ -132,15 +132,13 @@ func (c CharacteristicService) Update(id int64, e *types.Entity, genus string, c
payload.Characteristic.CanEdit = helpers.CanEdit(claims, payload.Characteristic.CreatedBy)
payload.Characteristic.CharacteristicTypeID = id
// TODO: fix this
count, err := models.DBH.Update(payload.Characteristic.CharacteristicBase)
if err != nil {
if err := models.Update(payload.Characteristic.CharacteristicBase); err != nil {
if err == errors.ErrCharacteristicNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
return newJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
// TODO: fix this
return newJSONError(errors.ErrCharacteristicNotUpdated, http.StatusBadRequest)
}
strains, strainOpts, err := models.StrainsFromCharacteristicID(id, genus, claims)
if err != nil {

View file

@ -95,15 +95,12 @@ func (m MeasurementService) Update(id int64, e *types.Entity, genus string, clai
payload.Measurement.TextMeasurementTypeID.Valid = true
}
// TODO: fix this
count, err := models.DBH.Update(payload.Measurement.MeasurementBase)
if err != nil {
if err := models.Update(payload.Measurement.MeasurementBase); err != nil {
if err == errors.ErrMeasurementNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
return newJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
// TODO: fix this
return newJSONError(errors.ErrStrainNotUpdated, http.StatusBadRequest)
}
measurement, err := models.GetMeasurement(id, genus, claims)
if err != nil {

View file

@ -93,15 +93,12 @@ func (s SpeciesService) Update(id int64, e *types.Entity, genus string, claims *
}
payload.Species.SpeciesBase.GenusID = genusID
// TODO: fix this
count, err := models.DBH.Update(payload.Species.SpeciesBase)
if err != nil {
if err := models.Update(payload.Species.SpeciesBase); err != nil {
if err == errors.ErrSpeciesNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
return newJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
// TODO: fix this
return newJSONError(errors.ErrSpeciesNotUpdated, http.StatusBadRequest)
}
// Reload to send back down the wire
species, err := models.GetSpecies(id, genus, claims)

View file

@ -155,15 +155,12 @@ func (s StrainService) Update(id int64, e *types.Entity, genus string, claims *t
payload.Strain.UpdatedBy = claims.Sub
payload.Strain.ID = id
// TODO: fix this
count, err := models.DBH.Update(payload.Strain.StrainBase)
if err != nil {
if err := models.Update(payload.Strain.StrainBase); err != nil {
if err == errors.ErrStrainNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
return newJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
// TODO: fix this
return newJSONError(errors.ErrStrainNotUpdated, http.StatusBadRequest)
}
strain, err := models.GetStrain(id, genus, claims)
if err != nil {

View file

@ -109,15 +109,14 @@ func (u UserService) Update(id int64, e *types.Entity, dummy string, claims *typ
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
// TODO: fix this
count, err := models.DBH.Update(user.UserBase)
user.Password = ""
if err != nil {
if err := models.Update(user.UserBase); err != nil {
if err == errors.ErrUserNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
return newJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
return newJSONError(errors.ErrUserNotUpdated, http.StatusInternalServerError)
}
user.Password = ""
return nil
}

View file

@ -5,4 +5,6 @@ import "errors"
var (
// ErrMeasurementNotFound when not found.
ErrMeasurementNotFound = errors.New("Measurement not found")
// ErrMeasurementNotUpdate when not updated.
ErrMeasurementNotUpdated = errors.New("Measurement not updated")
)

View file

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

19
models/interfaces.go Normal file
View file

@ -0,0 +1,19 @@
package models
import "github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/jmoiron/modl"
type updater interface {
PreUpdate(modl.SqlExecutor) error
UpdateError() error
}
func Update(u updater) error {
count, err := DBH.Update(u)
if err != nil {
return err
}
if count != 1 {
return u.UpdateError()
}
return nil
}

View file

@ -29,6 +29,10 @@ func (m *MeasurementBase) PreUpdate(e modl.SqlExecutor) error {
return nil
}
func (m *MeasurementBase) UpdateError() error {
return errors.ErrMeasurementNotUpdated
}
// 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

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

View file

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

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"regexp"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/jmoiron/modl"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/golang.org/x/crypto/bcrypt"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
@ -15,6 +16,24 @@ func init() {
DB.AddTableWithName(UserBase{}, "users").SetKeys(true, "ID")
}
// PreInsert is a modl hook.
func (u *UserBase) PreInsert(e modl.SqlExecutor) error {
ct := helpers.CurrentTime()
u.CreatedAt = ct
u.UpdatedAt = ct
return nil
}
// PreUpdate is a modl hook.
func (u *UserBase) PreUpdate(e modl.SqlExecutor) error {
u.UpdatedAt = helpers.CurrentTime()
return nil
}
func (u *UserBase) UpdateError() error {
return errors.ErrUserNotUpdated
}
// UserBase is what the DB expects to see for write operations.
type UserBase struct {
ID int64 `json:"id,omitempty"`