Filter measurements + in helper
This commit is contained in:
parent
616f891704
commit
6d3dfc88bf
2 changed files with 40 additions and 37 deletions
28
helpers.go
28
helpers.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
|
@ -8,24 +9,24 @@ import (
|
||||||
|
|
||||||
// ListOptions specifies general pagination options for fetching a list of results
|
// ListOptions specifies general pagination options for fetching a list of results
|
||||||
type ListOptions struct {
|
type ListOptions struct {
|
||||||
PerPage int `url:",omitempty" json:",omitempty"`
|
PerPage int64 `url:",omitempty" json:",omitempty"`
|
||||||
Page int `url:",omitempty" json:",omitempty"`
|
Page int64 `url:",omitempty" json:",omitempty"`
|
||||||
Ids []int `url:",omitempty" json:",omitempty" schema:"ids[]"`
|
Ids []int64 `url:",omitempty" json:",omitempty" schema:"ids[]"`
|
||||||
Genus string
|
Genus string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o ListOptions) PageOrDefault() int {
|
func (o ListOptions) PageOrDefault() int64 {
|
||||||
if o.Page <= 0 {
|
if o.Page <= 0 {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
return o.Page
|
return o.Page
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o ListOptions) Offset() int {
|
func (o ListOptions) Offset() int64 {
|
||||||
return (o.PageOrDefault() - 1) * o.PerPageOrDefault()
|
return (o.PageOrDefault() - 1) * o.PerPageOrDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o ListOptions) PerPageOrDefault() int {
|
func (o ListOptions) PerPageOrDefault() int64 {
|
||||||
if o.PerPage <= 0 {
|
if o.PerPage <= 0 {
|
||||||
return DefaultPerPage
|
return DefaultPerPage
|
||||||
}
|
}
|
||||||
|
@ -43,3 +44,18 @@ func currentTime() NullTime {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func valsIn(attribute string, values []int64, vals *[]interface{}, counter *int64) string {
|
||||||
|
if len(values) == 1 {
|
||||||
|
return fmt.Sprintf("%v=%v", attribute, values[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
m := fmt.Sprintf("%v IN (", attribute)
|
||||||
|
for _, id := range values {
|
||||||
|
m = m + fmt.Sprintf("$%v,", *counter)
|
||||||
|
*vals = append(*vals, id)
|
||||||
|
*counter++
|
||||||
|
}
|
||||||
|
m = m[:len(m)-1] + ")"
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
|
@ -3,9 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -70,8 +68,8 @@ func (m MeasurementService) list(val *url.Values) (entity, error) {
|
||||||
}
|
}
|
||||||
var opt struct {
|
var opt struct {
|
||||||
ListOptions
|
ListOptions
|
||||||
Strain *int64
|
Strains []int64 `schema:"strain[]"`
|
||||||
Characteristic *int64
|
Characteristics []int64 `schema:"characteristic[]"`
|
||||||
}
|
}
|
||||||
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -90,49 +88,38 @@ func (m MeasurementService) list(val *url.Values) (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)
|
||||||
|
|
||||||
strainId := opt.Strain != nil
|
strainIds := len(opt.Strains) != 0
|
||||||
charId := opt.Characteristic != nil
|
charIds := len(opt.Characteristics) != 0
|
||||||
ids := len(opt.Ids) != 0
|
ids := len(opt.Ids) != 0
|
||||||
|
|
||||||
if strainId || charId || ids {
|
if strainIds || charIds || ids {
|
||||||
paramsCounter := 2
|
var paramsCounter int64 = 2
|
||||||
sql += "\nWHERE ("
|
sql += "\nWHERE ("
|
||||||
|
|
||||||
// Filter by strain
|
// Filter by strains
|
||||||
if strainId {
|
if strainIds {
|
||||||
sql += fmt.Sprintf("st.id=$%v", paramsCounter)
|
sStr := valsIn("st.id", opt.Strains, &vals, ¶msCounter)
|
||||||
vals = append(vals, *opt.Strain)
|
sql += sStr
|
||||||
paramsCounter++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if strainId && (charId || ids) {
|
if strainIds && (charIds || ids) {
|
||||||
sql += " AND "
|
sql += " AND "
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by characteristic
|
// Filter by characteristics
|
||||||
if charId {
|
if charIds {
|
||||||
sql += fmt.Sprintf("c.id=$%v", paramsCounter)
|
sChar := valsIn("c.id", opt.Characteristics, &vals, ¶msCounter)
|
||||||
vals = append(vals, *opt.Characteristic)
|
sql += sChar
|
||||||
paramsCounter++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if charId && ids {
|
if charIds && ids {
|
||||||
sql += " AND "
|
sql += " AND "
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get specific records
|
// Get specific records
|
||||||
if ids {
|
if ids {
|
||||||
var conds []string
|
sId := valsIn("m.id", opt.Ids, &vals, ¶msCounter)
|
||||||
|
sql += sId
|
||||||
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 += ")"
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue