5) update services JSON errors
This commit is contained in:
parent
4ae382aa8f
commit
eb92319bdc
5 changed files with 33 additions and 33 deletions
|
@ -15,7 +15,7 @@ type lister interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type updater interface {
|
type updater interface {
|
||||||
update(int64, *entity, Claims) error
|
update(int64, *entity, Claims) *appError
|
||||||
unmarshal([]byte) (entity, error)
|
unmarshal([]byte) (entity, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
30
handlers.go
30
handlers.go
|
@ -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(handleLister(userService)), verifyClaims)).Methods("GET")
|
||||||
m.Handle("/users", j.Secure(errorHandler(handleCreater(userService)), verifyClaims)).Methods("POST")
|
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(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
|
// Path-based pattern matching subrouter
|
||||||
s := m.PathPrefix("/{genus}").Subrouter()
|
s := m.PathPrefix("/{genus}").Subrouter()
|
||||||
|
@ -97,11 +97,11 @@ func Handler() http.Handler {
|
||||||
r{handleLister(speciesService), "GET", "/species"},
|
r{handleLister(speciesService), "GET", "/species"},
|
||||||
r{handleCreater(speciesService), "POST", "/species"},
|
r{handleCreater(speciesService), "POST", "/species"},
|
||||||
r{handleGetter(speciesService), "GET", "/species/{Id:.+}"},
|
r{handleGetter(speciesService), "GET", "/species/{Id:.+}"},
|
||||||
// r{handleUpdater(speciesService), "PUT", "/species/{Id:.+}"},
|
r{handleUpdater(speciesService), "PUT", "/species/{Id:.+}"},
|
||||||
r{handleLister(strainService), "GET", "/strains"},
|
r{handleLister(strainService), "GET", "/strains"},
|
||||||
r{handleCreater(strainService), "POST", "/strains"},
|
r{handleCreater(strainService), "POST", "/strains"},
|
||||||
r{handleGetter(strainService), "GET", "/strains/{Id:.+}"},
|
r{handleGetter(strainService), "GET", "/strains/{Id:.+}"},
|
||||||
// r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"},
|
r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"},
|
||||||
r{handleLister(characteristicService), "GET", "/characteristics"},
|
r{handleLister(characteristicService), "GET", "/characteristics"},
|
||||||
r{handleGetter(characteristicService), "GET", "/characteristics/{Id:.+}"},
|
r{handleGetter(characteristicService), "GET", "/characteristics/{Id:.+}"},
|
||||||
r{handleLister(characteristicTypeService), "GET", "/characteristicTypes"},
|
r{handleLister(characteristicTypeService), "GET", "/characteristicTypes"},
|
||||||
|
@ -156,41 +156,37 @@ func handleLister(l lister) errorHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleUpdater(u updater) http.HandlerFunc {
|
func handleUpdater(u updater) errorHandler {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
e, err := u.unmarshal(bodyBytes)
|
e, err := u.unmarshal(bodyBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c := context.Get(r, "claims")
|
c := context.Get(r, "claims")
|
||||||
var claims Claims = c.(Claims)
|
var claims Claims = c.(Claims)
|
||||||
|
|
||||||
err = u.update(id, &e, claims)
|
appErr := u.update(id, &e, claims)
|
||||||
if err != nil {
|
if appErr != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
return appErr
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := e.marshal()
|
data, err := e.marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ var (
|
||||||
ErrSpeciesNotFound = errors.New("Species not found")
|
ErrSpeciesNotFound = errors.New("Species not found")
|
||||||
ErrSpeciesNotFoundJSON = newJSONError(ErrSpeciesNotFound, http.StatusNotFound)
|
ErrSpeciesNotFoundJSON = newJSONError(ErrSpeciesNotFound, http.StatusNotFound)
|
||||||
ErrSpeciesNotUpdated = errors.New("Species not updated")
|
ErrSpeciesNotUpdated = errors.New("Species not updated")
|
||||||
|
ErrSpeciesNotUpdatedJSON = newJSONError(ErrSpeciesNotUpdated, http.StatusBadRequest)
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -130,7 +131,7 @@ func (s SpeciesService) get(id int64, genus string) (entity, *appError) {
|
||||||
return &species, nil
|
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 := (*e).(*Species)
|
||||||
species.UpdatedBy = claims.Sub
|
species.UpdatedBy = claims.Sub
|
||||||
species.UpdatedAt = time.Now()
|
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)
|
count, err := DBH.Update(species.SpeciesBase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
if count != 1 {
|
if count != 1 {
|
||||||
return ErrSpeciesNotUpdated
|
return ErrSpeciesNotUpdatedJSON
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ var (
|
||||||
ErrStrainNotFound = errors.New("Strain not found")
|
ErrStrainNotFound = errors.New("Strain not found")
|
||||||
ErrStrainNotFoundJSON = newJSONError(ErrStrainNotFound, http.StatusNotFound)
|
ErrStrainNotFoundJSON = newJSONError(ErrStrainNotFound, http.StatusNotFound)
|
||||||
ErrStrainNotUpdated = errors.New("Strain not updated")
|
ErrStrainNotUpdated = errors.New("Strain not updated")
|
||||||
|
ErrStrainNotUpdatedJSON = newJSONError(ErrStrainNotUpdated, http.StatusBadRequest)
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -133,7 +134,7 @@ func (s StrainService) get(id int64, genus string) (entity, *appError) {
|
||||||
return &strain, nil
|
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 := (*e).(*Strain)
|
||||||
strain.UpdatedBy = claims.Sub
|
strain.UpdatedBy = claims.Sub
|
||||||
strain.UpdatedAt = time.Now()
|
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)
|
count, err := DBH.Update(strain.StrainBase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
if count != 1 {
|
if count != 1 {
|
||||||
return ErrStrainNotUpdated
|
return ErrStrainNotUpdatedJSON
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
8
users.go
8
users.go
|
@ -14,6 +14,8 @@ import (
|
||||||
var (
|
var (
|
||||||
ErrUserNotFound = errors.New("User not found")
|
ErrUserNotFound = errors.New("User not found")
|
||||||
ErrUserNotFoundJSON = newJSONError(ErrUserNotFound, http.StatusNotFound)
|
ErrUserNotFoundJSON = newJSONError(ErrUserNotFound, http.StatusNotFound)
|
||||||
|
ErrUserNotUpdated = errors.New("User not updated")
|
||||||
|
ErrUserNotUpdatedJSON = newJSONError(ErrUserNotUpdated, http.StatusBadRequest)
|
||||||
ErrInvalidEmailOrPassword = errors.New("Invalid email or password")
|
ErrInvalidEmailOrPassword = errors.New("Invalid email or password")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -118,17 +120,17 @@ func (u UserService) get(id int64, genus string) (entity, *appError) {
|
||||||
return &user, nil
|
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 := (*e).(*User)
|
||||||
user.UpdatedAt = time.Now()
|
user.UpdatedAt = time.Now()
|
||||||
user.Id = id
|
user.Id = id
|
||||||
|
|
||||||
count, err := DBH.Update(user)
|
count, err := DBH.Update(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
if count != 1 {
|
if count != 1 {
|
||||||
return ErrStrainNotUpdated
|
return ErrUserNotUpdatedJSON
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue