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") }
v: ModelValidationErrorStub{}, expectedCode: 400, expectedBody: validationError, }), ) }) Describe("#Index", func() { index := func(s IndexCase) { w := httptest.NewRecorder() r, err := http.NewRequest("GET", "http://example.com", nil) Expect(err).ToNot(HaveOccurred()) c := handler.Crud{} // When c.Index(s.rep, []string{"id"}, []string{"id"}, 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("Index", index, Entry("without error", IndexCase{ rep: IndexerStub{}, v: CriteriaValidationStub{}, expectedCode: 200,