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") }
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") }
expectedCode: 400, expectedBody: queryError, }), ) }) Describe("#Show", func() { show := func(s ShowCase) { // Given w := httptest.NewRecorder() r, err := http.NewRequest("GET", "http://example.com", nil) Expect(err).ToNot(HaveOccurred()) c := handler.Crud{} // When c.Show(s.rep)(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("Show", show, Entry("without error", ShowCase{ rep: FinderStub{}, expectedCode: 200, expectedHeaders: map[string]string{"Content-Type": "application/json; charset=utf-8"},