Skip to content
Snippets Groups Projects
Commit 76c88c1f authored by Allen Gilliland's avatar Allen Gilliland
Browse files

ability to filter list of Cards returned from GET /api/card by `:table` or...

ability to filter list of Cards returned from GET /api/card by `:table` or `:database` both of which require a second arguement for `id`
parent 38b71743
Branches
Tags
No related merge requests found
......@@ -14,7 +14,7 @@
(defannotation CardFilterOption
"Option must be one of `all`, `mine`, or `fav`."
[symb value :nillable]
(checkp-contains? #{:all :mine :fav} symb (keyword value)))
(checkp-contains? #{:all :mine :fav :database :table} symb (keyword value)))
(defannotation CardDisplayType
"Option must be a valid `display_type`."
......@@ -24,19 +24,30 @@
(defendpoint GET "/"
"Get all the `Cards`. With param `f` (default is `all`), restrict cards as follows:
* `all` Return all `Cards` which were created by current user or are publicly visible
* `mine` Return all `Cards` created by current user
* `fav` Return all `Cards` favorited by the current user"
[f]
{f CardFilterOption}
* `all` Return all `Cards`
* `mine` Return all `Cards` created by current user
* `fav` Return all `Cards` favorited by the current user
* `database` Return all `Cards` with `:database_id` equal to `id`
* `table` Return all `Cards` with `:table_id` equal to `id`
All returned cards must be either created by current user or are publicly visible."
[f id]
{f CardFilterOption
id Integer}
(-> (case (or f :all) ; default value for `f` is `:all`
:all (sel :many Card (k/order :name :ASC) (k/where (or {:creator_id *current-user-id*}
{:public_perms [> common/perms-none]})))
:mine (sel :many Card :creator_id *current-user-id* (k/order :name :ASC))
:fav (->> (-> (sel :many [CardFavorite :card_id] :owner_id *current-user-id*)
(hydrate :card))
(map :card)
(sort-by :name)))
:all (sel :many Card (k/order :name :ASC) (k/where (or {:creator_id *current-user-id*}
{:public_perms [> common/perms-none]})))
:mine (sel :many Card :creator_id *current-user-id* (k/order :name :ASC))
:fav (->> (-> (sel :many [CardFavorite :card_id] :owner_id *current-user-id*)
(hydrate :card))
(map :card)
(sort-by :name))
:database (sel :many Card (k/order :name :ASC) (k/where (and {:database_id id}
(or {:creator_id *current-user-id*}
{:public_perms [> common/perms-none]}))))
:table (sel :many Card (k/order :name :ASC) (k/where (and {:table_id id}
(or {:creator_id *current-user-id*}
{:public_perms [> common/perms-none]})))))
(hydrate :creator)))
(defendpoint POST "/"
......
......@@ -3,7 +3,8 @@
(:require [expectations :refer :all]
[metabase.db :refer :all]
(metabase.models [card :refer [Card]]
[common :as common])
[common :as common]
[database :refer [Database]])
[metabase.test.data :refer :all]
[metabase.test.data.users :refer :all]
[metabase.test.util :refer [match-$ expect-eval-actual-first random-name with-temp]]
......@@ -22,6 +23,63 @@
:visualization_settings {:global {:title nil}}}))
;; ## GET /api/card
;; Filter cards by database
(expect [true
false
true]
(with-temp Database [{dbid :id} {:name (random-name)
:engine :h2
:details {}}]
(with-temp Card [{id1 :id} {:name (random-name)
:public_perms common/perms-none
:creator_id (user->id :crowberto)
:display :table
:dataset_query {}
:visualization_settings {}
:database_id (db-id)}]
(with-temp Card [{id2 :id} {:name (random-name)
:public_perms common/perms-none
:creator_id (user->id :crowberto)
:display :table
:dataset_query {}
:visualization_settings {}
:database_id dbid}]
(let [card-returned? (fn [database-id card-id]
(contains? (->> ((user->client :crowberto) :get 200 "card" :f :database :id database-id)
(map :id)
set)
card-id))]
[(card-returned? 1 id1)
(card-returned? 2 id1)
(card-returned? 2 id2)])))))
;; Filter cards by table
(expect [true
false
true]
(with-temp Card [{id1 :id} {:name (random-name)
:public_perms common/perms-none
:creator_id (user->id :crowberto)
:display :table
:dataset_query {}
:visualization_settings {}
:table_id 1}]
(with-temp Card [{id2 :id} {:name (random-name)
:public_perms common/perms-none
:creator_id (user->id :crowberto)
:display :table
:dataset_query {}
:visualization_settings {}
:table_id 2}]
(let [card-returned? (fn [table-id card-id]
(contains? (->> ((user->client :crowberto) :get 200 "card" :f :table :id table-id)
(map :id)
set)
card-id))]
[(card-returned? 1 id1)
(card-returned? 2 id1)
(card-returned? 2 id2)]))))
;; Check that only the creator of a private Card can see it
(expect [true
false]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment