Update an observation
This commit is contained in:
parent
dac0721a41
commit
b90da728fe
9 changed files with 158 additions and 0 deletions
|
@ -50,6 +50,7 @@ func Handler() *mux.Router {
|
|||
m.Get(router.Observation).Handler(handler(serveObservation))
|
||||
m.Get(router.CreateObservation).Handler(handler(serveCreateObservation))
|
||||
m.Get(router.Observations).Handler(handler(serveObservationList))
|
||||
m.Get(router.UpdateObservation).Handler(handler(serveUpdateObservation))
|
||||
|
||||
return m
|
||||
}
|
||||
|
|
|
@ -57,3 +57,22 @@ func serveObservationList(w http.ResponseWriter, r *http.Request) error {
|
|||
|
||||
return writeJSON(w, observations)
|
||||
}
|
||||
|
||||
func serveUpdateObservation(w http.ResponseWriter, r *http.Request) error {
|
||||
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
var observation models.Observation
|
||||
err := json.NewDecoder(r.Body).Decode(&observation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, err := store.Observations.Update(id, &observation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if updated {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
return writeJSON(w, observation)
|
||||
}
|
||||
|
|
|
@ -94,3 +94,33 @@ func TestObservation_List(t *testing.T) {
|
|||
t.Errorf("got observations %+v but wanted observations %+v", observations, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservation_Update(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
calledPut := false
|
||||
store.Observations.(*models.MockObservationsService).Update_ = func(id int64, observation *models.Observation) (bool, error) {
|
||||
if id != want.Id {
|
||||
t.Errorf("wanted request for observation %d but got %d", want.Id, id)
|
||||
}
|
||||
if !normalizeDeepEqual(want, observation) {
|
||||
t.Errorf("wanted request for observation %d but got %d", want, observation)
|
||||
}
|
||||
calledPut = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
success, err := apiClient.Observations.Update(want.Id, want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledPut {
|
||||
t.Error("!calledPut")
|
||||
}
|
||||
if !success {
|
||||
t.Error("!success")
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue