From eb7c778be80dc8eb438bd1d8d9426d7d51b76f0f Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Thu, 15 Jan 2015 16:51:40 -0900 Subject: [PATCH] Species changes for ember data. --- api/species.go | 14 +++++++------- models/species.go | 20 ++++++++++++++------ models/species_test.go | 8 ++++---- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/api/species.go b/api/species.go index 83c3536..4d16135 100644 --- a/api/species.go +++ b/api/species.go @@ -20,17 +20,17 @@ func serveSpecies(w http.ResponseWriter, r *http.Request) error { return err } - return writeJSON(w, species) + return writeJSON(w, models.SpeciesJSON{species}) } func serveCreateSpecies(w http.ResponseWriter, r *http.Request) error { - var species models.Species + var species models.SpeciesJSON err := json.NewDecoder(r.Body).Decode(&species) if err != nil { return err } - created, err := store.Species.Create(&species) + created, err := store.Species.Create(species.Species) if err != nil { return err } @@ -55,18 +55,18 @@ func serveSpeciesList(w http.ResponseWriter, r *http.Request) error { species = []*models.Species{} } - return writeJSON(w, species) + return writeJSON(w, models.SpeciesListJSON{Species: species}) } func serveUpdateSpecies(w http.ResponseWriter, r *http.Request) error { id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) - var species models.Species + var species models.SpeciesJSON err := json.NewDecoder(r.Body).Decode(&species) if err != nil { return err } - updated, err := store.Species.Update(id, &species) + updated, err := store.Species.Update(id, species.Species) if err != nil { return err } @@ -107,5 +107,5 @@ func serveSubrouterSpeciesList(w http.ResponseWriter, r *http.Request) error { species = []*models.Species{} } - return writeJSON(w, species) + return writeJSON(w, models.SpeciesListJSON{Species: species}) } diff --git a/models/species.go b/models/species.go index c8afebc..91870a0 100644 --- a/models/species.go +++ b/models/species.go @@ -20,6 +20,14 @@ type Species struct { 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 { return fmt.Sprintf("%v", *m) } @@ -68,13 +76,13 @@ func (s *speciesService) Get(id int64) (*Species, error) { return nil, err } - var species *Species + var species *SpeciesJSON _, err = s.client.Do(req, &species) if err != nil { return nil, err } - return species, nil + return species.Species, nil } func (s *speciesService) Create(species *Species) (bool, error) { @@ -83,7 +91,7 @@ func (s *speciesService) Create(species *Species) (bool, error) { 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 { return false, err } @@ -112,13 +120,13 @@ func (s *speciesService) List(opt *SpeciesListOptions) ([]*Species, error) { return nil, err } - var species []*Species + var species *SpeciesListJSON _, err = s.client.Do(req, &species) if err != nil { return nil, err } - return species, nil + return species.Species, nil } 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 } - req, err := s.client.NewRequest("PUT", url.String(), species) + req, err := s.client.NewRequest("PUT", url.String(), SpeciesJSON{Species: species}) if err != nil { return false, err } diff --git a/models/species_test.go b/models/species_test.go index ad53f48..81bb84c 100644 --- a/models/species_test.go +++ b/models/species_test.go @@ -26,7 +26,7 @@ func TestSpeciesService_Get(t *testing.T) { called = true testMethod(t, r, "GET") - writeJSON(w, want) + writeJSON(w, SpeciesJSON{Species: want}) }) 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) { called = true 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) writeJSON(w, want) @@ -93,7 +93,7 @@ func TestSpeciesService_List(t *testing.T) { testMethod(t, r, "GET") testFormValues(t, r, values{}) - writeJSON(w, want) + writeJSON(w, SpeciesListJSON{Species: want}) }) 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) { called = true 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) writeJSON(w, want)