Skip to content

Commit

Permalink
feat: get wallet by id or name
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrelstan committed Jan 26, 2022
1 parent 4463aa2 commit 0041a49
Showing 1 changed file with 18 additions and 33 deletions.
51 changes: 18 additions & 33 deletions server/infra/database/WalletsRepository.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
import { Wallets } from 'models/Wallets';
import { Wallet } from 'models/Wallets';
import HttpError from '../../utils/HttpError';
import BaseRepository from './BaseRepository';
import Session from './Session';

export default class WalletsRepository extends BaseRepository<Wallets> {
export default class WalletsRepository extends BaseRepository<Wallet> {
constructor(session: Session) {
super('wallets', session);
super('wallet.wallet', session);
}

async getWalletById(id: string) {
const object = await this.session
.getDB()
.select(
this.session.getDB().raw(`
id,
name
`),
)
.table(this.tableName)
.where('id', id)
.first();
if (!object) {
async getWalletByIdOrName(walletIdOrName: string) {
const sql = `
SELECT *
FROM
wallet.wallet
WHERE
id = '${walletIdOrName}'
OR
name = '${walletIdOrName}'`;

const object = await this.session.getDB().raw(sql);

if (!object && object.rows.length !== 1) {
throw new HttpError(
404,
`Can not found ${this.tableName} by id or name:${id}`,
`Can not found ${this.tableName} by id:${walletIdOrName} name:${walletIdOrName}`,
);
}
return object;
return object.rows[0];
}

// async getByWallets(wallets_id: string, options: any) {
// const { limit, offset } = options;
// const sql = `
// SELECT
// *
// FROM entity
// LEFT JOIN planter ON planter.organization_id = entity.id
// WHERE planter.id = ${wallets_id}
// LIMIT ${limit}
// OFFSET ${offset}
// `;
// const object = await this.session.getDB().raw(sql);
// return object.rows;
// }
}

0 comments on commit 0041a49

Please sign in to comment.