Model validation, initial cut

Fixes #11.
This commit is contained in:
Matthew Dillon 2015-10-13 15:28:44 -07:00
parent a678eb4017
commit ae17363f8b
12 changed files with 162 additions and 52 deletions

View file

@ -137,6 +137,9 @@ func (c CharacteristicService) Update(id int64, e *types.Entity, genus string, c
if err == errors.ErrCharacteristicNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}
@ -176,6 +179,9 @@ func (c CharacteristicService) Create(e *types.Entity, genus string, claims *typ
payload.Characteristic.CharacteristicTypeID = id
if err := models.Create(payload.Characteristic.CharacteristicBase); err != nil {
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}

View file

@ -99,6 +99,9 @@ func (m MeasurementService) Update(id int64, e *types.Entity, genus string, clai
if err == errors.ErrMeasurementNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}
@ -132,6 +135,9 @@ func (m MeasurementService) Create(e *types.Entity, genus string, claims *types.
payload.Measurement.UpdatedBy = claims.Sub
if err := models.Create(payload.Measurement.MeasurementBase); err != nil {
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}

View file

@ -97,6 +97,9 @@ func (s SpeciesService) Update(id int64, e *types.Entity, genus string, claims *
if err == errors.ErrSpeciesNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}
@ -133,6 +136,9 @@ func (s SpeciesService) Create(e *types.Entity, genus string, claims *types.Clai
payload.Species.SpeciesBase.GenusID = genusID
if err := models.Create(payload.Species.SpeciesBase); err != nil {
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}

View file

@ -159,6 +159,9 @@ func (s StrainService) Update(id int64, e *types.Entity, genus string, claims *t
if err == errors.ErrStrainNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}
@ -190,6 +193,9 @@ func (s StrainService) Create(e *types.Entity, genus string, claims *types.Claim
payload.Strain.UpdatedBy = claims.Sub
if err := models.Create(payload.Strain.StrainBase); err != nil {
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}

View file

@ -105,14 +105,13 @@ func (u UserService) Update(id int64, e *types.Entity, dummy string, claims *typ
user.Verified = originalUser.Verified
user.UpdatedAt = helpers.CurrentTime()
if err := user.Validate(); err != nil {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
if err := models.Update(user.UserBase); err != nil {
if err == errors.ErrUserNotUpdated {
return newJSONError(err, http.StatusBadRequest)
}
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}
@ -124,9 +123,7 @@ func (u UserService) Update(id int64, e *types.Entity, dummy string, claims *typ
// Create initializes a new user.
func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims) *types.AppError {
user := (*e).(*payloads.User).User
if err := user.Validate(); err != nil {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
ct := helpers.CurrentTime()
user.CreatedAt = ct
user.UpdatedAt = ct
@ -144,6 +141,9 @@ func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims)
return newJSONError(errors.ErrEmailAddressTaken, http.StatusInternalServerError)
}
}
if err, ok := err.(types.ValidationError); ok {
return &types.AppError{Error: err, Status: helpers.StatusUnprocessableEntity}
}
return newJSONError(err, http.StatusInternalServerError)
}