diff --git a/src/handler/user/user.router.ts b/src/handler/user/user.router.ts index 06eb19167e9bc856ff69edf0eb99ac7796a0f4c8..4ec56123235e46df1fb3f635e3ba20025389f35d 100644 --- a/src/handler/user/user.router.ts +++ b/src/handler/user/user.router.ts @@ -49,3 +49,22 @@ UserRouter.post('/login', async (request: Request, response: Response) => { return response.status(500).json(error.message); } }) + +UserRouter.get('/balance/:username', async (request: Request, response: Response) => { + try { + const responseString = await UserServices.getBalance(request.params.username); + console.log(responseString); + return response.status(200).json(responseString); + } catch (error: any) { + return response.status(500).json(error.message); + } +}) + +UserRouter.post('/balance', async (request: Request, response: Response) => { + try { + const responseString = await UserServices.topup(request.body.username, request.body.topupBalance); + return response.status(200).json(responseString); + } catch (error: any) { + return response.status(500).json(error.message); + } +}) diff --git a/src/handler/user/user.service.ts b/src/handler/user/user.service.ts index 76edeb4c6bb443e3c817ea2a7420671e4bee5e1c..1ed8480e4d8396627f61127eef8823b03a29a78f 100644 --- a/src/handler/user/user.service.ts +++ b/src/handler/user/user.service.ts @@ -63,4 +63,38 @@ export async function login(username : string, password : string) { }); const responseString = (password === result?.password) ? "success" : "failed"; return responseString; -} \ No newline at end of file +} + +export async function getBalance(username : string) { + console.log(username); + const balance = await db.user.findFirst({ + where : { + username : username + }, + select : { + saldo : true + } + }); + console.log("balance", balance); + return balance?.saldo; +} + +export async function topup(username : string, topupBalance : number) { + let responseString = ""; + try{ + const result : queryResult = await db.user.update({ + where: { + username : username + }, + data: { + saldo : { + increment : topupBalance + } + } + }); + responseString = "success"; + }catch (err) { + responseString = "failed"; + } + return responseString; +}