sort order: strains, species, char type, chars

This commit is contained in:
Matthew Dillon 2015-06-18 09:33:17 -08:00
parent 22e4bbe1e4
commit 8e386c3521
5 changed files with 16 additions and 9 deletions

View file

@ -30,6 +30,7 @@ type CharacteristicTypeBase struct {
type CharacteristicType struct {
*CharacteristicTypeBase
Characteristics NullSliceInt64 `db:"characteristics" json:"characteristics"`
SortOrder int64 `db:"sort_order" json:"sortOrder"`
}
type CharacteristicTypes []*CharacteristicType
@ -60,7 +61,8 @@ func (c CharacteristicTypeService) list(val *url.Values) (entity, error) {
}
var vals []interface{}
sql := `SELECT ct.*, array_agg(c.id) AS characteristics
sql := `SELECT ct.*, array_agg(c.id) AS characteristics,
rank() OVER (ORDER BY ct.characteristic_type_name) AS sort_order
FROM characteristic_types ct
INNER JOIN characteristics c ON c.characteristic_type_id=ct.id`
@ -89,7 +91,7 @@ func (c CharacteristicTypeService) list(val *url.Values) (entity, error) {
func (c CharacteristicTypeService) get(id int64, dummy string) (entity, error) {
var characteristic_type CharacteristicType
sql := `SELECT ct.*, array_agg(c.id) AS characteristics
sql := `SELECT ct.*, array_agg(c.id) AS characteristics, 0 AS sort_order
FROM characteristic_types ct
INNER JOIN characteristics c ON c.characteristic_type_id=ct.id
WHERE ct.id=$1

View file

@ -20,7 +20,7 @@ type CharacteristicBase struct {
Id int64 `json:"id,omitempty"`
CharacteristicName string `db:"characteristic_name" json:"characteristicName"`
CharacteristicTypeId int64 `db:"characteristic_type_id" json:"characteristicType"`
Sort NullInt64 `db:"sort" json:"sort"`
SortOrder NullInt64 `db:"sort_order" json:"sortOrder"`
CreatedAt time.Time `db:"created_at" json:"createdAt"`
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`

View file

@ -5,7 +5,7 @@ CREATE TABLE characteristics (
id BIGSERIAL NOT NULL,
characteristic_name TEXT NOT NULL,
characteristic_type_id BIGINT NOT NULL,
sort BIGINT NULL,
sort_order BIGINT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,

View file

@ -42,6 +42,7 @@ type Species struct {
GenusName string `db:"genus_name" json:"genusName"`
Strains NullSliceInt64 `db:"strains" json:"strains"`
TotalStrains int64 `db:"total_strains" json:"totalStrains"`
SortOrder int64 `db:"sort_order" json:"sortOrder"`
}
type ManySpecies []*Species
@ -79,7 +80,8 @@ func (s SpeciesService) list(val *url.Values) (entity, error) {
var vals []interface{}
sql := `SELECT sp.*, g.genus_name, array_agg(st.id) AS strains,
COUNT(st) AS total_strains
COUNT(st) AS total_strains,
rank() OVER (ORDER BY sp.species_name ASC) AS sort_order
FROM species sp
INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1
LEFT OUTER JOIN strains st ON st.species_id=sp.id`
@ -110,7 +112,7 @@ func (s SpeciesService) list(val *url.Values) (entity, error) {
func (s SpeciesService) get(id int64, genus string) (entity, error) {
var species Species
q := `SELECT sp.*, g.genus_name, array_agg(st.id) AS strains,
COUNT(st) AS total_strains
COUNT(st) AS total_strains, 0 AS sort_order
FROM species sp
INNER JOIN genera g ON g.id=sp.genus_id AND LOWER(g.genus_name)=$1
LEFT OUTER JOIN strains st ON st.species_id=sp.id

View file

@ -44,6 +44,7 @@ type Strain struct {
*StrainBase
Measurements NullSliceInt64 `db:"measurements" json:"measurements"`
TotalMeasurements int64 `db:"total_measurements" json:"totalMeasurements"`
SortOrder int64 `db:"sort_order" json:"sortOrder"`
}
type Strains []*Strain
@ -80,7 +81,8 @@ func (s StrainService) list(val *url.Values) (entity, error) {
}
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,
rank() OVER (ORDER BY sp.species_name ASC, st.type_strain ASC, st.strain_name ASC) AS sort_order
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
@ -99,7 +101,7 @@ func (s StrainService) list(val *url.Values) (entity, error) {
sql += " WHERE (" + strings.Join(conds, ") AND (") + ")"
}
sql += " GROUP BY st.id, st.species_id;"
sql += " GROUP BY st.id, st.species_id, sp.species_name;"
strains := make(Strains, 0)
err := DBH.Select(&strains, sql, vals...)
@ -111,7 +113,8 @@ func (s StrainService) list(val *url.Values) (entity, error) {
func (s StrainService) get(id int64, genus string) (entity, error) {
var strain Strain
q := `SELECT st.*, array_agg(m.id) AS measurements, COUNT(m) AS total_measurements
q := `SELECT st.*, array_agg(m.id) AS measurements, COUNT(m) AS total_measurements,
0 AS sort_order
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