From f469d9514b54acbf8ce95bd386649ac17285adee Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Tue, 16 Jun 2015 15:25:44 -0800 Subject: [PATCH] Rough in characteristic_type --- characteristic_types.go | 101 ++++++++++++++++++ characteristics.go | 19 ++-- handlers.go | 2 + .../00005_AddCharacteristicTypes_up.sql | 9 +- 4 files changed, 118 insertions(+), 13 deletions(-) create mode 100644 characteristic_types.go diff --git a/characteristic_types.go b/characteristic_types.go new file mode 100644 index 0000000..26c87d1 --- /dev/null +++ b/characteristic_types.go @@ -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 +} diff --git a/characteristics.go b/characteristics.go index 5a7d7b3..dedc647 100644 --- a/characteristics.go +++ b/characteristics.go @@ -19,7 +19,7 @@ type CharacteristicService struct{} type CharacteristicBase struct { Id int64 `json:"id,omitempty"` 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"` CreatedAt time.Time `db:"created_at" json:"createdAt"` UpdatedAt time.Time `db:"updated_at" json:"updatedAt"` @@ -31,9 +31,8 @@ type CharacteristicBase struct { type Characteristic struct { *CharacteristicBase - Measurements NullSliceInt64 `db:"measurements" json:"measurements"` - Strains NullSliceInt64 `db:"strains" json:"strains"` - CharacteristicTypeName string `db:"characteristic_type_name" json:"characteristicType"` + Measurements NullSliceInt64 `db:"measurements" json:"measurements"` + Strains NullSliceInt64 `db:"strains" json:"strains"` } type Characteristics []*Characteristic @@ -64,10 +63,8 @@ func (c CharacteristicService) list(val *url.Values) (entity, error) { } var vals []interface{} - sql := `SELECT c.*, ct.characteristic_type_name, - array_agg(m.id) AS measurements, array_agg(st.id) AS strains + sql := `SELECT c.*, array_agg(m.id) AS measurements, array_agg(st.id) AS strains 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 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 += " GROUP BY c.id, ct.characteristic_type_name;" + sql += " GROUP BY c.id;" var characteristics Characteristics 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) { var characteristic Characteristic - sql := `SELECT c.*, ct.characteristic_type_name, - array_agg(m.id) AS measurements, array_agg(st.id) AS strains + sql := `SELECT c.*, array_agg(m.id) AS measurements, array_agg(st.id) AS strains 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 strains st ON st.id=m.strain_id 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 { return nil, err } diff --git a/handlers.go b/handlers.go index 16d497a..1a609b0 100644 --- a/handlers.go +++ b/handlers.go @@ -97,6 +97,8 @@ func Handler() http.Handler { r{handleCreater(SpeciesService{}), "POST", "/species"}, r{handleGetter(SpeciesService{}), "GET", "/species/{Id:.+}"}, r{handleUpdater(SpeciesService{}), "PUT", "/species/{Id:.+}"}, + r{handleLister(CharacteristicTypeService{}), "GET", "/characteristicTypes"}, + r{handleGetter(CharacteristicTypeService{}), "GET", "/characteristicTypes/{Id:.+}"}, } for _, route := range routes { diff --git a/migrations/00005_AddCharacteristicTypes_up.sql b/migrations/00005_AddCharacteristicTypes_up.sql index 4e6de7c..42222dc 100644 --- a/migrations/00005_AddCharacteristicTypes_up.sql +++ b/migrations/00005_AddCharacteristicTypes_up.sql @@ -9,6 +9,13 @@ CREATE TABLE characteristic_types ( updated_at TIMESTAMP WITH TIME ZONE NOT 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) );