diff --git a/entities.go b/entities.go index 405bf30..59d115c 100644 --- a/entities.go +++ b/entities.go @@ -15,7 +15,7 @@ type lister interface { } type updater interface { - update(int64, *entity, Claims) *appError + update(int64, *entity, string, Claims) *appError unmarshal([]byte) (entity, error) } diff --git a/handlers.go b/handlers.go index 895cf5b..902454e 100644 --- a/handlers.go +++ b/handlers.go @@ -182,7 +182,7 @@ func handleUpdater(u updater) errorHandler { claims := getClaims(r) - appErr := u.update(id, &e, claims) + appErr := u.update(id, &e, mux.Vars(r)["genus"], claims) if appErr != nil { return appErr } diff --git a/species.go b/species.go index a416fbf..0cf735e 100644 --- a/species.go +++ b/species.go @@ -145,13 +145,13 @@ func (s SpeciesService) get(id int64, genus string, claims Claims) (entity, *app return &payload, nil } -func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError { +func (s SpeciesService) update(id int64, e *entity, genus string, claims Claims) *appError { payload := (*e).(*SpeciesPayload) payload.Species.UpdatedBy = claims.Sub payload.Species.UpdatedAt = currentTime() payload.Species.Id = id - genus_id, err := genusIdFromName(payload.Species.GenusName) + genus_id, err := genusIdFromName(genus) if err != nil { return newJSONError(err, http.StatusInternalServerError) } @@ -165,12 +165,12 @@ func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError { return ErrSpeciesNotUpdatedJSON } - species, err := getSpecies(id, payload.Species.GenusName) + species, err := getSpecies(id, genus) if err != nil { return newJSONError(err, http.StatusInternalServerError) } - strains, err := strainsFromSpeciesId(id, payload.Species.GenusName) + strains, err := strainsFromSpeciesId(id, genus) if err != nil { return newJSONError(err, http.StatusInternalServerError) } @@ -179,7 +179,7 @@ func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError { payload.Strains = strains payload.Meta = &SpeciesMeta{ CanAdd: canAdd(claims), - CanEdit: canEdit(claims, map[int64]int64{payload.Species.Id: payload.Species.CreatedBy}), + CanEdit: canEdit(claims, map[int64]int64{species.Id: species.CreatedBy}), } return nil diff --git a/strains.go b/strains.go index 20f6ca3..e530dba 100644 --- a/strains.go +++ b/strains.go @@ -149,7 +149,7 @@ func (s StrainService) get(id int64, genus string, claims Claims) (entity, *appE return &payload, nil } -func (s StrainService) update(id int64, e *entity, claims Claims) *appError { +func (s StrainService) update(id int64, e *entity, genus string, claims Claims) *appError { payload := (*e).(*StrainPayload) payload.Strain.UpdatedBy = claims.Sub payload.Strain.UpdatedAt = currentTime() @@ -163,7 +163,25 @@ func (s StrainService) update(id int64, e *entity, claims Claims) *appError { return ErrStrainNotUpdatedJSON } - // TODO: add species and meta to payload + strain, err := getStrain(id, genus) + if err != nil { + return newJSONError(err, http.StatusInternalServerError) + } + + species, err := getSpecies(strain.SpeciesId, genus) + if err != nil { + return newJSONError(err, http.StatusInternalServerError) + } + + var many_species ManySpecies = []*Species{species} + + payload.Strain = strain + + payload.Species = &many_species + payload.Meta = &StrainMeta{ + CanAdd: canAdd(claims), + CanEdit: canEdit(claims, map[int64]int64{strain.Id: strain.CreatedBy}), + } return nil } diff --git a/users.go b/users.go index 1e6e7b9..31d35b5 100644 --- a/users.go +++ b/users.go @@ -145,7 +145,7 @@ func (u UserService) list(val *url.Values, claims Claims) (entity, *appError) { return &users, nil } -func (u UserService) get(id int64, genus string, claims Claims) (entity, *appError) { +func (u UserService) get(id int64, dummy string, claims Claims) (entity, *appError) { var user User q := `SELECT id, email, 'password' AS password, name, role, created_at, updated_at, deleted_at @@ -162,7 +162,7 @@ func (u UserService) get(id int64, genus string, claims Claims) (entity, *appErr return &user, nil } -func (u UserService) update(id int64, e *entity, claims Claims) *appError { +func (u UserService) update(id int64, e *entity, dummy string, claims Claims) *appError { user := (*e).(*User) user.UpdatedAt = currentTime() user.Id = id