Roughing in multi-component payload

This commit is contained in:
Matthew Dillon 2015-07-07 09:56:32 -08:00
parent a1eb237f7f
commit 1cfee8a460
9 changed files with 29 additions and 15 deletions

View file

@ -57,7 +57,7 @@ func (c *CharacteristicTypes) marshal() ([]byte, error) {
return json.Marshal(&CharacteristicTypesJSON{CharacteristicTypes: c})
}
func (c CharacteristicTypeService) list(val *url.Values) (entity, *appError) {
func (c CharacteristicTypeService) list(val *url.Values, claims Claims) (entity, *appError) {
if val == nil {
return nil, ErrMustProvideOptionsJSON
}

View file

@ -59,7 +59,7 @@ func (c *Characteristics) marshal() ([]byte, error) {
return json.Marshal(&CharacteristicsJSON{Characteristics: c})
}
func (c CharacteristicService) list(val *url.Values) (entity, *appError) {
func (c CharacteristicService) list(val *url.Values, claims Claims) (entity, *appError) {
if val == nil {
return nil, ErrMustProvideOptionsJSON
}

View file

@ -11,7 +11,7 @@ type getter interface {
}
type lister interface {
list(*url.Values) (entity, *appError)
list(*url.Values, Claims) (entity, *appError)
}
type updater interface {

View file

@ -146,7 +146,9 @@ func handleLister(l lister) errorHandler {
opt := r.URL.Query()
opt.Add("Genus", mux.Vars(r)["genus"])
es, appErr := l.list(&opt)
claims := getClaims(r)
es, appErr := l.list(&opt, claims)
if appErr != nil {
return appErr
}

View file

@ -114,3 +114,7 @@ func getClaims(r *http.Request) Claims {
}
return claims
}
func canAdd(claims Claims) bool {
return claims.Role == "A" || claims.Role == "W"
}

View file

@ -66,7 +66,7 @@ func (m *Measurements) marshal() ([]byte, error) {
return json.Marshal(&MeasurementsJSON{Measurements: m})
}
func (m MeasurementService) list(val *url.Values) (entity, *appError) {
func (m MeasurementService) list(val *url.Values, claims Claims) (entity, *appError) {
if val == nil {
return nil, ErrMustProvideOptionsJSON
}

View file

@ -50,20 +50,25 @@ type Species struct {
type ManySpecies []*Species
type SpeciesJSON struct {
Species *Species `json:"species"`
type SpeciesMeta struct {
CanAdd bool `json:"canAdd"`
}
type ManySpeciesJSON struct {
ManySpecies *ManySpecies `json:"species"`
type ManySpeciesPayload struct {
Species *ManySpecies `json:"species"`
Meta *SpeciesMeta `json:"meta"`
}
type SpeciesJSON struct {
Species *Species `json:"species"`
}
func (s *Species) marshal() ([]byte, error) {
return json.Marshal(&SpeciesJSON{Species: s})
}
func (s *ManySpecies) marshal() ([]byte, error) {
return json.Marshal(&ManySpeciesJSON{ManySpecies: s})
func (s *ManySpeciesPayload) marshal() ([]byte, error) {
return json.Marshal(s)
}
func (s SpeciesService) unmarshal(b []byte) (entity, error) {
@ -72,7 +77,7 @@ func (s SpeciesService) unmarshal(b []byte) (entity, error) {
return sj.Species, err
}
func (s SpeciesService) list(val *url.Values) (entity, *appError) {
func (s SpeciesService) list(val *url.Values, claims Claims) (entity, *appError) {
if val == nil {
return nil, ErrMustProvideOptionsJSON
}
@ -109,7 +114,10 @@ func (s SpeciesService) list(val *url.Values) (entity, *appError) {
if err != nil {
return nil, newJSONError(err, http.StatusInternalServerError)
}
return &species, nil
meta := SpeciesMeta{CanAdd: canAdd(claims)}
payload := ManySpeciesPayload{Species: &species, Meta: &meta}
return &payload, nil
}
func (s SpeciesService) get(id int64, genus string) (entity, *appError) {

View file

@ -74,7 +74,7 @@ func (s StrainService) unmarshal(b []byte) (entity, error) {
return sj.Strain, err
}
func (s StrainService) list(val *url.Values) (entity, *appError) {
func (s StrainService) list(val *url.Values, claims Claims) (entity, *appError) {
if val == nil {
return nil, ErrMustProvideOptionsJSON
}

View file

@ -124,7 +124,7 @@ func (u *User) validate() error {
return nil
}
func (u UserService) list(val *url.Values) (entity, *appError) {
func (u UserService) list(val *url.Values, claims Claims) (entity, *appError) {
if val == nil {
return nil, ErrMustProvideOptionsJSON
}