diff --git a/characteristic_types.go b/characteristic_types.go index 2c5b1c4..5cce602 100644 --- a/characteristic_types.go +++ b/characteristic_types.go @@ -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 diff --git a/characteristics.go b/characteristics.go index 8070765..b323c32 100644 --- a/characteristics.go +++ b/characteristics.go @@ -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"` diff --git a/migrations/00006_AddCharacteristics_up.sql b/migrations/00006_AddCharacteristics_up.sql index 1c369e8..62b7d4f 100644 --- a/migrations/00006_AddCharacteristics_up.sql +++ b/migrations/00006_AddCharacteristics_up.sql @@ -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, diff --git a/species.go b/species.go index 2003447..fed8894 100644 --- a/species.go +++ b/species.go @@ -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 diff --git a/strains.go b/strains.go index 43c5ccb..ea9e2c1 100644 --- a/strains.go +++ b/strains.go @@ -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