Strains changes for ember data.
This commit is contained in:
parent
a669cf8d93
commit
a03a9837a6
3 changed files with 27 additions and 18 deletions
|
@ -20,17 +20,17 @@ func serveStrain(w http.ResponseWriter, r *http.Request) error {
|
|||
return err
|
||||
}
|
||||
|
||||
return writeJSON(w, strain)
|
||||
return writeJSON(w, models.StrainJSON{Strain: strain})
|
||||
}
|
||||
|
||||
func serveCreateStrain(w http.ResponseWriter, r *http.Request) error {
|
||||
var strain models.Strain
|
||||
var strain models.StrainJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&strain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
created, err := store.Strains.Create(&strain)
|
||||
created, err := store.Strains.Create(strain.Strain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -55,18 +55,18 @@ func serveStrainList(w http.ResponseWriter, r *http.Request) error {
|
|||
strains = []*models.Strain{}
|
||||
}
|
||||
|
||||
return writeJSON(w, strains)
|
||||
return writeJSON(w, models.StrainsJSON{Strains: strains})
|
||||
}
|
||||
|
||||
func serveUpdateStrain(w http.ResponseWriter, r *http.Request) error {
|
||||
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
var strain models.Strain
|
||||
var strain models.StrainJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&strain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, err := store.Strains.Update(id, &strain)
|
||||
updated, err := store.Strains.Update(id, strain.Strain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ func serveDeleteStrain(w http.ResponseWriter, r *http.Request) error {
|
|||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
return writeJSON(w, &models.Strain{})
|
||||
return writeJSON(w, nil)
|
||||
}
|
||||
|
||||
func serveSubrouterStrainsList(w http.ResponseWriter, r *http.Request) error {
|
||||
|
@ -107,5 +107,5 @@ func serveSubrouterStrainsList(w http.ResponseWriter, r *http.Request) error {
|
|||
strains = []*models.Strain{}
|
||||
}
|
||||
|
||||
return writeJSON(w, strains)
|
||||
return writeJSON(w, models.StrainsJSON{Strains: strains})
|
||||
}
|
||||
|
|
|
@ -26,6 +26,14 @@ type Strain struct {
|
|||
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
|
||||
}
|
||||
|
||||
type StrainJSON struct {
|
||||
Strain *Strain `json:"strain"`
|
||||
}
|
||||
|
||||
type StrainsJSON struct {
|
||||
Strains []*Strain `json:"strains"`
|
||||
}
|
||||
|
||||
func (s *Strain) String() string {
|
||||
return fmt.Sprintf("%v", *s)
|
||||
}
|
||||
|
@ -95,13 +103,13 @@ func (s *strainsService) Get(id int64) (*Strain, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var strain *Strain
|
||||
var strain *StrainJSON
|
||||
_, err = s.client.Do(req, &strain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return strain, nil
|
||||
return strain.Strain, nil
|
||||
}
|
||||
|
||||
func (s *strainsService) Create(strain *Strain) (bool, error) {
|
||||
|
@ -110,7 +118,7 @@ func (s *strainsService) Create(strain *Strain) (bool, error) {
|
|||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("POST", url.String(), strain)
|
||||
req, err := s.client.NewRequest("POST", url.String(), StrainJSON{Strain: strain})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -139,13 +147,13 @@ func (s *strainsService) List(opt *StrainListOptions) ([]*Strain, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var strains []*Strain
|
||||
var strains *StrainsJSON
|
||||
_, err = s.client.Do(req, &strains)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return strains, nil
|
||||
return strains.Strains, nil
|
||||
}
|
||||
|
||||
func (s *strainsService) Update(id int64, strain *Strain) (bool, error) {
|
||||
|
@ -156,7 +164,7 @@ func (s *strainsService) Update(id int64, strain *Strain) (bool, error) {
|
|||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("PUT", url.String(), strain)
|
||||
req, err := s.client.NewRequest("PUT", url.String(), StrainJSON{Strain: strain})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ func TestStrainService_Get(t *testing.T) {
|
|||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
|
||||
writeJSON(w, want)
|
||||
writeJSON(w, StrainJSON{Strain: want})
|
||||
})
|
||||
|
||||
strain, err := client.Strains.Get(want.Id)
|
||||
|
@ -55,7 +55,7 @@ func TestStrainService_Create(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.CreateStrain, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "POST")
|
||||
testBody(t, r, `{"id":1,"speciesId":1,"strainName":"Test Strain","strainType":"Test Type","etymology":"Test Etymology","accessionBanks":"Test Accession","genbankEmblDdb":"Test Genbank","isolatedFrom":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
|
||||
testBody(t, r, `{"strain":{"id":1,"speciesId":1,"strainName":"Test Strain","strainType":"Test Type","etymology":"Test Etymology","accessionBanks":"Test Accession","genbankEmblDdb":"Test Genbank","isolatedFrom":null,"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 TestStrainService_List(t *testing.T) {
|
|||
testMethod(t, r, "GET")
|
||||
testFormValues(t, r, values{})
|
||||
|
||||
writeJSON(w, want)
|
||||
writeJSON(w, StrainsJSON{Strains: want})
|
||||
})
|
||||
|
||||
strains, err := client.Strains.List(nil)
|
||||
|
@ -124,7 +124,7 @@ func TestStrainService_Update(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.UpdateStrain, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"id":1,"speciesId":1,"strainName":"Test Strain Updated","strainType":"Test Type Updated","etymology":"Test Etymology","accessionBanks":"Test Accession Updated","genbankEmblDdb":"Test Genbank Updated","isolatedFrom":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
|
||||
testBody(t, r, `{"strain":{"id":1,"speciesId":1,"strainName":"Test Strain Updated","strainType":"Test Type Updated","etymology":"Test Etymology","accessionBanks":"Test Accession Updated","genbankEmblDdb":"Test Genbank Updated","isolatedFrom":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
@ -158,6 +158,7 @@ func TestStrainService_Delete(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.DeleteStrain, 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)
|
||||
|
|
Reference in a new issue