Create a new observation

This commit is contained in:
Matthew Dillon 2014-11-20 10:36:21 -09:00
parent a018d2bd7a
commit f867e5c424
9 changed files with 154 additions and 3 deletions

View file

@ -48,6 +48,7 @@ func Handler() *mux.Router {
m.Get(router.DeleteObservationType).Handler(handler(serveDeleteObservationType))
m.Get(router.Observation).Handler(handler(serveObservation))
m.Get(router.CreateObservation).Handler(handler(serveCreateObservation))
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 serveObservation(w http.ResponseWriter, r *http.Request) error {
@ -20,3 +22,21 @@ func serveObservation(w http.ResponseWriter, r *http.Request) error {
return writeJSON(w, observation)
}
func serveCreateObservation(w http.ResponseWriter, r *http.Request) error {
var observation models.Observation
err := json.NewDecoder(r.Body).Decode(&observation)
if err != nil {
return err
}
created, err := store.Observations.Create(&observation)
if err != nil {
return err
}
if created {
w.WriteHeader(http.StatusCreated)
}
return writeJSON(w, observation)
}

View file

@ -38,3 +38,30 @@ func TestObservation_Get(t *testing.T) {
t.Errorf("got %+v but wanted %+v", got, want)
}
}
func TestObservation_Create(t *testing.T) {
setup()
want := newObservation()
calledPost := false
store.Observations.(*models.MockObservationsService).Create_ = func(observation *models.Observation) (bool, error) {
if !normalizeDeepEqual(want, observation) {
t.Errorf("wanted request for observation %d but got %d", want, observation)
}
calledPost = true
return true, nil
}
success, err := apiClient.Observations.Create(want)
if err != nil {
t.Fatal(err)
}
if !calledPost {
t.Error("!calledPost")
}
if !success {
t.Error("!success")
}
}