diff --git a/app/controllers/api/v1/xudts_controller.rb b/app/controllers/api/v1/xudts_controller.rb index 0600c8a04..76ef3e728 100644 --- a/app/controllers/api/v1/xudts_controller.rb +++ b/app/controllers/api/v1/xudts_controller.rb @@ -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) diff --git a/app/controllers/api/v2/nft/collections_controller.rb b/app/controllers/api/v2/nft/collections_controller.rb index c007a7cff..7d68bdf18 100644 --- a/app/controllers/api/v2/nft/collections_controller.rb +++ b/app/controllers/api/v2/nft/collections_controller.rb @@ -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)) diff --git a/test/controllers/api/v1/xudts_controller_test.rb b/test/controllers/api/v1/xudts_controller_test.rb index 7c2af0c92..17c0fecd8 100644 --- a/test/controllers/api/v1/xudts_controller_test.rb +++ b/test/controllers/api/v1/xudts_controller_test.rb @@ -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"] diff --git a/test/controllers/api/v2/nft/collections_controller_test.rb b/test/controllers/api/v2/nft/collections_controller_test.rb index 15def51e2..80f5c7a7a 100644 --- a/test/controllers/api/v2/nft/collections_controller_test.rb +++ b/test/controllers/api/v2/nft/collections_controller_test.rb @@ -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)