Don’t need genus in chars, do on meas/id and st/id
This commit is contained in:
parent
b774da0bfb
commit
c5393e1cf5
3 changed files with 18 additions and 19 deletions
|
@ -79,7 +79,7 @@ func serveCharacteristic(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
characteristic, err := dbGetCharacteristic(id, mux.Vars(r)["genus"])
|
characteristic, err := dbGetCharacteristic(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -103,17 +103,14 @@ func dbGetCharacteristics(opt *CharacteristicListOptions) ([]*Characteristic, er
|
||||||
FROM characteristics c
|
FROM characteristics c
|
||||||
INNER JOIN characteristic_types ct ON ct.id=characteristic_type_id
|
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 measurements m ON m.characteristic_id=c.id
|
||||||
LEFT OUTER JOIN strains st ON st.id=m.strain_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)
|
|
||||||
|
|
||||||
if len(opt.Ids) != 0 {
|
if len(opt.Ids) != 0 {
|
||||||
var conds []string
|
var conds []string
|
||||||
|
|
||||||
c := "c.id IN ("
|
c := "c.id IN ("
|
||||||
for i, id := range opt.Ids {
|
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)
|
vals = append(vals, id)
|
||||||
}
|
}
|
||||||
c = c[:len(c)-1] + ")"
|
c = c[:len(c)-1] + ")"
|
||||||
|
@ -131,7 +128,7 @@ func dbGetCharacteristics(opt *CharacteristicListOptions) ([]*Characteristic, er
|
||||||
return characteristics, nil
|
return characteristics, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetCharacteristic(id int64, genus string) (*Characteristic, error) {
|
func dbGetCharacteristic(id int64) (*Characteristic, error) {
|
||||||
var characteristic Characteristic
|
var characteristic Characteristic
|
||||||
sql := `SELECT c.*, ct.characteristic_type_name,
|
sql := `SELECT c.*, ct.characteristic_type_name,
|
||||||
array_agg(m.id) AS measurements, array_agg(st.id) AS strains
|
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
|
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 measurements m ON m.characteristic_id=c.id
|
||||||
LEFT OUTER JOIN strains st ON st.id=m.strain_id
|
LEFT OUTER JOIN strains st ON st.id=m.strain_id
|
||||||
INNER JOIN species sp ON sp.id=st.species_id
|
WHERE c.id=$1
|
||||||
INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1
|
|
||||||
WHERE c.id=$2
|
|
||||||
GROUP BY c.id, ct.characteristic_type_name;`
|
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 nil, err
|
||||||
}
|
}
|
||||||
return &characteristic, nil
|
return &characteristic, nil
|
||||||
|
|
|
@ -92,7 +92,7 @@ func serveMeasurement(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
measurement, err := dbGetMeasurement(id)
|
measurement, err := dbGetMeasurement(id, mux.Vars(r)["genus"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -148,18 +148,21 @@ func dbGetMeasurements(opt *MeasurementListOptions) ([]*Measurement, error) {
|
||||||
return measurements, nil
|
return measurements, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetMeasurement(id int64) (*Measurement, error) {
|
func dbGetMeasurement(id int64, genus string) (*Measurement, error) {
|
||||||
var measurement Measurement
|
var measurement Measurement
|
||||||
sql := `SELECT m.*, c.characteristic_name,
|
sql := `SELECT m.*, c.characteristic_name,
|
||||||
t.text_measurement_name AS text_measurement_type_name,
|
t.text_measurement_name AS text_measurement_type_name,
|
||||||
u.symbol AS unit_type_name, te.name AS test_method_name
|
u.symbol AS unit_type_name, te.name AS test_method_name
|
||||||
FROM measurements m
|
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 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 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 unit_types u ON u.id=m.unit_type_id
|
||||||
LEFT OUTER JOIN test_methods te ON te.id=m.test_method_id
|
LEFT OUTER JOIN test_methods te ON te.id=m.test_method_id
|
||||||
WHERE m.id=$1;`
|
WHERE m.id=$2;`
|
||||||
if err := DBH.SelectOne(&measurement, sql, id); err != nil {
|
if err := DBH.SelectOne(&measurement, sql, genus, id); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if &measurement == nil {
|
if &measurement == nil {
|
||||||
|
|
|
@ -86,7 +86,7 @@ func serveStrain(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
strain, err := dbGetStrain(id)
|
strain, err := dbGetStrain(id, mux.Vars(r)["genus"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -137,16 +137,17 @@ func dbGetStrains(opt *StrainListOptions) ([]*Strain, error) {
|
||||||
return strains, nil
|
return strains, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetStrain(id int64) (*Strain, error) {
|
func dbGetStrain(id int64, genus string) (*Strain, error) {
|
||||||
var strain Strain
|
var strain Strain
|
||||||
sql := `SELECT st.*, sp.species_name, array_agg(m.id) AS measurements,
|
sql := `SELECT st.*, sp.species_name, array_agg(m.id) AS measurements,
|
||||||
COUNT(m) AS total_measurements
|
COUNT(m) AS total_measurements
|
||||||
FROM strains st
|
FROM strains st
|
||||||
INNER JOIN species sp ON sp.id=st.species_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 measurements m ON m.strain_id=st.id
|
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;`
|
GROUP BY st.id, sp.species_name;`
|
||||||
err := DBH.SelectOne(&strain, sql, id)
|
err := DBH.SelectOne(&strain, sql, genus, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue