Roughing in multi-component payload
This commit is contained in:
parent
a1eb237f7f
commit
1cfee8a460
9 changed files with 29 additions and 15 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ type getter interface {
|
|||
}
|
||||
|
||||
type lister interface {
|
||||
list(*url.Values) (entity, *appError)
|
||||
list(*url.Values, Claims) (entity, *appError)
|
||||
}
|
||||
|
||||
type updater interface {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -114,3 +114,7 @@ func getClaims(r *http.Request) Claims {
|
|||
}
|
||||
return claims
|
||||
}
|
||||
|
||||
func canAdd(claims Claims) bool {
|
||||
return claims.Role == "A" || claims.Role == "W"
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
24
species.go
24
species.go
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
2
users.go
2
users.go
|
@ -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
|
||||
}
|
||||
|
|
Reference in a new issue