 (ns metabase.api.query
-  (:require [korma.core :refer [where subselect fields]]
+  (:require [korma.core :refer [where subselect fields order limit]]
             [compojure.core :refer [defroutes GET PUT POST DELETE]]
             [clojure.data.json :as json]
             [medley.core :refer :all]



(defendpoint DELETE "/:id" [id]
  ;; TODO - permissions check
  (let-404 [{:keys [can_write] :as query} (sel :one [Query :id :creator_id :public_perms] :id id)]
    (check-403 @can_write)
    (del Query :id id)))

  {:TODO "TODO"})


(defendpoint GET "/:id/results" [id]
  ;; TODO - implementation (list recent results of a query)
  (let-404 [{:keys [can_read] :as query} (sel :one Query :id id)]
    (check-403 @can_read)
    (sel :many QueryExecution :query_id id (order :finished_at :DESC) (limit 10))))


(defendpoint POST "/:id/csv" [id]
  ;; TODO - this should return a CSV file instead of JSON
  {:TODO "TODO"})


(define-routes)
