Create a strain.
This commit is contained in:
parent
6c118f47f7
commit
6bba9058aa
9 changed files with 146 additions and 2 deletions
|
@ -36,6 +36,7 @@ func Handler() *mux.Router {
|
|||
m.Get(router.DeleteSpecies).Handler(handler(serveDeleteSpecies))
|
||||
|
||||
m.Get(router.Strain).Handler(handler(serveStrain))
|
||||
m.Get(router.CreateStrain).Handler(handler(serveCreateStrain))
|
||||
|
||||
return m
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func serveStrain(w http.ResponseWriter, r *http.Request) error {
|
||||
|
@ -20,3 +22,21 @@ func serveStrain(w http.ResponseWriter, r *http.Request) error {
|
|||
|
||||
return writeJSON(w, strain)
|
||||
}
|
||||
|
||||
func serveCreateStrain(w http.ResponseWriter, r *http.Request) error {
|
||||
var strain models.Strain
|
||||
err := json.NewDecoder(r.Body).Decode(&strain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
created, err := store.Strains.Create(&strain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if created {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
return writeJSON(w, strain)
|
||||
}
|
||||
|
|
|
@ -40,3 +40,30 @@ func TestStrain_Get(t *testing.T) {
|
|||
t.Errorf("got strain %+v but wanted strain %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrain_Create(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newStrain()
|
||||
|
||||
calledPost := false
|
||||
store.Strains.(*models.MockStrainsService).Create_ = func(strain *models.Strain) (bool, error) {
|
||||
if !normalizeDeepEqual(want, strain) {
|
||||
t.Errorf("wanted request for strain %d but got %d", want, strain)
|
||||
}
|
||||
calledPost = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
success, err := apiClient.Strains.Create(want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledPost {
|
||||
t.Error("!calledPost")
|
||||
}
|
||||
if !success {
|
||||
t.Error("!success")
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue