Add genus to create/POST

This commit is contained in:
Matthew Dillon 2015-07-08 14:27:26 -08:00
parent a14f4f0418
commit 0c9688917f
5 changed files with 28 additions and 10 deletions

View file

@ -20,6 +20,6 @@ type updater interface {
}
type creater interface {
create(*entity, Claims) *appError
create(*entity, string, Claims) *appError
unmarshal([]byte) (entity, error)
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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}