Refactoring update handlers.

Fixes .
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

@ -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"`