Пример #1
0
func (c Crud) Index(rep repository.Indexer, filterWhitelist []string, sortWhitelist []string, v validation.CriteriaValidator) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		criteria := decode.NewCriteria(*r.URL, filterWhitelist, sortWhitelist)

		var result repository.Result
		var err error
		if err = v.IsCriteriaValid(criteria, validation.Context{}); err == nil {
			result, err = rep.All(criteria)
		}

		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Header().Set("Access-Control-Allow-Origin", "*")

		if result.TotalCount.Valid {
			countStr := strconv.Itoa(int(result.TotalCount.Int64))
			w.Header().Set("Access-Control-Expose-Headers", "X-Total-Count")
			w.Header().Set("X-Total-Count", countStr)
		}

		if err != nil {
			code, e := parseError(err)
			writeError(w, e, code)

		} else {
			w.WriteHeader(http.StatusOK)
			json.NewEncoder(w).Encode(result.Data)
		}
	}
}
Пример #2
0
		whitelist = []string{"bounds", "id", "rating", "createdAt", "rockId", "before", "after"}
	})

	Describe("NewCriteria", func() {
		BeforeEach(func() {
			urlStr = "http://example.com/climbs?sort=-rating,createdAt,rockId"
		})

		Context("When pagination is present", func() {
			BeforeEach(func() {
				urlStr = "http://example.com/climbs?before=10&sort=id&pagination=true"
			})

			It("sets pagintion to true in critiera", func() {
				// When
				c := decode.NewCriteria(*url, whitelist, whitelist)

				// Expect
				Expect(c.Pagination).To(BeTrue())
			})

			Context("When pagination is not present", func() {
				BeforeEach(func() {
					urlStr = "http://example.com/climbs?before=10&sort=id"
				})

				It("sets pagintion to false in critiera", func() {
					// When
					c := decode.NewCriteria(*url, whitelist, whitelist)

					// Expect