Skip to content

Commit

Permalink
add total to get_nft_collections
Browse files Browse the repository at this point in the history
  • Loading branch information
dkackman committed Jan 26, 2025
1 parent d7b9259 commit 01d43b7
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 320 deletions.
1 change: 1 addition & 0 deletions crates/sage-api/src/requests/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub struct GetNftCollections {
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct GetNftCollectionsResponse {
pub collections: Vec<NftCollectionRecord>,
pub total: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
Expand Down
19 changes: 17 additions & 2 deletions crates/sage-database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ impl Database {
/// Count NFTs matching the given search parameters
pub async fn count_nfts(&self, params: NftSearchParams) -> Result<u32> {
let mut query_builder = sqlx::QueryBuilder::new("SELECT COUNT(*) FROM nfts ");

let where_builder = query_builder.push(" WHERE 1=1 ");

if !params.include_hidden {
where_builder.push(" AND visible = 1");
}
Expand Down Expand Up @@ -122,6 +122,21 @@ impl Database {

Ok(count.try_into()?)
}

/// Count collections, optionally including hidden ones
pub async fn count_collections(&self, include_hidden: bool) -> Result<i64> {
let count: i64 = if include_hidden {
sqlx::query_scalar!("SELECT COUNT(*) FROM collections")
.fetch_one(&self.pool)
.await?
} else {
sqlx::query_scalar!("SELECT COUNT(*) FROM collections WHERE visible = 1")
.fetch_one(&self.pool)
.await?
};

Ok(count)
}
}

#[derive(Debug)]
Expand Down
5 changes: 4 additions & 1 deletion crates/sage/src/endpoints/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,11 @@ impl Sage {
});
}

let total = wallet.db.count_collections(req.include_hidden).await?;

Ok(GetNftCollectionsResponse {
collections: records,
total: total.try_into()?,
})
}

Expand Down Expand Up @@ -454,7 +457,7 @@ impl Sage {
records.push(self.nft_record(nft_row, nft, collection_name)?);
}

Ok(GetNftsResponse {
Ok(GetNftsResponse {
nfts: records,
total: total.try_into().unwrap_or(0),
})
Expand Down
Loading

0 comments on commit 01d43b7

Please sign in to comment.