예제 #1
0
func AddRoutes(r *mux.Router, db *sqlx.DB) {
	rh := handler.Crud{}
	rv := Validator{}
	rd := Decoder{Whitelist: []string{"Latitude", "Longitude"}}
	repo := Repository{Db: db}

	r.HandleFunc("/rocks", rh.Index(repo, []string{"id", "before", "after", "latitude", "longitude", "createdAt", "updatedAt"}, []string{"id", "latitude", "longitude", "createdAt", "updatedAt"}, rv)).Methods("GET")
	r.HandleFunc("/rocks/{id:[0-9]+}", rh.Show(repo)).Methods("GET")
	r.HandleFunc("/rocks", rh.Create(repo, rd, rv)).Methods("POST")
	r.HandleFunc("/rocks/{id:[0-9]+}", rh.Update(repo, rd, rv)).Methods("PATCH")
	r.HandleFunc("/rocks/{id:[0-9]+}", rh.Destroy(repo)).Methods("DELETE")
}
예제 #2
0
func AddRoutes(r *mux.Router, db *sqlx.DB) {
	ch := handler.Crud{}
	cv := Validator{}
	cd := Decoder{Whitelist: []string{"Name", "Rating", "Description"}}

	repo := Repository{Db: db}

	r.HandleFunc("/climbs", ch.Index(repo, []string{"bounds", "id", "rockId", "before", "after", "rating", "createdAt", "updatedAt"}, []string{"bounds", "id", "rating", "createdAt", "updatedAt"}, cv)).Methods("GET")
	r.HandleFunc("/climbs/{id:[0-9]+}", ch.Show(repo)).Methods("GET")
	r.HandleFunc("/climbs/{id:[0-9]+}", ch.Update(repo, cd, cv)).Methods("PATCH")
	r.HandleFunc("/climbs/{id:[0-9]+}", ch.Destroy(repo)).Methods("DELETE")
	r.HandleFunc("/rocks/{id:[0-9]+}/climbs", ch.Create(repo, cd, cv)).Methods("POST")
}
예제 #3
0
				v:            ModelValidationErrorStub{},
				expectedCode: 400,
				expectedBody: validationError,
			}),
		)
	})

	Describe("#Create", func() {
		create := func(s CreatorCase) {
			w := httptest.NewRecorder()
			r, err := http.NewRequest("POST", "http://example.com", nil)
			Expect(err).ToNot(HaveOccurred())
			c := handler.Crud{}

			// When
			c.Create(s.rep, s.d, s.v)(w, r)

			// Expect
			Expect(w.Code).To(Equal(s.expectedCode))
			for key, value := range s.expectedHeaders {
				Expect(w.Header().Get(key)).To(Equal(value))
			}
			Expect(w.Body.String()).To(Equal(s.expectedBody))
		}

		DescribeTable("Create",
			create,
			Entry("without error", CreatorCase{
				rep:             CreatorStub{},
				d:               ModelDecoder{},
				v:               ModelValidationStub{},