Add genus to create/POST
This commit is contained in:
parent
a14f4f0418
commit
0c9688917f
5 changed files with 28 additions and 10 deletions
|
@ -20,6 +20,6 @@ type updater interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type creater interface {
|
type creater interface {
|
||||||
create(*entity, Claims) *appError
|
create(*entity, string, Claims) *appError
|
||||||
unmarshal([]byte) (entity, error)
|
unmarshal([]byte) (entity, error)
|
||||||
}
|
}
|
||||||
|
|
|
@ -210,7 +210,7 @@ func handleCreater(c creater) errorHandler {
|
||||||
|
|
||||||
claims := getClaims(r)
|
claims := getClaims(r)
|
||||||
|
|
||||||
appErr := c.create(&e, claims)
|
appErr := c.create(&e, mux.Vars(r)["genus"], claims)
|
||||||
if appErr != nil {
|
if appErr != nil {
|
||||||
return appErr
|
return appErr
|
||||||
}
|
}
|
||||||
|
|
10
species.go
10
species.go
|
@ -165,6 +165,7 @@ func (s SpeciesService) update(id int64, e *entity, genus string, claims Claims)
|
||||||
return ErrSpeciesNotUpdatedJSON
|
return ErrSpeciesNotUpdatedJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reload to send back down the wire
|
||||||
species, err := getSpecies(id, genus)
|
species, err := getSpecies(id, genus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
|
@ -185,7 +186,7 @@ func (s SpeciesService) update(id int64, e *entity, genus string, claims Claims)
|
||||||
return nil
|
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)
|
payload := (*e).(*SpeciesPayload)
|
||||||
ct := currentTime()
|
ct := currentTime()
|
||||||
payload.Species.CreatedBy = claims.Sub
|
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.UpdatedBy = claims.Sub
|
||||||
payload.Species.UpdatedAt = ct
|
payload.Species.UpdatedAt = ct
|
||||||
|
|
||||||
genus_id, err := genusIdFromName(payload.Species.GenusName)
|
genus_id, err := genusIdFromName(genus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
@ -204,7 +205,8 @@ func (s SpeciesService) create(e *entity, claims Claims) *appError {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
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 {
|
if err != nil {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
@ -214,7 +216,7 @@ func (s SpeciesService) create(e *entity, claims Claims) *appError {
|
||||||
payload.Species = species
|
payload.Species = species
|
||||||
payload.Meta = &SpeciesMeta{
|
payload.Meta = &SpeciesMeta{
|
||||||
CanAdd: canAdd(claims),
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
22
strains.go
22
strains.go
|
@ -176,7 +176,6 @@ func (s StrainService) update(id int64, e *entity, genus string, claims Claims)
|
||||||
var many_species ManySpecies = []*Species{species}
|
var many_species ManySpecies = []*Species{species}
|
||||||
|
|
||||||
payload.Strain = strain
|
payload.Strain = strain
|
||||||
|
|
||||||
payload.Species = &many_species
|
payload.Species = &many_species
|
||||||
payload.Meta = &StrainMeta{
|
payload.Meta = &StrainMeta{
|
||||||
CanAdd: canAdd(claims),
|
CanAdd: canAdd(claims),
|
||||||
|
@ -186,7 +185,7 @@ func (s StrainService) update(id int64, e *entity, genus string, claims Claims)
|
||||||
return nil
|
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)
|
payload := (*e).(*StrainPayload)
|
||||||
ct := currentTime()
|
ct := currentTime()
|
||||||
payload.Strain.CreatedBy = claims.Sub
|
payload.Strain.CreatedBy = claims.Sub
|
||||||
|
@ -198,7 +197,24 @@ func (s StrainService) create(e *entity, claims Claims) *appError {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
2
users.go
2
users.go
|
@ -177,7 +177,7 @@ func (u UserService) update(id int64, e *entity, dummy string, claims Claims) *a
|
||||||
return nil
|
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)
|
user := (*e).(*User)
|
||||||
if err := user.validate(); err != nil {
|
if err := user.validate(); err != nil {
|
||||||
return &appError{Error: err, Status: StatusUnprocessableEntity}
|
return &appError{Error: err, Status: StatusUnprocessableEntity}
|
||||||
|
|
Reference in a new issue