Ember data: characteristics
This commit is contained in:
parent
a37a5a9400
commit
41ee2857ee
7 changed files with 56 additions and 35 deletions
|
@ -20,17 +20,17 @@ func serveCharacteristic(w http.ResponseWriter, r *http.Request) error {
|
|||
return err
|
||||
}
|
||||
|
||||
return writeJSON(w, characteristic)
|
||||
return writeJSON(w, models.CharacteristicJSON{Characteristic: characteristic})
|
||||
}
|
||||
|
||||
func serveCreateCharacteristic(w http.ResponseWriter, r *http.Request) error {
|
||||
var characteristic models.Characteristic
|
||||
var characteristic models.CharacteristicJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&characteristic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
created, err := store.Characteristics.Create(&characteristic)
|
||||
created, err := store.Characteristics.Create(characteristic.Characteristic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -55,18 +55,18 @@ func serveCharacteristicList(w http.ResponseWriter, r *http.Request) error {
|
|||
characteristics = []*models.Characteristic{}
|
||||
}
|
||||
|
||||
return writeJSON(w, characteristics)
|
||||
return writeJSON(w, models.CharacteristicsJSON{Characteristics: characteristics})
|
||||
}
|
||||
|
||||
func serveUpdateCharacteristic(w http.ResponseWriter, r *http.Request) error {
|
||||
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
var characteristic models.Characteristic
|
||||
var characteristic models.CharacteristicJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&characteristic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, err := store.Characteristics.Update(id, &characteristic)
|
||||
updated, err := store.Characteristics.Update(id, characteristic.Characteristic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -88,5 +88,5 @@ func serveDeleteCharacteristic(w http.ResponseWriter, r *http.Request) error {
|
|||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
return writeJSON(w, &models.Characteristic{})
|
||||
return writeJSON(w, nil)
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
DB.AddTableWithName(models.Characteristic{}, "characteristics").SetKeys(true, "Id")
|
||||
DB.AddTableWithName(models.CharacteristicBase{}, "characteristics").SetKeys(true, "Id")
|
||||
}
|
||||
|
||||
type characteristicsStore struct {
|
||||
|
@ -16,7 +16,8 @@ type characteristicsStore struct {
|
|||
|
||||
func (s *characteristicsStore) Get(id int64) (*models.Characteristic, error) {
|
||||
var characteristic models.Characteristic
|
||||
if err := s.dbh.SelectOne(&characteristic, `SELECT * FROM characteristics WHERE id=$1;`, id); err != nil {
|
||||
err := s.dbh.SelectOne(&characteristic, `SELECT c.*, array_agg(m.id) AS measurements FROM characteristics c LEFT OUTER JOIN measurements m ON m.characteristic_id=c.id WHERE c.id=$1 GROUP BY c.id;`, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if &characteristic == nil {
|
||||
|
@ -29,9 +30,11 @@ func (s *characteristicsStore) Create(characteristic *models.Characteristic) (bo
|
|||
currentTime := time.Now()
|
||||
characteristic.CreatedAt = currentTime
|
||||
characteristic.UpdatedAt = currentTime
|
||||
if err := s.dbh.Insert(characteristic); err != nil {
|
||||
base := characteristic.CharacteristicBase
|
||||
if err := s.dbh.Insert(base); err != nil {
|
||||
return false, err
|
||||
}
|
||||
characteristic.Id = base.Id
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
@ -40,7 +43,7 @@ func (s *characteristicsStore) List(opt *models.CharacteristicListOptions) ([]*m
|
|||
opt = &models.CharacteristicListOptions{}
|
||||
}
|
||||
var characteristics []*models.Characteristic
|
||||
err := s.dbh.Select(&characteristics, `SELECT * FROM characteristics LIMIT $1 OFFSET $2;`, opt.PerPageOrDefault(), opt.Offset())
|
||||
err := s.dbh.Select(&characteristics, `SELECT c.*, array_agg(m.id) AS measurements FROM characteristics c LEFT OUTER JOIN measurements m ON m.characteristic_id=c.id GROUP BY c.id;`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -58,7 +61,7 @@ func (s *characteristicsStore) Update(id int64, characteristic *models.Character
|
|||
}
|
||||
|
||||
characteristic.UpdatedAt = time.Now()
|
||||
changed, err := s.dbh.Update(characteristic)
|
||||
changed, err := s.dbh.Update(characteristic.CharacteristicBase)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -76,7 +79,7 @@ func (s *characteristicsStore) Delete(id int64) (bool, error) {
|
|||
return false, err
|
||||
}
|
||||
|
||||
deleted, err := s.dbh.Delete(characteristic)
|
||||
deleted, err := s.dbh.Delete(characteristic.CharacteristicBase)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
|
@ -11,17 +11,17 @@ import (
|
|||
func insertCharacteristic(t *testing.T, tx *modl.Transaction) *models.Characteristic {
|
||||
// clean up our target table
|
||||
tx.Exec(`DELETE FROM characteristics;`)
|
||||
characteristic := newCharacteristic(t, tx)
|
||||
if err := tx.Insert(characteristic); err != nil {
|
||||
c := newCharacteristic(t, tx)
|
||||
if err := tx.Insert(c); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return characteristic
|
||||
return &models.Characteristic{c, []int64(nil)}
|
||||
}
|
||||
|
||||
func newCharacteristic(t *testing.T, tx *modl.Transaction) *models.Characteristic {
|
||||
func newCharacteristic(t *testing.T, tx *modl.Transaction) *models.CharacteristicBase {
|
||||
// we want to create and insert an characteristic type record, too.
|
||||
characteristic_type := insertCharacteristicType(t, tx)
|
||||
return &models.Characteristic{CharacteristicName: "Test Characteristic",
|
||||
return &models.CharacteristicBase{CharacteristicName: "Test Characteristic",
|
||||
CharacteristicTypeId: characteristic_type.Id}
|
||||
}
|
||||
|
||||
|
@ -50,11 +50,12 @@ func TestCharacteristicsStore_Create_db(t *testing.T) {
|
|||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
characteristic := newCharacteristic(t, tx)
|
||||
base_characteristic := newCharacteristic(t, tx)
|
||||
characteristic := models.Characteristic{base_characteristic, []int64(nil)}
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
created, err := d.Characteristics.Create(characteristic)
|
||||
created, err := d.Characteristics.Create(&characteristic)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
)
|
||||
|
||||
// A Characteristic is a lookup type
|
||||
type Characteristic struct {
|
||||
type CharacteristicBase struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
CharacteristicName string `db:"characteristic_name" json:"characteristicName"`
|
||||
CharacteristicTypeId int64 `db:"characteristic_type_id" json:"characteristicTypeId"`
|
||||
|
@ -20,13 +20,29 @@ type Characteristic struct {
|
|||
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
|
||||
}
|
||||
|
||||
type Characteristic struct {
|
||||
*CharacteristicBase
|
||||
Measurements NullSliceInt64 `db:"measurements" json:"measurements"`
|
||||
}
|
||||
|
||||
type CharacteristicJSON struct {
|
||||
Characteristic *Characteristic `json:"characteristic"`
|
||||
}
|
||||
|
||||
type CharacteristicsJSON struct {
|
||||
Characteristics []*Characteristic `json:"characteristics"`
|
||||
}
|
||||
|
||||
func (m *Characteristic) String() string {
|
||||
return fmt.Sprintf("%v", *m)
|
||||
}
|
||||
|
||||
func NewCharacteristic() *Characteristic {
|
||||
return &Characteristic{
|
||||
&CharacteristicBase{
|
||||
CharacteristicName: "Test Characteristic",
|
||||
},
|
||||
make([]int64, 0),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,13 +84,13 @@ func (s *characteristicsService) Get(id int64) (*Characteristic, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var characteristic *Characteristic
|
||||
var characteristic *CharacteristicJSON
|
||||
_, err = s.client.Do(req, &characteristic)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return characteristic, nil
|
||||
return characteristic.Characteristic, nil
|
||||
}
|
||||
|
||||
func (s *characteristicsService) Create(characteristic *Characteristic) (bool, error) {
|
||||
|
@ -83,7 +99,7 @@ func (s *characteristicsService) Create(characteristic *Characteristic) (bool, e
|
|||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("POST", url.String(), characteristic)
|
||||
req, err := s.client.NewRequest("POST", url.String(), CharacteristicJSON{Characteristic: characteristic})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -111,13 +127,13 @@ func (s *characteristicsService) List(opt *CharacteristicListOptions) ([]*Charac
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var characteristics []*Characteristic
|
||||
var characteristics *CharacteristicsJSON
|
||||
_, err = s.client.Do(req, &characteristics)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return characteristics, nil
|
||||
return characteristics.Characteristics, nil
|
||||
}
|
||||
|
||||
func (s *characteristicsService) Update(id int64, characteristic *Characteristic) (bool, error) {
|
||||
|
@ -128,7 +144,7 @@ func (s *characteristicsService) Update(id int64, characteristic *Characteristic
|
|||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("PUT", url.String(), characteristic)
|
||||
req, err := s.client.NewRequest("PUT", url.String(), CharacteristicJSON{Characteristic: characteristic})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func TestCharacteristicService_Get(t *testing.T) {
|
|||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
|
||||
writeJSON(w, want)
|
||||
writeJSON(w, CharacteristicJSON{Characteristic: want})
|
||||
})
|
||||
|
||||
characteristic, err := client.Characteristics.Get(want.Id)
|
||||
|
@ -54,7 +54,7 @@ func TestCharacteristicService_Create(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.CreateCharacteristic, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "POST")
|
||||
testBody(t, r, `{"id":1,"characteristicName":"Test Characteristic","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
|
||||
testBody(t, r, `{"characteristic":{"id":1,"characteristicName":"Test Characteristic","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null,"measurements":[]}}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
|
@ -92,7 +92,7 @@ func TestCharacteristicService_List(t *testing.T) {
|
|||
testMethod(t, r, "GET")
|
||||
testFormValues(t, r, values{})
|
||||
|
||||
writeJSON(w, want)
|
||||
writeJSON(w, CharacteristicsJSON{Characteristics: want})
|
||||
})
|
||||
|
||||
characteristics, err := client.Characteristics.List(nil)
|
||||
|
@ -123,7 +123,7 @@ func TestCharacteristicService_Update(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.UpdateCharacteristic, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"id":1,"characteristicName":"Test Char Updated","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
|
||||
testBody(t, r, `{"characteristic":{"id":1,"characteristicName":"Test Char Updated","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null,"measurements":[]}}`+"\n")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
@ -154,6 +154,7 @@ func TestCharacteristicService_Delete(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.DeleteCharacteristic, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "DELETE")
|
||||
testBody(t, r, "")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
|
|
|
@ -18,7 +18,7 @@ import (
|
|||
type Measurement struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
StrainId int64 `db:"strain_id" json:"strain"`
|
||||
CharacteristicId int64 `db:"characteristic_id" json:"characteristicId"`
|
||||
CharacteristicId int64 `db:"characteristic_id" json:"characteristic"`
|
||||
TextMeasurementTypeId NullInt64 `db:"text_measurement_type_id" json:"textMeasurementTypeId"`
|
||||
TxtValue NullString `db:"txt_value" json:"txtValue"`
|
||||
NumValue NullFloat64 `db:"num_value" json:"numValue"`
|
||||
|
|
|
@ -59,7 +59,7 @@ func TestMeasurementService_Create(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.CreateMeasurement, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "POST")
|
||||
testBody(t, r, `{"measurement":{"id":1,"strain":1,"characteristicId":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":1.23,"confidenceInterval":null,"unitTypeId":1,"notes":"a note","testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}}`+"\n")
|
||||
testBody(t, r, `{"measurement":{"id":1,"strain":1,"characteristic":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":1.23,"confidenceInterval":null,"unitTypeId":1,"notes":"a note","testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
|
@ -128,7 +128,7 @@ func TestMeasurementService_Update(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.UpdateMeasurement, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"measurement":{"id":1,"strain":1,"characteristicId":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":4.56,"confidenceInterval":null,"unitTypeId":1,"notes":"a note","testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}}`+"\n")
|
||||
testBody(t, r, `{"measurement":{"id":1,"strain":1,"characteristic":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":4.56,"confidenceInterval":null,"unitTypeId":1,"notes":"a note","testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}}`+"\n")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
|
Reference in a new issue