Rough in characteristic_type

This commit is contained in:
Matthew Dillon 2015-06-16 15:25:44 -08:00
parent 6d3dfc88bf
commit f469d9514b
4 changed files with 118 additions and 13 deletions

101
characteristic_types.go Normal file
View file

@ -0,0 +1,101 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"time"
)
func init() {
DB.AddTableWithName(CharacteristicTypeBase{}, "characteristic_types").SetKeys(true, "Id")
}
type CharacteristicTypeService struct{}
// A CharacteristicType is a lookup type
type CharacteristicTypeBase struct {
Id int64 `json:"id,omitempty"`
CharacteristicTypeName string `db:"characteristic_type_name" json:"characteristicTypeName"`
CreatedAt time.Time `db:"created_at" json:"createdAt"`
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
CreatedBy int64 `db:"created_by" json:"createdBy"`
UpdatedBy int64 `db:"updated_by" json:"updatedBy"`
DeletedBy NullInt64 `db:"deleted_by" json:"deletedBy"`
}
type CharacteristicType struct {
*CharacteristicTypeBase
Characteristics NullSliceInt64 `db:"characteristics" json:"characteristics"`
}
type CharacteristicTypes []*CharacteristicType
type CharacteristicTypeJSON struct {
CharacteristicType *CharacteristicType `json:"characteristicType"`
}
type CharacteristicTypesJSON struct {
CharacteristicTypes *CharacteristicTypes `json:"characteristicTypes"`
}
func (c *CharacteristicType) marshal() ([]byte, error) {
return json.Marshal(&CharacteristicTypeJSON{CharacteristicType: c})
}
func (c *CharacteristicTypes) marshal() ([]byte, error) {
return json.Marshal(&CharacteristicTypesJSON{CharacteristicTypes: c})
}
func (c CharacteristicTypeService) list(val *url.Values) (entity, error) {
if val == nil {
return nil, errors.New("must provide options")
}
var opt ListOptions
if err := schemaDecoder.Decode(&opt, *val); err != nil {
return nil, err
}
var vals []interface{}
sql := `SELECT ct.*, array_agg(c.id) AS characteristics
FROM characteristic_types ct
INNER JOIN characteristics c ON c.characteristic_type_id=ct.id`
if len(opt.Ids) != 0 {
var conds []string
c := "c.id IN ("
for i, id := range opt.Ids {
c = c + fmt.Sprintf("$%v,", i+1) // start param index at 1
vals = append(vals, id)
}
c = c[:len(c)-1] + ")"
conds = append(conds, c)
sql += " WHERE (" + strings.Join(conds, ") AND (") + ")"
}
sql += " GROUP BY ct.id;"
var characteristic_types CharacteristicTypes
err := DBH.Select(&characteristic_types, sql, vals...)
if err != nil {
return nil, err
}
return &characteristic_types, nil
}
func (c CharacteristicTypeService) get(id int64, dummy string) (entity, error) {
var characteristic_type CharacteristicType
sql := `SELECT ct.*, array_agg(c.id) AS characteristics
FROM characteristic_types ct
INNER JOIN characteristics c ON c.characteristic_type_id=ct.id
WHERE ct.id=$1
GROUP BY ct.id;`
if err := DBH.SelectOne(&characteristic_type, sql, id); err != nil {
return nil, err
}
return &characteristic_type, nil
}

View file

@ -19,7 +19,7 @@ type CharacteristicService struct{}
type CharacteristicBase struct { type CharacteristicBase struct {
Id int64 `json:"id,omitempty"` Id int64 `json:"id,omitempty"`
CharacteristicName string `db:"characteristic_name" json:"characteristicName"` CharacteristicName string `db:"characteristic_name" json:"characteristicName"`
CharacteristicTypeId int64 `db:"characteristic_type_id" json:"-"` CharacteristicTypeId int64 `db:"characteristic_type_id" json:"characteristicType"`
Sort NullInt64 `db:"sort" json:"sort"` Sort NullInt64 `db:"sort" json:"sort"`
CreatedAt time.Time `db:"created_at" json:"createdAt"` CreatedAt time.Time `db:"created_at" json:"createdAt"`
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"` UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
@ -31,9 +31,8 @@ type CharacteristicBase struct {
type Characteristic struct { type Characteristic struct {
*CharacteristicBase *CharacteristicBase
Measurements NullSliceInt64 `db:"measurements" json:"measurements"` Measurements NullSliceInt64 `db:"measurements" json:"measurements"`
Strains NullSliceInt64 `db:"strains" json:"strains"` Strains NullSliceInt64 `db:"strains" json:"strains"`
CharacteristicTypeName string `db:"characteristic_type_name" json:"characteristicType"`
} }
type Characteristics []*Characteristic type Characteristics []*Characteristic
@ -64,10 +63,8 @@ func (c CharacteristicService) list(val *url.Values) (entity, error) {
} }
var vals []interface{} var vals []interface{}
sql := `SELECT c.*, ct.characteristic_type_name, sql := `SELECT c.*, array_agg(m.id) AS measurements, array_agg(st.id) AS strains
array_agg(m.id) AS measurements, array_agg(st.id) AS strains
FROM characteristics c FROM characteristics c
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`
@ -84,7 +81,7 @@ func (c CharacteristicService) list(val *url.Values) (entity, error) {
sql += " WHERE (" + strings.Join(conds, ") AND (") + ")" sql += " WHERE (" + strings.Join(conds, ") AND (") + ")"
} }
sql += " GROUP BY c.id, ct.characteristic_type_name;" sql += " GROUP BY c.id;"
var characteristics Characteristics var characteristics Characteristics
err := DBH.Select(&characteristics, sql, vals...) err := DBH.Select(&characteristics, sql, vals...)
@ -96,14 +93,12 @@ func (c CharacteristicService) list(val *url.Values) (entity, error) {
func (c CharacteristicService) get(id int64, dummy string) (entity, error) { func (c CharacteristicService) get(id int64, dummy string) (entity, error) {
var characteristic Characteristic var characteristic Characteristic
sql := `SELECT c.*, ct.characteristic_type_name, sql := `SELECT c.*, array_agg(m.id) AS measurements, array_agg(st.id) AS strains
array_agg(m.id) AS measurements, array_agg(st.id) AS strains
FROM characteristics c FROM characteristics c
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
WHERE c.id=$1 WHERE c.id=$1
GROUP BY c.id, ct.characteristic_type_name;` GROUP BY c.id;`
if err := DBH.SelectOne(&characteristic, sql, id); err != nil { if err := DBH.SelectOne(&characteristic, sql, id); err != nil {
return nil, err return nil, err
} }

View file

@ -97,6 +97,8 @@ func Handler() http.Handler {
r{handleCreater(SpeciesService{}), "POST", "/species"}, r{handleCreater(SpeciesService{}), "POST", "/species"},
r{handleGetter(SpeciesService{}), "GET", "/species/{Id:.+}"}, r{handleGetter(SpeciesService{}), "GET", "/species/{Id:.+}"},
r{handleUpdater(SpeciesService{}), "PUT", "/species/{Id:.+}"}, r{handleUpdater(SpeciesService{}), "PUT", "/species/{Id:.+}"},
r{handleLister(CharacteristicTypeService{}), "GET", "/characteristicTypes"},
r{handleGetter(CharacteristicTypeService{}), "GET", "/characteristicTypes/{Id:.+}"},
} }
for _, route := range routes { for _, route := range routes {

View file

@ -9,6 +9,13 @@ CREATE TABLE characteristic_types (
updated_at TIMESTAMP WITH TIME ZONE NOT NULL, updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
deleted_at TIMESTAMP WITH TIME ZONE NULL, deleted_at TIMESTAMP WITH TIME ZONE NULL,
CONSTRAINT characteristic_types_pkey PRIMARY KEY (id) created_by BIGINT NOT NULL,
updated_by BIGINT NOT NULL,
deleted_by BIGINT NULL,
CONSTRAINT characteristic_types_pkey PRIMARY KEY (id),
FOREIGN KEY (created_by) REFERENCES users(id),
FOREIGN KEY (updated_by) REFERENCES users(id),
FOREIGN KEY (deleted_by) REFERENCES users(id)
); );