Add missing species attributes

This commit is contained in:
Matthew Dillon 2015-06-04 10:28:49 -08:00
parent d436a8c344
commit 459dd5b4c7

View file

@ -22,6 +22,8 @@ type SpeciesService struct{}
// SpeciesBase is what the DB expects to see for inserts/updates // SpeciesBase is what the DB expects to see for inserts/updates
type SpeciesBase struct { type SpeciesBase struct {
Id int64 `db:"id" json:"id"` Id int64 `db:"id" json:"id"`
GenusID int64 `db:"genus_id" json:"-"`
SubspeciesSpeciesID NullInt64 `db:"subspecies_species_id" json:"-"`
SpeciesName string `db:"species_name" json:"speciesName"` SpeciesName string `db:"species_name" json:"speciesName"`
TypeSpecies NullBool `db:"type_species" json:"typeSpecies"` TypeSpecies NullBool `db:"type_species" json:"typeSpecies"`
Etymology NullString `db:"etymology" json:"etymology"` Etymology NullString `db:"etymology" json:"etymology"`
@ -71,9 +73,7 @@ func (s SpeciesService) list(opt *ListOptions) (entity, error) {
} }
var vals []interface{} var vals []interface{}
sql := `SELECT sp.id, sp.species_name, sp.type_species, sp.etymology, sql := `SELECT sp.*, g.genus_name, array_agg(st.id) AS strains,
sp.created_at, sp.created_by, sp.updated_at, sp.updated_by,
sp.deleted_at, sp.deleted_by, g.genus_name, array_agg(st.id) AS strains,
COUNT(st) AS total_strains COUNT(st) AS total_strains
FROM species sp FROM species sp
INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1 INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1
@ -104,9 +104,7 @@ func (s SpeciesService) list(opt *ListOptions) (entity, error) {
func (s SpeciesService) get(id int64, genus string) (entity, error) { func (s SpeciesService) get(id int64, genus string) (entity, error) {
var species Species var species Species
q := `SELECT sp.id, sp.species_name, sp.type_species, sp.etymology, q := `SELECT sp.*, g.genus_name, array_agg(st.id) AS strains,
sp.created_at, sp.created_by, sp.updated_at, sp.updated_by, sp.deleted_at,
sp.deleted_by, g.genus_name, array_agg(st.id) AS strains,
COUNT(st) AS total_strains COUNT(st) AS total_strains
FROM species sp FROM species sp
INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1 INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1
@ -146,6 +144,13 @@ func (s SpeciesService) create(e *entity, claims Claims) error {
species.UpdatedBy = claims.Sub species.UpdatedBy = claims.Sub
species.UpdatedAt = ct species.UpdatedAt = ct
var genus_id struct{ Id int64 }
q := `SELECT id FROM genera WHERE LOWER(genus_name) = $1;`
if err := DBH.SelectOne(&genus_id, q, species.GenusName); err != nil {
return err
}
species.SpeciesBase.GenusID = genus_id.Id
err := DBH.Insert(species.SpeciesBase) err := DBH.Insert(species.SpeciesBase)
if err != nil { if err != nil {
return err return err