    :databases   (databases-for-org org)})
 
 
-(defendpoint GET "/" [org f]
+(defendpoint GET "/"
+  "Fetch all `Querys` for an `Org`. With filter option F (default `all`), return all queries;
+   with `mine`, only return queries created by the current user."
+  [org f]
   {org Required, f FilterOptionAllOrMine}
   (read-check Org org)
-  (-> (case (or f :all) ; default value for `f` is `:all`
+  (-> (case (or f :all)
         :all (sel :many Query
                   (where (or (= :creator_id *current-user-id*) (> :public_perms common/perms-none)))
                (where {:database_id [in (subselect Database (fields :id) (where {:organization_id org}))]})

    :database_id database))


(defendpoint POST "/" [:as {{:keys [clone] :as body} :body}]
  {clone Integer}
  (if clone
    (query-clone clone)
    (query-create body)))


(defendpoint GET "/:id" [id]
  (->404 (sel :one Query :id id)
         read-check
         (hydrate :creator :database :can_read :can_write)))


(defendpoint PUT "/:id" [id :as {{:keys [name public_perms details timezone database] :as body} :body}]
  {database     [Required Dict]
   name         NonEmptyString
   public_perms PublicPerms}

      (driver/dataset-query dataset-query options))))


(defendpoint GET "/:id/results"
  "Fetch the most recent results of a `Query`."
  [id]
  (read-check Query id)
  (sel :many QueryExecution :query_id id (order :finished_at :DESC) (limit 10)))

(defendpoint GET "/:id/csv"
  "Fetch the most recent results of a `Query` as CSV."
  [id]
  (let-404 [{{:keys [columns rows]} :result_data :as query-execution} (sel :one all-fields, :query_id id, (order :started_at :DESC))]
    (let-404 [{{:keys [can_read name]} :query} (hydrate query-execution :query)]
      (check-403 @can_read)
