Implement functionality to view a user's wallet in wallet module

master
nicekid1 2 months ago
parent 021e22fcc9
commit f65fbce4d6
  1. 5
      src/wallet/wallet.controller.ts
  2. 3
      src/wallet/wallet.module.ts
  3. 12
      src/wallet/wallet.service.ts

@ -4,5 +4,8 @@ import { WalletService } from './wallet.service';
@Controller('wallet')
export class WalletController {
constructor(private readonly walletService: WalletService) {}
@Get(':userId')
async getBalance(@Param('userId') userId: number): Promise<number> {
return this.walletService.getBalance(userId);
}
}

@ -1,8 +1,11 @@
import { Module } from '@nestjs/common';
import { WalletService } from './wallet.service';
import { WalletController } from './wallet.controller';
import { Wallet } from './entities/wallet.entity';
import { SequelizeModule } from '@nestjs/sequelize';
@Module({
imports: [SequelizeModule.forFeature([Wallet])],
controllers: [WalletController],
providers: [WalletService],
})

@ -1,7 +1,15 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Wallet } from './entities/wallet.entity';
@Injectable()
export class WalletService {
constructor(){}
constructor(
@InjectModel(Wallet) private walletModel: typeof Wallet,
) {}
async getBalance(userId: number): Promise<number> {
const wallet = await this.walletModel.findOne({ where: { userId } });
return wallet ? wallet.balance : 0;
}
}

Loading…
Cancel
Save