Skip to content
Snippets Groups Projects
Commit 03fe1ac9 authored by DewanaGustavus's avatar DewanaGustavus
Browse files

feat: user balance

parent d05e03fe
No related merge requests found
Pipeline #59600 failed with stages
in 0 seconds
...@@ -49,3 +49,22 @@ UserRouter.post('/login', async (request: Request, response: Response) => { ...@@ -49,3 +49,22 @@ UserRouter.post('/login', async (request: Request, response: Response) => {
return response.status(500).json(error.message); 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);
}
})
...@@ -63,4 +63,38 @@ export async function login(username : string, password : string) { ...@@ -63,4 +63,38 @@ export async function login(username : string, password : string) {
}); });
const responseString = (password === result?.password) ? "success" : "failed"; const responseString = (password === result?.password) ? "success" : "failed";
return responseString; 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;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment