func Test_Update(t *testing.T) { gs, db := setup(t, "gallery") defer teardown(t, db) t.Log("Given the need to list galleries.") { t.Log("\tWhen starting from an empty galleries collection.") { //---------------------------------------------------------------------- // Starting with a single gallery. g := gs[0] //---------------------------------------------------------------------- // Create the gallery. if err := gallery.Create(tests.Context, db, &g); err != nil { t.Fatalf("\t%s\tShould be able to upsert a gallery : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to upsert a gallery.", tests.Success) //---------------------------------------------------------------------- // Update the gallery. newHeadline := "my new headline" g.Headline = newHeadline if err := gallery.Update(tests.Context, db, g.ID.Hex(), &g); err != nil { t.Fatalf("\t%s\tShould be able to update the gallery : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to update the gallery.", tests.Success) if g.Headline != newHeadline { t.Fatalf("\t%s\tShould update the headline on the returned gallery : Expected \"%s\", got \"%s\"", tests.Failed, newHeadline, g.Headline) } t.Logf("\t%s\tShould update the headline on the returned gallery.", tests.Success) rg, err := gallery.Retrieve(tests.Context, db, g.ID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the gallery : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the gallery.", tests.Success) if rg.Headline != newHeadline { t.Fatalf("\t%s\tShould update the headline on the retrieved gallery : Expected \"%s\", got \"%s\"", tests.Failed, newHeadline, rg.Headline) } t.Logf("\t%s\tShould update the headline on the retrieved gallery.", tests.Success) } } }
// Update updates a FormGallery based on it's id and it's provided payload. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (formGalleryHandle) Update(c *web.Context) error { var g gallery.Gallery if err := json.NewDecoder(c.Request.Body).Decode(&g); err != nil { return err } id := c.Params["id"] err := gallery.Update(c.SessionID, c.Ctx["DB"].(*db.DB), id, &g) if err != nil { return err } c.Respond(g, http.StatusOK) return nil }