Species changes for ember data.

This commit is contained in:
Matthew Dillon 2015-01-15 16:51:40 -09:00
parent 3e3e9d6e52
commit eb7c778be8
3 changed files with 25 additions and 17 deletions

View file

@ -20,17 +20,17 @@ func serveSpecies(w http.ResponseWriter, r *http.Request) error {
return err return err
} }
return writeJSON(w, species) return writeJSON(w, models.SpeciesJSON{species})
} }
func serveCreateSpecies(w http.ResponseWriter, r *http.Request) error { func serveCreateSpecies(w http.ResponseWriter, r *http.Request) error {
var species models.Species var species models.SpeciesJSON
err := json.NewDecoder(r.Body).Decode(&species) err := json.NewDecoder(r.Body).Decode(&species)
if err != nil { if err != nil {
return err return err
} }
created, err := store.Species.Create(&species) created, err := store.Species.Create(species.Species)
if err != nil { if err != nil {
return err return err
} }
@ -55,18 +55,18 @@ func serveSpeciesList(w http.ResponseWriter, r *http.Request) error {
species = []*models.Species{} species = []*models.Species{}
} }
return writeJSON(w, species) return writeJSON(w, models.SpeciesListJSON{Species: species})
} }
func serveUpdateSpecies(w http.ResponseWriter, r *http.Request) error { func serveUpdateSpecies(w http.ResponseWriter, r *http.Request) error {
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
var species models.Species var species models.SpeciesJSON
err := json.NewDecoder(r.Body).Decode(&species) err := json.NewDecoder(r.Body).Decode(&species)
if err != nil { if err != nil {
return err return err
} }
updated, err := store.Species.Update(id, &species) updated, err := store.Species.Update(id, species.Species)
if err != nil { if err != nil {
return err return err
} }
@ -107,5 +107,5 @@ func serveSubrouterSpeciesList(w http.ResponseWriter, r *http.Request) error {
species = []*models.Species{} species = []*models.Species{}
} }
return writeJSON(w, species) return writeJSON(w, models.SpeciesListJSON{Species: species})
} }

View file

@ -20,6 +20,14 @@ type Species struct {
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"` DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
} }
type SpeciesJSON struct {
Species *Species `json:"species"`
}
type SpeciesListJSON struct {
Species []*Species `json:"species"`
}
func (m *Species) String() string { func (m *Species) String() string {
return fmt.Sprintf("%v", *m) return fmt.Sprintf("%v", *m)
} }
@ -68,13 +76,13 @@ func (s *speciesService) Get(id int64) (*Species, error) {
return nil, err return nil, err
} }
var species *Species var species *SpeciesJSON
_, err = s.client.Do(req, &species) _, err = s.client.Do(req, &species)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return species, nil return species.Species, nil
} }
func (s *speciesService) Create(species *Species) (bool, error) { func (s *speciesService) Create(species *Species) (bool, error) {
@ -83,7 +91,7 @@ func (s *speciesService) Create(species *Species) (bool, error) {
return false, err return false, err
} }
req, err := s.client.NewRequest("POST", url.String(), species) req, err := s.client.NewRequest("POST", url.String(), SpeciesJSON{Species: species})
if err != nil { if err != nil {
return false, err return false, err
} }
@ -112,13 +120,13 @@ func (s *speciesService) List(opt *SpeciesListOptions) ([]*Species, error) {
return nil, err return nil, err
} }
var species []*Species var species *SpeciesListJSON
_, err = s.client.Do(req, &species) _, err = s.client.Do(req, &species)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return species, nil return species.Species, nil
} }
func (s *speciesService) Update(id int64, species *Species) (bool, error) { func (s *speciesService) Update(id int64, species *Species) (bool, error) {
@ -129,7 +137,7 @@ func (s *speciesService) Update(id int64, species *Species) (bool, error) {
return false, err return false, err
} }
req, err := s.client.NewRequest("PUT", url.String(), species) req, err := s.client.NewRequest("PUT", url.String(), SpeciesJSON{Species: species})
if err != nil { if err != nil {
return false, err return false, err
} }

View file

@ -26,7 +26,7 @@ func TestSpeciesService_Get(t *testing.T) {
called = true called = true
testMethod(t, r, "GET") testMethod(t, r, "GET")
writeJSON(w, want) writeJSON(w, SpeciesJSON{Species: want})
}) })
species, err := client.Species.Get(want.Id) species, err := client.Species.Get(want.Id)
@ -55,7 +55,7 @@ func TestSpeciesService_Create(t *testing.T) {
mux.HandleFunc(urlPath(t, router.CreateSpecies, nil), func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc(urlPath(t, router.CreateSpecies, nil), func(w http.ResponseWriter, r *http.Request) {
called = true called = true
testMethod(t, r, "POST") testMethod(t, r, "POST")
testBody(t, r, `{"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,"genusId":1,"speciesName":"Test Species","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
writeJSON(w, want) writeJSON(w, want)
@ -93,7 +93,7 @@ func TestSpeciesService_List(t *testing.T) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
testFormValues(t, r, values{}) testFormValues(t, r, values{})
writeJSON(w, want) writeJSON(w, SpeciesListJSON{Species: want})
}) })
species, err := client.Species.List(nil) species, err := client.Species.List(nil)
@ -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) { mux.HandleFunc(urlPath(t, router.UpdateSpecies, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true called = true
testMethod(t, r, "PUT") testMethod(t, r, "PUT")
testBody(t, r, `{"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,"genusId":1,"speciesName":"Test Species Updated","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
writeJSON(w, want) writeJSON(w, want)