Skip to content

Commit

Permalink
Merge branch 'fix_1035_get_wallets_add_count' into fix_297_total_is_a…
Browse files Browse the repository at this point in the history
…lways_null
  • Loading branch information
OlhaD committed Mar 24, 2023
2 parents a1ca59f + 61bf6ac commit 3ebb34a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
17 changes: 17 additions & 0 deletions server/infra/database/WalletsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Filter = Partial<{ name: string }>;
export default class WalletsRepository extends BaseRepository<Wallets> {
constructor(session: Session) {
super('wallet.wallet', session);
this.tableName = 'wallet.wallet';
}

async getWalletByIdOrName(walletIdOrName: string) {
Expand Down Expand Up @@ -134,4 +135,20 @@ export default class WalletsRepository extends BaseRepository<Wallets> {
);
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;
}
}
12 changes: 11 additions & 1 deletion server/models/Wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Filter = Partial<{ name: string }>;
function getByFilter(
WalletRepository: WalletsRepository,
): (filter: Filter, options: FilterOptions) => Promise<Wallets[]> {
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);
Expand All @@ -20,6 +20,15 @@ function getByFilter(
};
}

function getCount(
WalletRepository: WalletsRepository,
): (filter: Filter) => Promise<Wallets[]> {
return async (filter: Filter) => {
const count = await WalletRepository.getCount(filter);
return count;
};
}

export default {
getWalletByIdOrName: delegateRepository<WalletsRepository, Wallets>(
'getWalletByIdOrName',
Expand All @@ -28,6 +37,7 @@ export default {
'getWalletTokenContinentCount',
),
getByFilter,
getCount,
getFeaturedWallet: delegateRepository<WalletsRepository, Wallets>(
'getFeaturedWallet',
),
Expand Down
10 changes: 7 additions & 3 deletions server/routers/walletsRouter.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3ebb34a

Please sign in to comment.