Ember data: convey genera/species relationships
This commit is contained in:
parent
52b21b24d8
commit
1dbfb3bc54
7 changed files with 62 additions and 20 deletions
|
@ -11,7 +11,7 @@ import (
|
|||
)
|
||||
|
||||
// A Genus is a high-level classifier in bactdb.
|
||||
type Genus struct {
|
||||
type GenusBase struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
GenusName string `db:"genus_name" json:"genusName"`
|
||||
CreatedAt time.Time `db:"created_at" json:"createdAt"`
|
||||
|
@ -19,6 +19,11 @@ type Genus struct {
|
|||
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
|
||||
}
|
||||
|
||||
type Genus struct {
|
||||
*GenusBase
|
||||
Species NullSliceInt64 `db:"species" json:"species"`
|
||||
}
|
||||
|
||||
type GenusJSON struct {
|
||||
Genus *Genus `json:"genus"`
|
||||
}
|
||||
|
@ -31,8 +36,12 @@ func (m *Genus) String() string {
|
|||
return fmt.Sprintf("%v", *m)
|
||||
}
|
||||
|
||||
func (m *GenusBase) String() string {
|
||||
return fmt.Sprintf("%v", *m)
|
||||
}
|
||||
|
||||
func NewGenus() *Genus {
|
||||
return &Genus{GenusName: "Test Genus"}
|
||||
return &Genus{&GenusBase{GenusName: "Test Genus"}, make([]int64, 0)}
|
||||
}
|
||||
|
||||
// GeneraService interacts with the genus-related endpoints in bactdb's API.
|
||||
|
|
|
@ -54,7 +54,7 @@ func TestGeneraService_Create(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.CreateGenus, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "POST")
|
||||
testBody(t, r, `{"genus":{"id":1,"genusName":"Test Genus","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
|
||||
testBody(t, r, `{"genus":{"id":1,"genusName":"Test Genus","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null,"species":[]}}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
|
@ -124,7 +124,7 @@ func TestGeneraService_Update(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.UpdateGenus, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"genus":{"id":1,"genusName":"Test Genus Updated","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
|
||||
testBody(t, r, `{"genus":{"id":1,"genusName":"Test Genus Updated","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null,"species":[]}}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
// A Species is a high-level classifier in bactdb.
|
||||
type Species struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
GenusId int64 `db:"genus_id" json:"genusId"`
|
||||
GenusId int64 `db:"genus_id" json:"genus"`
|
||||
SpeciesName string `db:"species_name" json:"speciesName"`
|
||||
CreatedAt time.Time `db:"created_at" json:"createdAt"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
|
||||
|
|
|
@ -55,7 +55,7 @@ func TestSpeciesService_Create(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.CreateSpecies, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "POST")
|
||||
testBody(t, r, `{"species":{"id":1,"genusId":1,"speciesName":"Test Species","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
|
||||
testBody(t, r, `{"species":{"id":1,"genus":1,"speciesName":"Test Species","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
|
@ -124,7 +124,7 @@ func TestSpeciesService_Update(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.UpdateSpecies, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"species":{"id":1,"genusId":1,"speciesName":"Test Species Updated","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
|
||||
testBody(t, r, `{"species":{"id":1,"genus":1,"speciesName":"Test Species Updated","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
|
|
|
@ -4,6 +4,9 @@ import (
|
|||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
|
@ -129,3 +132,27 @@ func (t *NullTime) UnmarshalJSON(b []byte) error {
|
|||
t.Valid = true
|
||||
return err
|
||||
}
|
||||
|
||||
type NullSliceInt64 []int64
|
||||
|
||||
func (i *NullSliceInt64) Scan(src interface{}) error {
|
||||
asBytes, ok := src.([]byte)
|
||||
if !ok {
|
||||
return errors.New("Scan source was not []byte")
|
||||
}
|
||||
asString := string(asBytes)
|
||||
(*i) = strToIntSlice(asString)
|
||||
return nil
|
||||
}
|
||||
|
||||
func strToIntSlice(s string) []int64 {
|
||||
r := strings.Trim(s, "{}")
|
||||
a := []int64(nil)
|
||||
if r != "NULL" {
|
||||
for _, t := range strings.Split(r, ",") {
|
||||
i, _ := strconv.ParseInt(t, 10, 64)
|
||||
a = append(a, i)
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
|
Reference in a new issue