Create species, cleanup schema.

- create species
- species genus_id not null
This commit is contained in:
Matthew Dillon 2014-10-15 16:43:09 -08:00
parent ed2ba26654
commit 7fe5566edf
10 changed files with 155 additions and 3 deletions

View file

@ -30,6 +30,7 @@ func Handler() *mux.Router {
m.Get(router.DeleteGenus).Handler(handler(serveDeleteGenus))
m.Get(router.Species).Handler(handler(serveSpecies))
m.Get(router.CreateSpecies).Handler(handler(serveCreateSpecies))
return m
}

View file

@ -1,10 +1,12 @@
package api
import (
"encoding/json"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/thermokarst/bactdb/models"
)
func serveSpecies(w http.ResponseWriter, r *http.Request) error {
@ -20,3 +22,21 @@ func serveSpecies(w http.ResponseWriter, r *http.Request) error {
return writeJSON(w, species)
}
func serveCreateSpecies(w http.ResponseWriter, r *http.Request) error {
var species models.Species
err := json.NewDecoder(r.Body).Decode(&species)
if err != nil {
return err
}
created, err := store.Species.Create(&species)
if err != nil {
return err
}
if created {
w.WriteHeader(http.StatusCreated)
}
return writeJSON(w, species)
}

View file

@ -32,3 +32,30 @@ func TestSpecies_Get(t *testing.T) {
t.Errorf("got species %+v but wanted species %+v", got, want)
}
}
func TestSpecies_Create(t *testing.T) {
setup()
want := &models.Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
calledPost := false
store.Species.(*models.MockSpeciesService).Create_ = func(species *models.Species) (bool, error) {
if !normalizeDeepEqual(want, species) {
t.Errorf("wanted request for species %d but got %d", want, species)
}
calledPost = true
return true, nil
}
success, err := apiClient.Species.Create(want)
if err != nil {
t.Fatal(err)
}
if !calledPost {
t.Error("!calledPost")
}
if !success {
t.Error("!success")
}
}