Skip to content

Commit 97ccf8f

Browse files
authored
Merge pull request #2443 from nervosnetwork/develop
Deploy to testnet
2 parents fcbf718 + cca4c67 commit 97ccf8f

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

app/controllers/api/v2/nft/transfers_controller.rb

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,18 @@ module V2
33
module NFT
44
class TransfersController < BaseController
55
def index
6-
if params[:collection_id].present?
7-
if /\A\d+\z/.match?(params[:collection_id])
8-
collection = TokenCollection.find params[:collection_id]
9-
else
10-
collection = TokenCollection.find_by_sn params[:collection_id]
11-
end
12-
end
13-
14-
if collection && params[:token_id]
15-
item = collection.items.find_by token_id: params[:token_id]
16-
end
17-
18-
if item
19-
scope = item.transfers
20-
elsif collection
21-
scope = collection.transfers
22-
else
23-
scope = TokenTransfer.all
24-
end
6+
scope = TokenTransfer.all
257

26-
if params[:from]
27-
scope = scope.where(from: find_address(params[:from]))
28-
end
29-
if params[:to]
30-
scope = scope.where(to: find_address(params[:to]))
31-
end
32-
if params[:address_hash]
33-
address = find_address(params[:address_hash])
34-
scope = scope.where(from: address).or(scope.where(to: address))
35-
end
36-
if params[:transfer_action]
37-
scope = scope.where(action: params[:transfer_action])
38-
end
39-
if params[:tx_hash]
40-
scope = scope.includes(:ckb_transaction).where(ckb_transaction: { tx_hash: params[:tx_hash] })
8+
if params[:collection_id].present?
9+
collection = find_collection(params[:collection_id])
10+
scope = collection.present? ? filtered_by_token_id(collection) : TokenTransfer.none
4111
end
4212

13+
scope = apply_filters(scope)
4314
scope = scope.order(transaction_id: :desc)
4415
pagy, token_transfers = pagy(scope)
4516

46-
render json: {
47-
data: token_transfers,
48-
pagination: pagy
49-
}
17+
render json: { data: token_transfers, pagination: pagy }
5018
end
5119

5220
def show
@@ -64,6 +32,36 @@ def download_csv
6432

6533
private
6634

35+
def find_collection(collection_id)
36+
if /\A\d+\z/.match?(collection_id)
37+
TokenCollection.find_by(id: collection_id)
38+
else
39+
TokenCollection.find_by(sn: collection_id)
40+
end
41+
end
42+
43+
def filtered_by_token_id(collection)
44+
if params[:token_id].present?
45+
item = collection.items.find_by(token_id: params[:token_id])
46+
item.nil? ? TokenTransfer.none : item.transfers
47+
else
48+
collection.transfers
49+
end
50+
end
51+
52+
def apply_filters(scope)
53+
scope = scope.where(from: find_address(params[:from])) if params[:from].present?
54+
scope = scope.where(to: find_address(params[:to])) if params[:to].present?
55+
if params[:address_hash].present?
56+
address = find_address(params[:address_hash])
57+
scope = scope.where(from: address).or(scope.where(to: address))
58+
end
59+
scope = scope.where(action: params[:transfer_action]) if params[:transfer_action].present?
60+
scope = scope.includes(:ckb_transaction).where(ckb_transaction: { tx_hash: params[:tx_hash] }) if params[:tx_hash].present?
61+
62+
scope
63+
end
64+
6765
def find_address(address_hash)
6866
Address.find_by_address_hash(address_hash)
6967
end

test/controllers/api/v2/nft/transfers_controller_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ def setup
1818
end
1919
test "should get index" do
2020

21-
get api_v2_nft_transfers_url(collection_id: @token_collection.id, token_id: @token_item.id,
21+
get api_v2_nft_transfers_url(collection_id: @token_collection.id, token_id: @token_item.token_id,
2222
from: @from_address.address_hash,
2323
to: @to_address.address_hash)
2424
assert_response :success
2525
assert_equal 1, JSON.parse(response.body)['data'].size
26-
puts response.body
2726
assert_equal @token_transfer.id, JSON.parse(response.body)['data'][0]['id']
2827
end
2928

0 commit comments

Comments
 (0)