diff --git a/characteristics.go b/characteristics.go index 7719ea4..51c70a5 100644 --- a/characteristics.go +++ b/characteristics.go @@ -79,7 +79,7 @@ func serveCharacteristic(w http.ResponseWriter, r *http.Request) { return } - characteristic, err := dbGetCharacteristic(id, mux.Vars(r)["genus"]) + characteristic, err := dbGetCharacteristic(id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -103,17 +103,14 @@ func dbGetCharacteristics(opt *CharacteristicListOptions) ([]*Characteristic, er FROM characteristics c INNER JOIN characteristic_types ct ON ct.id=characteristic_type_id LEFT OUTER JOIN measurements m ON m.characteristic_id=c.id - LEFT OUTER JOIN strains st ON st.id=m.strain_id - INNER JOIN species sp ON sp.id=st.species_id - INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1` - vals = append(vals, opt.Genus) + LEFT OUTER JOIN strains st ON st.id=m.strain_id` if len(opt.Ids) != 0 { var conds []string c := "c.id IN (" for i, id := range opt.Ids { - c = c + fmt.Sprintf("$%v,", i+2) // start param index at 2 + c = c + fmt.Sprintf("$%v,", i+1) // start param index at 1 vals = append(vals, id) } c = c[:len(c)-1] + ")" @@ -131,7 +128,7 @@ func dbGetCharacteristics(opt *CharacteristicListOptions) ([]*Characteristic, er return characteristics, nil } -func dbGetCharacteristic(id int64, genus string) (*Characteristic, error) { +func dbGetCharacteristic(id int64) (*Characteristic, error) { var characteristic Characteristic sql := `SELECT c.*, ct.characteristic_type_name, array_agg(m.id) AS measurements, array_agg(st.id) AS strains @@ -139,11 +136,9 @@ func dbGetCharacteristic(id int64, genus string) (*Characteristic, error) { INNER JOIN characteristic_types ct ON ct.id=characteristic_type_id LEFT OUTER JOIN measurements m ON m.characteristic_id=c.id LEFT OUTER JOIN strains st ON st.id=m.strain_id - INNER JOIN species sp ON sp.id=st.species_id - INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1 - WHERE c.id=$2 + WHERE c.id=$1 GROUP BY c.id, ct.characteristic_type_name;` - if err := DBH.SelectOne(&characteristic, sql, genus, id); err != nil { + if err := DBH.SelectOne(&characteristic, sql, id); err != nil { return nil, err } return &characteristic, nil diff --git a/measurements.go b/measurements.go index 9d5d364..e06cbfc 100644 --- a/measurements.go +++ b/measurements.go @@ -92,7 +92,7 @@ func serveMeasurement(w http.ResponseWriter, r *http.Request) { return } - measurement, err := dbGetMeasurement(id) + measurement, err := dbGetMeasurement(id, mux.Vars(r)["genus"]) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -148,18 +148,21 @@ func dbGetMeasurements(opt *MeasurementListOptions) ([]*Measurement, error) { return measurements, nil } -func dbGetMeasurement(id int64) (*Measurement, error) { +func dbGetMeasurement(id int64, genus string) (*Measurement, error) { var measurement Measurement sql := `SELECT m.*, c.characteristic_name, t.text_measurement_name AS text_measurement_type_name, u.symbol AS unit_type_name, te.name AS test_method_name FROM measurements m + INNER JOIN strains st ON st.id=m.strain_id + INNER JOIN species sp ON sp.id=st.species_id + INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1 LEFT OUTER JOIN characteristics c ON c.id=m.characteristic_id LEFT OUTER JOIN text_measurement_types t ON t.id=m.text_measurement_type_id LEFT OUTER JOIN unit_types u ON u.id=m.unit_type_id LEFT OUTER JOIN test_methods te ON te.id=m.test_method_id - WHERE m.id=$1;` - if err := DBH.SelectOne(&measurement, sql, id); err != nil { + WHERE m.id=$2;` + if err := DBH.SelectOne(&measurement, sql, genus, id); err != nil { return nil, err } if &measurement == nil { diff --git a/strains.go b/strains.go index 2f033e6..cf3846b 100644 --- a/strains.go +++ b/strains.go @@ -86,7 +86,7 @@ func serveStrain(w http.ResponseWriter, r *http.Request) { return } - strain, err := dbGetStrain(id) + strain, err := dbGetStrain(id, mux.Vars(r)["genus"]) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -137,16 +137,17 @@ func dbGetStrains(opt *StrainListOptions) ([]*Strain, error) { return strains, nil } -func dbGetStrain(id int64) (*Strain, error) { +func dbGetStrain(id int64, genus string) (*Strain, error) { var strain Strain sql := `SELECT st.*, sp.species_name, array_agg(m.id) AS measurements, COUNT(m) AS total_measurements FROM strains st INNER JOIN species sp ON sp.id=st.species_id + INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1 LEFT OUTER JOIN measurements m ON m.strain_id=st.id - WHERE st.id=$1 + WHERE st.id=$2 GROUP BY st.id, sp.species_name;` - err := DBH.SelectOne(&strain, sql, id) + err := DBH.SelectOne(&strain, sql, genus, id) if err != nil { return nil, err }