Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: query udt by name or symbol #1488

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/api/v1/udt_queries_controller.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions app/models/udt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions test/controllers/api/v1/udt_queries_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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