Compare commits
	
		
			No commits in common. 'e18858fd60b6005a2325d09f4f28a4971f9d4467' and '021e22fcc978da119e8ef40a550e03dd2eebb7f4' have entirely different histories. 
		
	
	
		
			e18858fd60
			...
			021e22fcc9
		
	
		
	
				 4 changed files with 4 additions and 46 deletions
			
			
		@ -1,4 +0,0 @@ | 
				
			||||
export interface AddBalanceResponse { | 
				
			||||
  message: string; | 
				
			||||
  balance: number; | 
				
			||||
} | 
				
			||||
@ -1,19 +1,8 @@ | 
				
			||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; | 
				
			||||
import { WalletService } from './wallet.service'; | 
				
			||||
import { AddBalanceResponse } from './add-balance-response.interface'; | 
				
			||||
 | 
				
			||||
@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); | 
				
			||||
  } | 
				
			||||
  @Post(':userId/add') | 
				
			||||
  async addBalance( | 
				
			||||
    @Param('userId') userId: number, 
 | 
				
			||||
    @Body('amount') amount: number    
 | 
				
			||||
  ): Promise<AddBalanceResponse> { | 
				
			||||
    return this.walletService.addBalance(userId, amount); | 
				
			||||
  } | 
				
			||||
 | 
				
			||||
} | 
				
			||||
 | 
				
			||||
@ -1,31 +1,7 @@ | 
				
			||||
import { Injectable } from "@nestjs/common"; | 
				
			||||
import { InjectModel } from "@nestjs/sequelize"; | 
				
			||||
import { Wallet } from "./entities/wallet.entity"; | 
				
			||||
import { HttpException, HttpStatus } from "@nestjs/common"; | 
				
			||||
import { AddBalanceResponse } from "./add-balance-response.interface"; | 
				
			||||
import { Injectable } from '@nestjs/common'; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
@Injectable() | 
				
			||||
export class WalletService { | 
				
			||||
  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; | 
				
			||||
  } | 
				
			||||
  async addBalance(userId: number, amount: number): Promise<AddBalanceResponse> { | 
				
			||||
    try { | 
				
			||||
      const wallet = await this.walletModel.findOne({ where: { userId } }); | 
				
			||||
 | 
				
			||||
      if (wallet) { | 
				
			||||
        wallet.balance += Number(amount); | 
				
			||||
        await wallet.save(); | 
				
			||||
        return { message: "Balance updated successfully.", balance: wallet.balance }; | 
				
			||||
      } else { | 
				
			||||
        const newWallet = await this.walletModel.create({ userId, balance: amount }); | 
				
			||||
        return { message: "Wallet created and balance added successfully.", balance: newWallet.balance }; | 
				
			||||
      } | 
				
			||||
    } catch (error) { | 
				
			||||
      throw new HttpException("An error occurred while adding balance to the wallet.", HttpStatus.INTERNAL_SERVER_ERROR); | 
				
			||||
    } | 
				
			||||
  } | 
				
			||||
 constructor(){} | 
				
			||||
} | 
				
			||||
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue