@@ -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
0 commit comments