5) update services JSON errors

This commit is contained in:
Matthew Dillon 2015-06-24 18:05:50 -08:00
parent 4ae382aa8f
commit eb92319bdc
5 changed files with 33 additions and 33 deletions

View file

@ -15,7 +15,7 @@ type lister interface {
}
type updater interface {
update(int64, *entity, Claims) error
update(int64, *entity, Claims) *appError
unmarshal([]byte) (entity, error)
}

View file

@ -82,7 +82,7 @@ func Handler() http.Handler {
m.Handle("/users", j.Secure(errorHandler(handleLister(userService)), verifyClaims)).Methods("GET")
m.Handle("/users", j.Secure(errorHandler(handleCreater(userService)), verifyClaims)).Methods("POST")
m.Handle("/users/{Id:.+}", j.Secure(errorHandler(handleGetter(userService)), verifyClaims)).Methods("GET")
m.Handle("/users/{Id:.+}", j.Secure(http.HandlerFunc(handleUpdater(userService)), verifyClaims)).Methods("PUT")
m.Handle("/users/{Id:.+}", j.Secure(errorHandler(handleUpdater(userService)), verifyClaims)).Methods("PUT")
// Path-based pattern matching subrouter
s := m.PathPrefix("/{genus}").Subrouter()
@ -97,11 +97,11 @@ func Handler() http.Handler {
r{handleLister(speciesService), "GET", "/species"},
r{handleCreater(speciesService), "POST", "/species"},
r{handleGetter(speciesService), "GET", "/species/{Id:.+}"},
// r{handleUpdater(speciesService), "PUT", "/species/{Id:.+}"},
r{handleUpdater(speciesService), "PUT", "/species/{Id:.+}"},
r{handleLister(strainService), "GET", "/strains"},
r{handleCreater(strainService), "POST", "/strains"},
r{handleGetter(strainService), "GET", "/strains/{Id:.+}"},
// r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"},
r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"},
r{handleLister(characteristicService), "GET", "/characteristics"},
r{handleGetter(characteristicService), "GET", "/characteristics/{Id:.+}"},
r{handleLister(characteristicTypeService), "GET", "/characteristicTypes"},
@ -156,41 +156,37 @@ func handleLister(l lister) errorHandler {
}
}
func handleUpdater(u updater) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
func handleUpdater(u updater) errorHandler {
return func(w http.ResponseWriter, r *http.Request) *appError {
id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return newJSONError(err, http.StatusInternalServerError)
}
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return newJSONError(err, http.StatusInternalServerError)
}
e, err := u.unmarshal(bodyBytes)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return newJSONError(err, http.StatusInternalServerError)
}
c := context.Get(r, "claims")
var claims Claims = c.(Claims)
err = u.update(id, &e, claims)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
appErr := u.update(id, &e, claims)
if appErr != nil {
return appErr
}
data, err := e.marshal()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return newJSONError(err, http.StatusInternalServerError)
}
w.Write(data)
return nil
}
}

View file

@ -12,9 +12,10 @@ import (
)
var (
ErrSpeciesNotFound = errors.New("Species not found")
ErrSpeciesNotFoundJSON = newJSONError(ErrSpeciesNotFound, http.StatusNotFound)
ErrSpeciesNotUpdated = errors.New("Species not updated")
ErrSpeciesNotFound = errors.New("Species not found")
ErrSpeciesNotFoundJSON = newJSONError(ErrSpeciesNotFound, http.StatusNotFound)
ErrSpeciesNotUpdated = errors.New("Species not updated")
ErrSpeciesNotUpdatedJSON = newJSONError(ErrSpeciesNotUpdated, http.StatusBadRequest)
)
func init() {
@ -130,7 +131,7 @@ func (s SpeciesService) get(id int64, genus string) (entity, *appError) {
return &species, nil
}
func (s SpeciesService) update(id int64, e *entity, claims Claims) error {
func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError {
species := (*e).(*Species)
species.UpdatedBy = claims.Sub
species.UpdatedAt = time.Now()
@ -138,10 +139,10 @@ func (s SpeciesService) update(id int64, e *entity, claims Claims) error {
count, err := DBH.Update(species.SpeciesBase)
if err != nil {
return err
return newJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
return ErrSpeciesNotUpdated
return ErrSpeciesNotUpdatedJSON
}
return nil
}

View file

@ -12,9 +12,10 @@ import (
)
var (
ErrStrainNotFound = errors.New("Strain not found")
ErrStrainNotFoundJSON = newJSONError(ErrStrainNotFound, http.StatusNotFound)
ErrStrainNotUpdated = errors.New("Strain not updated")
ErrStrainNotFound = errors.New("Strain not found")
ErrStrainNotFoundJSON = newJSONError(ErrStrainNotFound, http.StatusNotFound)
ErrStrainNotUpdated = errors.New("Strain not updated")
ErrStrainNotUpdatedJSON = newJSONError(ErrStrainNotUpdated, http.StatusBadRequest)
)
func init() {
@ -133,7 +134,7 @@ func (s StrainService) get(id int64, genus string) (entity, *appError) {
return &strain, nil
}
func (s StrainService) update(id int64, e *entity, claims Claims) error {
func (s StrainService) update(id int64, e *entity, claims Claims) *appError {
strain := (*e).(*Strain)
strain.UpdatedBy = claims.Sub
strain.UpdatedAt = time.Now()
@ -141,10 +142,10 @@ func (s StrainService) update(id int64, e *entity, claims Claims) error {
count, err := DBH.Update(strain.StrainBase)
if err != nil {
return err
return newJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
return ErrStrainNotUpdated
return ErrStrainNotUpdatedJSON
}
return nil
}

View file

@ -14,6 +14,8 @@ import (
var (
ErrUserNotFound = errors.New("User not found")
ErrUserNotFoundJSON = newJSONError(ErrUserNotFound, http.StatusNotFound)
ErrUserNotUpdated = errors.New("User not updated")
ErrUserNotUpdatedJSON = newJSONError(ErrUserNotUpdated, http.StatusBadRequest)
ErrInvalidEmailOrPassword = errors.New("Invalid email or password")
)
@ -118,17 +120,17 @@ func (u UserService) get(id int64, genus string) (entity, *appError) {
return &user, nil
}
func (u UserService) update(id int64, e *entity, claims Claims) error {
func (u UserService) update(id int64, e *entity, claims Claims) *appError {
user := (*e).(*User)
user.UpdatedAt = time.Now()
user.Id = id
count, err := DBH.Update(user)
if err != nil {
return err
return newJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
return ErrStrainNotUpdated
return ErrUserNotUpdatedJSON
}
return nil
}