From 0c9688917f9c174c84ad715d0f037d8b9110a0da Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 8 Jul 2015 14:27:26 -0800 Subject: [PATCH] Add genus to create/POST --- entities.go | 2 +- handlers.go | 2 +- species.go | 10 ++++++---- strains.go | 22 +++++++++++++++++++--- users.go | 2 +- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/entities.go b/entities.go index 59d115c..a5a83e8 100644 --- a/entities.go +++ b/entities.go @@ -20,6 +20,6 @@ type updater interface { } type creater interface { - create(*entity, Claims) *appError + create(*entity, string, Claims) *appError unmarshal([]byte) (entity, error) } diff --git a/handlers.go b/handlers.go index 902454e..c127bfc 100644 --- a/handlers.go +++ b/handlers.go @@ -210,7 +210,7 @@ func handleCreater(c creater) errorHandler { claims := getClaims(r) - appErr := c.create(&e, claims) + appErr := c.create(&e, mux.Vars(r)["genus"], claims) if appErr != nil { return appErr } diff --git a/species.go b/species.go index 0cf735e..2b9880d 100644 --- a/species.go +++ b/species.go @@ -165,6 +165,7 @@ func (s SpeciesService) update(id int64, e *entity, genus string, claims Claims) return ErrSpeciesNotUpdatedJSON } + // Reload to send back down the wire species, err := getSpecies(id, genus) if err != nil { return newJSONError(err, http.StatusInternalServerError) @@ -185,7 +186,7 @@ func (s SpeciesService) update(id int64, e *entity, genus string, claims Claims) return nil } -func (s SpeciesService) create(e *entity, claims Claims) *appError { +func (s SpeciesService) create(e *entity, genus string, claims Claims) *appError { payload := (*e).(*SpeciesPayload) ct := currentTime() payload.Species.CreatedBy = claims.Sub @@ -193,7 +194,7 @@ func (s SpeciesService) create(e *entity, claims Claims) *appError { payload.Species.UpdatedBy = claims.Sub payload.Species.UpdatedAt = ct - genus_id, err := genusIdFromName(payload.Species.GenusName) + genus_id, err := genusIdFromName(genus) if err != nil { return newJSONError(err, http.StatusInternalServerError) } @@ -204,7 +205,8 @@ func (s SpeciesService) create(e *entity, claims Claims) *appError { return newJSONError(err, http.StatusInternalServerError) } - species, err := getSpecies(payload.Species.Id, payload.Species.GenusName) + // Reload to send back down the wire + species, err := getSpecies(payload.Species.Id, genus) if err != nil { return newJSONError(err, http.StatusInternalServerError) } @@ -214,7 +216,7 @@ func (s SpeciesService) create(e *entity, claims Claims) *appError { payload.Species = species 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 e530dba..1c4883a 100644 --- a/strains.go +++ b/strains.go @@ -176,7 +176,6 @@ func (s StrainService) update(id int64, e *entity, genus string, claims Claims) var many_species ManySpecies = []*Species{species} payload.Strain = strain - payload.Species = &many_species payload.Meta = &StrainMeta{ CanAdd: canAdd(claims), @@ -186,7 +185,7 @@ func (s StrainService) update(id int64, e *entity, genus string, claims Claims) return nil } -func (s StrainService) create(e *entity, claims Claims) *appError { +func (s StrainService) create(e *entity, genus string, claims Claims) *appError { payload := (*e).(*StrainPayload) ct := currentTime() payload.Strain.CreatedBy = claims.Sub @@ -198,7 +197,24 @@ func (s StrainService) create(e *entity, claims Claims) *appError { return newJSONError(err, http.StatusInternalServerError) } - // TODO: add species and meta to payload + strain, err := getStrain(payload.Strain.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 31d35b5..2e347f9 100644 --- a/users.go +++ b/users.go @@ -177,7 +177,7 @@ func (u UserService) update(id int64, e *entity, dummy string, claims Claims) *a return nil } -func (u UserService) create(e *entity, claims Claims) *appError { +func (u UserService) create(e *entity, dummy string, claims Claims) *appError { user := (*e).(*User) if err := user.validate(); err != nil { return &appError{Error: err, Status: StatusUnprocessableEntity}