From f65fbce4d63f3b9d1da37d9886c859f24af4565c Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Tue, 31 Dec 2024 15:48:35 +0330 Subject: [PATCH] Implement functionality to view a user's wallet in wallet module --- src/wallet/wallet.controller.ts | 5 ++++- src/wallet/wallet.module.ts | 3 +++ src/wallet/wallet.service.ts | 12 ++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/wallet/wallet.controller.ts b/src/wallet/wallet.controller.ts index 47bb6dc..e2bff43 100644 --- a/src/wallet/wallet.controller.ts +++ b/src/wallet/wallet.controller.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 { + return this.walletService.getBalance(userId); + } } diff --git a/src/wallet/wallet.module.ts b/src/wallet/wallet.module.ts index 00ed833..d809bb0 100644 --- a/src/wallet/wallet.module.ts +++ b/src/wallet/wallet.module.ts @@ -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], }) diff --git a/src/wallet/wallet.service.ts b/src/wallet/wallet.service.ts index 7ea1a64..86751b1 100644 --- a/src/wallet/wallet.service.ts +++ b/src/wallet/wallet.service.ts @@ -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 { + const wallet = await this.walletModel.findOne({ where: { userId } }); + return wallet ? wallet.balance : 0; + } }