|
| 1 | +module Api |
| 2 | + module V1 |
| 3 | + class FungibleTokensController < ApplicationController |
| 4 | + before_action :validate_query_params, only: :show |
| 5 | + before_action :validate_pagination_params, :pagination_params, only: :index |
| 6 | + |
| 7 | + def index |
| 8 | + scope = Udt.includes(:xudt_tag).where(published: true, udt_type: %i[ssri xudt xudt_compatible sudt]) |
| 9 | + |
| 10 | + if params[:tags].present? |
| 11 | + tags = parse_tags |
| 12 | + if params[:union].present? |
| 13 | + scope = scope.joins(:xudt_tag).where("xudt_tags.tags && ARRAY[?]::varchar[]", tags).select("udts.*") unless tags.empty? |
| 14 | + else |
| 15 | + scope = scope.joins(:xudt_tag).where("xudt_tags.tags @> array[?]::varchar[]", tags).select("udts.*") unless tags.empty? |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + if stale?(scope) |
| 20 | + expires_in 1.minute, public: true |
| 21 | + |
| 22 | + udts = sort_udts(scope).page(@page).per(@page_size).fast_page |
| 23 | + options = FastJsonapi::PaginationMetaGenerator.new( |
| 24 | + request:, |
| 25 | + records: udts, |
| 26 | + page: @page, |
| 27 | + page_size: @page_size, |
| 28 | + ).call |
| 29 | + |
| 30 | + render json: UdtSerializer.new(udts, options) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + def show |
| 35 | + udt = Udt.find_by!(type_hash: params[:id], published: true) |
| 36 | + render json: UdtSerializer.new(udt) |
| 37 | + rescue ActiveRecord::RecordNotFound |
| 38 | + raise Api::V1::Exceptions::UdtNotFoundError |
| 39 | + end |
| 40 | + |
| 41 | + def download_csv |
| 42 | + args = params.permit(:id, :start_date, :end_date, :start_number, :end_number, udt: {}) |
| 43 | + file = CsvExportable::ExportUdtTransactionsJob.perform_now(args.to_h) |
| 44 | + |
| 45 | + send_data file, type: "text/csv; charset=utf-8; header=present", |
| 46 | + disposition: "attachment;filename=udt_transactions.csv" |
| 47 | + rescue ActiveRecord::RecordNotFound |
| 48 | + raise Api::V1::Exceptions::UdtNotFoundError |
| 49 | + end |
| 50 | + |
| 51 | + private |
| 52 | + |
| 53 | + def validate_query_params |
| 54 | + validator = Validations::Udt.new(params) |
| 55 | + |
| 56 | + if validator.invalid? |
| 57 | + errors = validator.error_object[:errors] |
| 58 | + status = validator.error_object[:status] |
| 59 | + |
| 60 | + render json: errors, status: |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + def pagination_params |
| 65 | + @page = params[:page] || 1 |
| 66 | + @page_size = params[:page_size] || Udt.default_per_page |
| 67 | + end |
| 68 | + |
| 69 | + def sort_udts(records) |
| 70 | + sort, order = params.fetch(:sort, "id.desc").split(".", 2) |
| 71 | + sort = |
| 72 | + case sort |
| 73 | + when "created_time" then "block_timestamp" |
| 74 | + when "transactions" then "h24_ckb_transactions_count" |
| 75 | + when "addresses_count" then "addresses_count" |
| 76 | + else "id" |
| 77 | + end |
| 78 | + |
| 79 | + if order.nil? || !order.match?(/^(asc|desc)$/i) |
| 80 | + order = "asc" |
| 81 | + end |
| 82 | + |
| 83 | + records.order("#{sort} #{order}").order("full_name ASC, id ASC") |
| 84 | + end |
| 85 | + |
| 86 | + def parse_tags |
| 87 | + tags = params[:tags].split(",") |
| 88 | + tags & XudtTag::VALID_TAGS |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments