Created
December 21, 2023 06:55
-
-
Save Enigo/00ebfbf9420d150fc3c6e0477e1785ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub async fn get_search_results(pool: &Pool<Postgres>, search: &String) -> Option<SearchData> { | |
return match query_as::<_, AssetContentDb>( | |
"select token_id, token_address, metadata->>'name' as name, metadata->>'image_url' as image_url from asset | |
where ($1~'^\\d+$' and token_id::text like '%' || $1 || '%') or lower(metadata->>'name') like '%' || $1 || '%' | |
order by 1, 3 | |
limit 20", | |
) | |
.bind(search.to_lowercase()) | |
.fetch_all(pool) | |
.await | |
{ | |
Ok(res) => { | |
let asset_content_data = res.into_iter().map(|asset| asset.into()).collect(); | |
Some(SearchData { | |
asset_content_data | |
}) | |
} | |
Err(e) => { | |
error!("Error fetching data: {e}"); | |
None | |
} | |
}; | |
} | |
#[derive(FromRow)] | |
struct AssetContentDb { | |
token_id: i32, | |
token_address: String, | |
name: String, | |
image_url: String, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment