Move url param to list func, search meas.
This commit is contained in:
parent
765ceae620
commit
d861eff377
6 changed files with 83 additions and 27 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -52,10 +53,15 @@ func (c *Characteristics) marshal() ([]byte, error) {
|
||||||
return json.Marshal(&CharacteristicsJSON{Characteristics: c})
|
return json.Marshal(&CharacteristicsJSON{Characteristics: c})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c CharacteristicService) list(opt *ListOptions) (entity, error) {
|
func (c CharacteristicService) list(val *url.Values) (entity, error) {
|
||||||
if opt == nil {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, errors.New("must provide options")
|
||||||
}
|
}
|
||||||
|
var opt ListOptions
|
||||||
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
var vals []interface{}
|
var vals []interface{}
|
||||||
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
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "net/url"
|
||||||
|
|
||||||
type entity interface {
|
type entity interface {
|
||||||
marshal() ([]byte, error)
|
marshal() ([]byte, error)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +11,7 @@ type getter interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type lister interface {
|
type lister interface {
|
||||||
list(*ListOptions) (entity, error)
|
list(*url.Values) (entity, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type updater interface {
|
type updater interface {
|
||||||
|
|
|
@ -132,13 +132,8 @@ func handleGetter(g getter) http.HandlerFunc {
|
||||||
|
|
||||||
func handleLister(l lister) http.HandlerFunc {
|
func handleLister(l lister) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var opt ListOptions
|
opt := r.URL.Query()
|
||||||
if err := schemaDecoder.Decode(&opt, r.URL.Query()); err != nil {
|
opt.Add("Genus", mux.Vars(r)["genus"])
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
opt.Genus = mux.Vars(r)["genus"]
|
|
||||||
|
|
||||||
es, err := l.list(&opt)
|
es, err := l.list(&opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -63,10 +64,18 @@ func (m *Measurements) marshal() ([]byte, error) {
|
||||||
return json.Marshal(&MeasurementsJSON{Measurements: m})
|
return json.Marshal(&MeasurementsJSON{Measurements: m})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m MeasurementService) list(opt *ListOptions) (entity, error) {
|
func (m MeasurementService) list(val *url.Values) (entity, error) {
|
||||||
if opt == nil {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, errors.New("must provide options")
|
||||||
}
|
}
|
||||||
|
var opt struct {
|
||||||
|
ListOptions
|
||||||
|
Strain *int64
|
||||||
|
Characteristic *int64
|
||||||
|
}
|
||||||
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
var vals []interface{}
|
var vals []interface{}
|
||||||
sql := `SELECT m.*, t.text_measurement_name AS text_measurement_type_name,
|
sql := `SELECT m.*, t.text_measurement_name AS text_measurement_type_name,
|
||||||
|
@ -81,17 +90,51 @@ func (m MeasurementService) list(opt *ListOptions) (entity, error) {
|
||||||
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`
|
||||||
vals = append(vals, opt.Genus)
|
vals = append(vals, opt.Genus)
|
||||||
|
|
||||||
if len(opt.Ids) != 0 {
|
strainId := opt.Strain != nil
|
||||||
var conds []string
|
charId := opt.Characteristic != nil
|
||||||
|
ids := len(opt.Ids) != 0
|
||||||
|
|
||||||
m := "m.id IN ("
|
if strainId || charId || ids {
|
||||||
for i, id := range opt.Ids {
|
paramsCounter := 2
|
||||||
m = m + fmt.Sprintf("$%v,", i+2) // start param index at 2
|
sql += "\nWHERE ("
|
||||||
vals = append(vals, id)
|
|
||||||
|
// Filter by strain
|
||||||
|
if strainId {
|
||||||
|
sql += fmt.Sprintf("st.id=$%v", paramsCounter)
|
||||||
|
vals = append(vals, *opt.Strain)
|
||||||
|
paramsCounter++
|
||||||
}
|
}
|
||||||
m = m[:len(m)-1] + ")"
|
|
||||||
conds = append(conds, m)
|
if strainId && (charId || ids) {
|
||||||
sql += " WHERE (" + strings.Join(conds, ") AND (") + ")"
|
sql += " AND "
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter by characteristic
|
||||||
|
if charId {
|
||||||
|
sql += fmt.Sprintf("c.id=$%v", paramsCounter)
|
||||||
|
vals = append(vals, *opt.Characteristic)
|
||||||
|
paramsCounter++
|
||||||
|
}
|
||||||
|
|
||||||
|
if charId && ids {
|
||||||
|
sql += " AND "
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get specific records
|
||||||
|
if ids {
|
||||||
|
var conds []string
|
||||||
|
|
||||||
|
m := "m.id IN ("
|
||||||
|
for _, id := range opt.Ids {
|
||||||
|
m = m + fmt.Sprintf("$%v,", paramsCounter)
|
||||||
|
vals = append(vals, id)
|
||||||
|
paramsCounter++
|
||||||
|
}
|
||||||
|
m = m[:len(m)-1] + ")"
|
||||||
|
conds = append(conds, m)
|
||||||
|
sql += strings.Join(conds, ") AND (")
|
||||||
|
}
|
||||||
|
sql += ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
sql += ";"
|
sql += ";"
|
||||||
|
|
11
species.go
11
species.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -67,12 +68,16 @@ func (s SpeciesService) unmarshal(b []byte) (entity, error) {
|
||||||
return sj.Species, err
|
return sj.Species, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SpeciesService) list(opt *ListOptions) (entity, error) {
|
func (s SpeciesService) list(val *url.Values) (entity, error) {
|
||||||
if opt == nil {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, errors.New("must provide options")
|
||||||
}
|
}
|
||||||
var vals []interface{}
|
var opt ListOptions
|
||||||
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var vals []interface{}
|
||||||
sql := `SELECT sp.*, g.genus_name, array_agg(st.id) AS strains,
|
sql := `SELECT sp.*, 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
|
||||||
|
|
11
strains.go
11
strains.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -68,12 +69,16 @@ func (s StrainService) unmarshal(b []byte) (entity, error) {
|
||||||
return sj.Strain, err
|
return sj.Strain, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s StrainService) list(opt *ListOptions) (entity, error) {
|
func (s StrainService) list(val *url.Values) (entity, error) {
|
||||||
if opt == nil {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, errors.New("must provide options")
|
||||||
}
|
}
|
||||||
var vals []interface{}
|
var opt ListOptions
|
||||||
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var vals []interface{}
|
||||||
sql := `SELECT st.*, array_agg(m.id) AS measurements, COUNT(m) AS total_measurements
|
sql := `SELECT st.*, array_agg(m.id) AS 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
|
||||||
|
|
Reference in a new issue