diff --git a/server/infra/database/WalletsRepository.ts b/server/infra/database/WalletsRepository.ts index fb35ca12..6fd951a3 100644 --- a/server/infra/database/WalletsRepository.ts +++ b/server/infra/database/WalletsRepository.ts @@ -9,6 +9,7 @@ type Filter = Partial<{ name: string }>; export default class WalletsRepository extends BaseRepository { constructor(session: Session) { super('wallet.wallet', session); + this.tableName = 'wallet.wallet'; } async getWalletByIdOrName(walletIdOrName: string) { @@ -134,4 +135,20 @@ export default class WalletsRepository extends BaseRepository { ); return objectPatched; } + + async getCount(filter: Filter) { + const knex = this.session.getDB(); + + const result = await knex + .select( + knex.raw(` + COUNT(DISTINCT(${this.tableName}.id)) AS count + FROM ${this.tableName} + ${filter.name ? `WHERE name LIKE '%${filter.name}%'` : ''} + `), + ) + .first(); + + return result.count; + } } diff --git a/server/models/Wallets.ts b/server/models/Wallets.ts index 8b37c227..f1a035d5 100644 --- a/server/models/Wallets.ts +++ b/server/models/Wallets.ts @@ -9,7 +9,7 @@ type Filter = Partial<{ name: string }>; function getByFilter( WalletRepository: WalletsRepository, ): (filter: Filter, options: FilterOptions) => Promise { - return async function (filter: Filter, options: FilterOptions) { + return async (filter: Filter, options: FilterOptions) => { if (filter.name) { log.warn('using wallet name filter...'); const wallets = await WalletRepository.getByName(filter.name, options); @@ -20,6 +20,15 @@ function getByFilter( }; } +function getCount( + WalletRepository: WalletsRepository, +): (filter: Filter) => Promise { + return async (filter: Filter) => { + const count = await WalletRepository.getCount(filter); + return count; + }; +} + export default { getWalletByIdOrName: delegateRepository( 'getWalletByIdOrName', @@ -28,6 +37,7 @@ export default { 'getWalletTokenContinentCount', ), getByFilter, + getCount, getFeaturedWallet: delegateRepository( 'getFeaturedWallet', ), diff --git a/server/routers/walletsRouter.ts b/server/routers/walletsRouter.ts index 0c1cea5b..34b96d38 100644 --- a/server/routers/walletsRouter.ts +++ b/server/routers/walletsRouter.ts @@ -1,5 +1,6 @@ import express from 'express'; import Joi from 'joi'; +import FilterOptions from 'interfaces/FilterOptions'; import { handlerWrapper } from './utils'; import Session from '../infra/database/Session'; import WalletsRepository from '../infra/database/WalletsRepository'; @@ -65,12 +66,15 @@ router.get( filter.name = name; } - const result = await WalletModel.getByFilter(repo)(filter, { + const options: FilterOptions = { limit, offset, - }); + }; + + const result = await WalletModel.getByFilter(repo)(filter, options); + const count = await WalletModel.getCount(repo)(filter); res.send({ - total: null, + total: Number(count), offset, limit, wallets: result,