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') @Controller('wallet')
export class WalletController { export class WalletController {
constructor(private readonly walletService: WalletService) {} 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 { Module } from '@nestjs/common';
import { WalletService } from './wallet.service'; import { WalletService } from './wallet.service';
import { WalletController } from './wallet.controller'; import { WalletController } from './wallet.controller';
import { Wallet } from './entities/wallet.entity';
import { SequelizeModule } from '@nestjs/sequelize';
@Module({ @Module({
imports: [SequelizeModule.forFeature([Wallet])],
controllers: [WalletController], controllers: [WalletController],
providers: [WalletService], providers: [WalletService],
}) })

@ -1,7 +1,15 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Wallet } from './entities/wallet.entity';
@Injectable() @Injectable()
export class WalletService { 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