parent
7c253d7aa5
commit
b87077a1df
12 changed files with 79 additions and 35 deletions
|
@ -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.CanEdit = helpers.CanEdit(claims, payload.Characteristic.CreatedBy)
|
||||||
|
|
||||||
payload.Characteristic.CharacteristicTypeID = id
|
payload.Characteristic.CharacteristicTypeID = id
|
||||||
// TODO: fix this
|
|
||||||
count, err := models.DBH.Update(payload.Characteristic.CharacteristicBase)
|
if err := models.Update(payload.Characteristic.CharacteristicBase); err != nil {
|
||||||
if err != nil {
|
if err == errors.ErrCharacteristicNotUpdated {
|
||||||
|
return newJSONError(err, http.StatusBadRequest)
|
||||||
|
}
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
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)
|
strains, strainOpts, err := models.StrainsFromCharacteristicID(id, genus, claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -95,15 +95,12 @@ func (m MeasurementService) Update(id int64, e *types.Entity, genus string, clai
|
||||||
payload.Measurement.TextMeasurementTypeID.Valid = true
|
payload.Measurement.TextMeasurementTypeID.Valid = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: fix this
|
if err := models.Update(payload.Measurement.MeasurementBase); err != nil {
|
||||||
count, err := models.DBH.Update(payload.Measurement.MeasurementBase)
|
if err == errors.ErrMeasurementNotUpdated {
|
||||||
if err != nil {
|
return newJSONError(err, http.StatusBadRequest)
|
||||||
|
}
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
if count != 1 {
|
|
||||||
// TODO: fix this
|
|
||||||
return newJSONError(errors.ErrStrainNotUpdated, http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
|
|
||||||
measurement, err := models.GetMeasurement(id, genus, claims)
|
measurement, err := models.GetMeasurement(id, genus, claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -93,15 +93,12 @@ func (s SpeciesService) Update(id int64, e *types.Entity, genus string, claims *
|
||||||
}
|
}
|
||||||
payload.Species.SpeciesBase.GenusID = genusID
|
payload.Species.SpeciesBase.GenusID = genusID
|
||||||
|
|
||||||
// TODO: fix this
|
if err := models.Update(payload.Species.SpeciesBase); err != nil {
|
||||||
count, err := models.DBH.Update(payload.Species.SpeciesBase)
|
if err == errors.ErrSpeciesNotUpdated {
|
||||||
if err != nil {
|
return newJSONError(err, http.StatusBadRequest)
|
||||||
|
}
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
if count != 1 {
|
|
||||||
// TODO: fix this
|
|
||||||
return newJSONError(errors.ErrSpeciesNotUpdated, http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload to send back down the wire
|
// Reload to send back down the wire
|
||||||
species, err := models.GetSpecies(id, genus, claims)
|
species, err := models.GetSpecies(id, genus, claims)
|
||||||
|
|
|
@ -155,15 +155,12 @@ func (s StrainService) Update(id int64, e *types.Entity, genus string, claims *t
|
||||||
payload.Strain.UpdatedBy = claims.Sub
|
payload.Strain.UpdatedBy = claims.Sub
|
||||||
payload.Strain.ID = id
|
payload.Strain.ID = id
|
||||||
|
|
||||||
// TODO: fix this
|
if err := models.Update(payload.Strain.StrainBase); err != nil {
|
||||||
count, err := models.DBH.Update(payload.Strain.StrainBase)
|
if err == errors.ErrStrainNotUpdated {
|
||||||
if err != nil {
|
return newJSONError(err, http.StatusBadRequest)
|
||||||
|
}
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
if count != 1 {
|
|
||||||
// TODO: fix this
|
|
||||||
return newJSONError(errors.ErrStrainNotUpdated, http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
|
|
||||||
strain, err := models.GetStrain(id, genus, claims)
|
strain, err := models.GetStrain(id, genus, claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
13
api/users.go
13
api/users.go
|
@ -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}
|
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: fix this
|
if err := models.Update(user.UserBase); err != nil {
|
||||||
count, err := models.DBH.Update(user.UserBase)
|
if err == errors.ErrUserNotUpdated {
|
||||||
user.Password = ""
|
return newJSONError(err, http.StatusBadRequest)
|
||||||
if err != nil {
|
}
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
if count != 1 {
|
|
||||||
return newJSONError(errors.ErrUserNotUpdated, http.StatusInternalServerError)
|
user.Password = ""
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,4 +5,6 @@ import "errors"
|
||||||
var (
|
var (
|
||||||
// ErrMeasurementNotFound when not found.
|
// ErrMeasurementNotFound when not found.
|
||||||
ErrMeasurementNotFound = errors.New("Measurement not found")
|
ErrMeasurementNotFound = errors.New("Measurement not found")
|
||||||
|
// ErrMeasurementNotUpdate when not updated.
|
||||||
|
ErrMeasurementNotUpdated = errors.New("Measurement not updated")
|
||||||
)
|
)
|
||||||
|
|
|
@ -28,6 +28,10 @@ func (c *CharacteristicBase) PreUpdate(e modl.SqlExecutor) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CharacteristicBase) UpdateError() error {
|
||||||
|
return errors.ErrCharacteristicNotUpdated
|
||||||
|
}
|
||||||
|
|
||||||
// CharacteristicBase is what the DB expects for write operations
|
// CharacteristicBase is what the DB expects for write operations
|
||||||
type CharacteristicBase struct {
|
type CharacteristicBase struct {
|
||||||
ID int64 `json:"id,omitempty"`
|
ID int64 `json:"id,omitempty"`
|
||||||
|
|
19
models/interfaces.go
Normal file
19
models/interfaces.go
Normal 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
|
||||||
|
}
|
|
@ -29,6 +29,10 @@ func (m *MeasurementBase) PreUpdate(e modl.SqlExecutor) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MeasurementBase) UpdateError() error {
|
||||||
|
return errors.ErrMeasurementNotUpdated
|
||||||
|
}
|
||||||
|
|
||||||
// MeasurementBase is what the DB expects for write operations
|
// MeasurementBase is what the DB expects for write operations
|
||||||
// There are three types of supported measurements: fixed-text, free-text,
|
// There are three types of supported measurements: fixed-text, free-text,
|
||||||
// & numerical. The table has a constraint that will allow at most one
|
// & numerical. The table has a constraint that will allow at most one
|
||||||
|
|
|
@ -29,6 +29,10 @@ func (s *SpeciesBase) PreUpdate(e modl.SqlExecutor) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SpeciesBase) UpdateError() error {
|
||||||
|
return errors.ErrSpeciesNotUpdated
|
||||||
|
}
|
||||||
|
|
||||||
// SpeciesBase is what the DB expects for write operations.
|
// SpeciesBase is what the DB expects for write operations.
|
||||||
type SpeciesBase struct {
|
type SpeciesBase struct {
|
||||||
ID int64 `db:"id" json:"id"`
|
ID int64 `db:"id" json:"id"`
|
||||||
|
|
|
@ -29,6 +29,10 @@ func (s *StrainBase) PreUpdate(e modl.SqlExecutor) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *StrainBase) UpdateError() error {
|
||||||
|
return errors.ErrStrainNotUpdated
|
||||||
|
}
|
||||||
|
|
||||||
// StrainBase is what the DB expects for write operations.
|
// StrainBase is what the DB expects for write operations.
|
||||||
type StrainBase struct {
|
type StrainBase struct {
|
||||||
ID int64 `db:"id" json:"id"`
|
ID int64 `db:"id" json:"id"`
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"regexp"
|
"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/Godeps/_workspace/src/golang.org/x/crypto/bcrypt"
|
||||||
"github.com/thermokarst/bactdb/errors"
|
"github.com/thermokarst/bactdb/errors"
|
||||||
"github.com/thermokarst/bactdb/helpers"
|
"github.com/thermokarst/bactdb/helpers"
|
||||||
|
@ -15,6 +16,24 @@ func init() {
|
||||||
DB.AddTableWithName(UserBase{}, "users").SetKeys(true, "ID")
|
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.
|
// UserBase is what the DB expects to see for write operations.
|
||||||
type UserBase struct {
|
type UserBase struct {
|
||||||
ID int64 `json:"id,omitempty"`
|
ID int64 `json:"id,omitempty"`
|
||||||
|
|
Reference in a new issue