func()

in api/wikipediapageview/post_queries.go [42:70]


func (h *handler) postQueries(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	req := &postQueriesRequest{}
	if err := hd.DecodeJSONBody(r, req); err != nil {
		hd.RespondErrorJSON(w, r, err)
		return
	}

	if req.OrderBy != "asc" && req.OrderBy != "desc" {
		hd.RespondErrorMessage(w, r,
			http.StatusBadRequest,
			"order_by can be 'asc' or 'desc'")
		return
	}

	q := h.buildQuery(req)
	q.DisableQueryCache = !req.QueryCache

	job, err := q.Run(ctx)
	if err != nil {
		hd.RespondErrorJSON(w, r, hd.Errorf(ctx,
			http.StatusInternalServerError,
			http.StatusText(http.StatusInternalServerError),
			"failed to run query: %w", err))
	}

	hd.RespondJSON(w, r, http.StatusOK, &postQueriesResponse{JobID: job.ID()})
}