From 8f3087ac99d8e6f20859d702548c1051c4329b8f Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 24 Oct 2023 14:27:45 +0800 Subject: [PATCH] feat: query udt by name or symbol Signed-off-by: Miles Zhang --- .../api/v1/udt_queries_controller.rb | 11 +++ app/models/udt.rb | 4 ++ config/routes.rb | 2 + .../api/v1/udt_queries_controller_test.rb | 69 +++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 app/controllers/api/v1/udt_queries_controller.rb create mode 100644 test/controllers/api/v1/udt_queries_controller_test.rb diff --git a/app/controllers/api/v1/udt_queries_controller.rb b/app/controllers/api/v1/udt_queries_controller.rb new file mode 100644 index 000000000..cdd934e39 --- /dev/null +++ b/app/controllers/api/v1/udt_queries_controller.rb @@ -0,0 +1,11 @@ +module Api + module V1 + class UdtQueriesController < ApplicationController + def index + udts = Udt.query_by_name_or_symbl(params[:q].downcase) + + render json: UdtSerializer.new(udts, { fields: { udt: [:full_name, :symbol, :type_hash, :icon_file] } }) + end + end + end +end diff --git a/app/models/udt.rb b/app/models/udt.rb index 0b73b73b5..2d9645aad 100644 --- a/app/models/udt.rb +++ b/app/models/udt.rb @@ -14,6 +14,10 @@ class Udt < ApplicationRecord validates :total_amount, numericality: { greater_than_or_equal_to: 0 } validates :email, format: { with: /\A(.+)@(.+)\z/, message: "Not a valid email" }, allow_nil: true + scope :query_by_name_or_symbl, ->(search) { + where("lower(full_name) LIKE ? or lower(symbol) LIKE ?", "%#{search}%", "%#{search}%") + } + attribute :code_hash, :ckb_hash has_and_belongs_to_many :ckb_transactions, join_table: :udt_transactions diff --git a/config/routes.rb b/config/routes.rb index fe4a1de51..8b2f4784e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -34,6 +34,8 @@ resources :cell_output_type_scripts, only: :show resources :cell_output_data, only: :show resources :suggest_queries, only: :index + resources :udt_queries, only: :index + resources :statistics, only: %i(index show) resources :statistics, only: %i(index show) resources :nets, only: %i(index show) resources :statistic_info_charts, only: :index diff --git a/test/controllers/api/v1/udt_queries_controller_test.rb b/test/controllers/api/v1/udt_queries_controller_test.rb new file mode 100644 index 000000000..1e9f82ef0 --- /dev/null +++ b/test/controllers/api/v1/udt_queries_controller_test.rb @@ -0,0 +1,69 @@ +require "test_helper" + +module Api + module V1 + class UdtQueriesControllerTest < ActionDispatch::IntegrationTest + test "should return empty array" do + valid_get api_v1_udt_queries_url, params: { q: "CKB" } + + response_json = { data: [] }.to_json + + assert_response :success + assert_equal "application/vnd.api+json", response.media_type + assert_equal response_json, response.body + end + + test "should query by symbol" do + udt = create(:udt, full_name: "Nervos Token", symbol: "CKB") + + valid_get api_v1_udt_queries_url, params: { q: "CKB" } + + response_json = UdtSerializer.new([udt], + { + fields: { + udt: [ + :full_name, :symbol, :type_hash, + :icon_file + ] } }).serialized_json + + assert_response :success + assert_equal response_json, response.body + end + + test "should query by name" do + udt = create(:udt, full_name: "Nervos Token", symbol: "CKB") + + valid_get api_v1_udt_queries_url, params: { q: "nervos" } + + response_json = UdtSerializer.new([udt], + { + fields: { + udt: [ + :full_name, :symbol, :type_hash, + :icon_file + ] } }).serialized_json + + assert_response :success + assert_equal response_json, response.body + end + + test "should query by symbol and name" do + udt1 = create(:udt, full_name: "Nervos Token", symbol: "CKB") + udt2 = create(:udt, full_name: "Nervos CKB", symbol: "NCKB") + + valid_get api_v1_udt_queries_url, params: { q: "CKB" } + + response_json = UdtSerializer.new([udt1, udt2], + { + fields: { + udt: [ + :full_name, :symbol, :type_hash, + :icon_file + ] } }).serialized_json + + assert_response :success + assert_equal response_json, response.body + end + end + end +end