From 93db6ca7f48b331bb98cab82c528a685e70b4d4a Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 27 Feb 2024 18:19:09 +0800 Subject: [PATCH] feat: query items by hex token_id (#1653) Signed-off-by: Miles Zhang --- .../api/v2/nft/items_controller.rb | 28 ++++++++++++------- .../api/v2/nft/items_controller_test.rb | 20 +++++++++++-- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v2/nft/items_controller.rb b/app/controllers/api/v2/nft/items_controller.rb index 9b07b6740..a44cbc223 100644 --- a/app/controllers/api/v2/nft/items_controller.rb +++ b/app/controllers/api/v2/nft/items_controller.rb @@ -9,18 +9,18 @@ def index scope = scope.where(owner_id: @owner.id) end scope = scope.where(collection_id: @collection.id) if @collection - scope = scope.where(collection:{standard: params[:standard]}) if params[:standard] + scope = scope.where(collection: { standard: params[:standard] }) if params[:standard] scope = scope.where(token_id: params[:token_id]) if params[:token_id] scope = scope.order(token_id: :asc) pagy, items = pagy(scope) items = items.map do |i| j = i.as_json - j['collection'] = i.collection.as_json + j["collection"] = i.collection.as_json j end render json: { data: items, - pagination: pagy_metadata(pagy) + pagination: pagy_metadata(pagy), } end @@ -29,7 +29,8 @@ def show return head(:not_found) end - item = @collection.items.find_by token_id: params[:id] + token_id = parse_hex_token_id(params[:id]) + item = @collection.items.find_by(token_id:) if item render json: item.as_json.merge(collection: item.collection.as_json) else @@ -39,14 +40,21 @@ def show protected - def find_collection if params[:collection_id].present? - if /\A\d+\z/.match?(params[:collection_id]) - @collection = TokenCollection.find params[:collection_id] - else - @collection = TokenCollection.find_by_sn params[:collection_id] - end + @collection = if /\A\d+\z/.match?(params[:collection_id]) + TokenCollection.find params[:collection_id] + else + TokenCollection.find_by_sn params[:collection_id] + end + end + end + + def parse_hex_token_id(hex_id) + if hex_id.start_with?("0x") + hex_id.hex + else + hex_id end end end diff --git a/test/controllers/api/v2/nft/items_controller_test.rb b/test/controllers/api/v2/nft/items_controller_test.rb index cdd5ee747..7d517d2de 100644 --- a/test/controllers/api/v2/nft/items_controller_test.rb +++ b/test/controllers/api/v2/nft/items_controller_test.rb @@ -21,7 +21,7 @@ def setup test "should get index with collection sn" do sn = "iam-an-sn" - token_collection = create :token_collection, name: "token1", sn: sn + token_collection = create(:token_collection, name: "token1", sn:) address = create :address, is_depositor: true create :token_item, name: "item1", collection_id: token_collection.id, owner_id: address.id @@ -50,7 +50,7 @@ def setup test "should return spore cell" do token_collection = create :token_collection, name: "token1", standard: "spore" address = create :address, is_depositor: true - my_token_id = 100 + my_token_id = 244995949481600724545646750271542270961771653267601098727781219042501243997 cell = create :cell_output, :with_full_transaction create(:cell_datum, cell_output: cell) create :token_item, name: "item1", collection_id: token_collection.id, owner_id: address.id, @@ -62,6 +62,22 @@ def setup assert_equal JSON.parse(response.body)["standard"], "spore" assert_not_nil JSON.parse(response.body)["cell"]["data"] end + + test "should return spore cell when pass hex token_id" do + token_collection = create :token_collection, name: "token1", standard: "spore" + address = create :address, is_depositor: true + my_token_id = "0x008aa9acd3bd41c6e5d051d3cea822772249f2945179dcd4bf97259c474ab45d" + cell = create :cell_output, :with_full_transaction + create(:cell_datum, cell_output: cell) + create :token_item, name: "item1", collection_id: token_collection.id, owner_id: address.id, + token_id: my_token_id.hex, cell_id: cell.id + + get api_v2_nft_collection_item_url(id: my_token_id, collection_id: token_collection.id) + + assert_response :success + assert_equal JSON.parse(response.body)["standard"], "spore" + assert_not_nil JSON.parse(response.body)["cell"]["data"] + end end end end