Genus ID helper (fixes species PUT regression)
This commit is contained in:
parent
12e3bac6ce
commit
a1eb237f7f
1 changed files with 19 additions and 5 deletions
24
species.go
24
species.go
|
@ -136,6 +136,12 @@ func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError {
|
||||||
species.UpdatedAt = currentTime()
|
species.UpdatedAt = currentTime()
|
||||||
species.Id = id
|
species.Id = id
|
||||||
|
|
||||||
|
genus_id, err := genusIdFromName(species.GenusName)
|
||||||
|
if err != nil {
|
||||||
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
species.SpeciesBase.GenusID = genus_id
|
||||||
|
|
||||||
count, err := DBH.Update(species.SpeciesBase)
|
count, err := DBH.Update(species.SpeciesBase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
|
@ -154,16 +160,24 @@ func (s SpeciesService) create(e *entity, claims Claims) *appError {
|
||||||
species.UpdatedBy = claims.Sub
|
species.UpdatedBy = claims.Sub
|
||||||
species.UpdatedAt = ct
|
species.UpdatedAt = ct
|
||||||
|
|
||||||
var genus_id struct{ Id int64 }
|
genus_id, err := genusIdFromName(species.GenusName)
|
||||||
q := `SELECT id FROM genera WHERE LOWER(genus_name) = LOWER($1);`
|
if err != nil {
|
||||||
if err := DBH.SelectOne(&genus_id, q, species.GenusName); err != nil {
|
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
species.SpeciesBase.GenusID = genus_id.Id
|
species.SpeciesBase.GenusID = genus_id
|
||||||
|
|
||||||
err := DBH.Insert(species.SpeciesBase)
|
err = DBH.Insert(species.SpeciesBase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newJSONError(err, http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genusIdFromName(genus_name string) (int64, error) {
|
||||||
|
var genus_id struct{ Id int64 }
|
||||||
|
q := `SELECT id FROM genera WHERE LOWER(genus_name) = LOWER($1);`
|
||||||
|
if err := DBH.SelectOne(&genus_id, q, genus_name); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return genus_id.Id, nil
|
||||||
|
}
|
||||||
|
|
Reference in a new issue