Skip to content

Commit

Permalink
feat: filter tags with union params (#2019)
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <mingchang555@hotmail.com>
  • Loading branch information
zmcNotafraid committed Jul 3, 2024
1 parent 57bd1c1 commit 7bac9fb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
6 changes: 5 additions & 1 deletion app/controllers/api/v1/xudts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def index

if params[:tags].present?
tags = parse_tags
scope = scope.joins(:xudt_tag).where("xudt_tags.tags && ARRAY[?]::varchar[]", tags).select("udts.*") unless tags.empty?
if params[:union].present?
scope = scope.joins(:xudt_tag).where("xudt_tags.tags && ARRAY[?]::varchar[]", tags).select("udts.*") unless tags.empty?
else
scope = scope.joins(:xudt_tag).where("xudt_tags.tags @> array[?]::varchar[]", tags).select("udts.*") unless tags.empty?
end
end

if stale?(scope)
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/api/v2/nft/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def index
end
if params[:tags].present?
tags = parse_tags
scope = scope.where("tags && ARRAY[?]::varchar[]", tags) unless tags.empty?
if params[:union].present?
scope = scope.where("tags && ARRAY[?]::varchar[]", tags) unless tags.empty?
else
scope = scope.where("tags @> array[?]::varchar[]", tags) unless tags.empty?
end
end
pagy, collections = pagy(sort_collections(scope))

Expand Down
13 changes: 12 additions & 1 deletion test/controllers/api/v1/xudts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,23 @@ class XudtsControllerTest < ActionDispatch::IntegrationTest
assert_equal 1, json["data"].length
end

test "filter union xudt by symbol and tags" do
udt = create(:udt, :xudt, symbol: "CKBB")
create(:xudt_tag, udt:, tags: ["duplicate", "layer-1-asset", "supply-limited"])
udt2 = create(:udt, :xudt, symbol: "RPGG")
create(:xudt_tag, udt: udt2, tags: ["duplicate", "layer-2-asset", "supply-limited"])
valid_get api_v1_xudts_url, params: { symbol: "CKBB", tags: "duplicate,supply-limited", union: true }
assert_response :success
assert_equal "CKBB", json["data"].first["attributes"]["symbol"]
assert_equal ["duplicate", "layer-1-asset", "supply-limited"], json["data"].first["attributes"]["xudt_tags"]
end

test "filter xudt by symbol and tags" do
udt = create(:udt, :xudt, symbol: "CKBB")
create(:xudt_tag, udt:, tags: ["duplicate", "layer-1-asset", "supply-limited"])
udt2 = create(:udt, :xudt, symbol: "RPGG")
create(:xudt_tag, udt: udt2, tags: ["duplicate", "layer-2-asset", "supply-limited"])
valid_get api_v1_xudts_url, params: { symbol: "CKBB", "tags": "duplicate,supply-limited" }
valid_get api_v1_xudts_url, params: { symbol: "CKBB", tags: "layer-1-asset,supply-limited,NOT EXIST" }
assert_response :success
assert_equal "CKBB", json["data"].first["attributes"]["symbol"]
assert_equal ["duplicate", "layer-1-asset", "supply-limited"], json["data"].first["attributes"]["xudt_tags"]
Expand Down
13 changes: 11 additions & 2 deletions test/controllers/api/v2/nft/collections_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ class NFT::CollectionsControllerTest < ActionDispatch::IntegrationTest
assert_equal ["invalid"], JSON.parse(response.body)["data"].last["tags"]
end

test "should filter by tags" do
test "should filter by union tags" do
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgbpp-compatible"]

get api_v2_nft_collections_url, params: { tags: "layer-1-asset,rgbpp-compatible" }
get api_v2_nft_collections_url, params: { tags: "layer-1-asset,rgbpp-compatible", union: true }
assert_response :success
assert_equal JSON.parse(response.body)["data"].size, 2
end

test "should filter by tags but not match" do
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgbpp-compatible"]

get api_v2_nft_collections_url, params: { tags: "layer-1-asset,invalid" }
assert_response :success
assert_equal JSON.parse(response.body)["data"].size, 0
end

test "sort by block_timestamp asc" do
block1 = create(:block, :with_block_hash, timestamp: 10.days.ago.to_i * 1000)
block3 = create(:block, :with_block_hash, timestamp: 1.day.ago.to_i * 1000)
Expand Down

0 comments on commit 7bac9fb

Please sign in to comment.