diff --git a/src/api/user/gUser.js b/src/api/user/gUser.js index 16a8439..4da2426 100644 --- a/src/api/user/gUser.js +++ b/src/api/user/gUser.js @@ -5,7 +5,7 @@ export async function gUser(request, reply, fastify, getUserInfo) { if (!userInfo) { return reply.code(404).send({ error: "User does not exist" }); } - return reply.code(200).send({ userInfo }); + return reply.code(200).send({ username: userInfo.username, displayName: userInfo.displayName, wins: userInfo.wins, losses: userInfo.losses }); } catch (err) { fastify.log.error(err); return reply.code(500).send({ error: "Internal server error" }); diff --git a/src/api/user/pMatchHistory.js b/src/api/user/pMatchHistory.js index d619149..1cf17b4 100644 --- a/src/api/user/pMatchHistory.js +++ b/src/api/user/pMatchHistory.js @@ -1,3 +1,12 @@ +async function fetchSave(request, reply, userId, addMatch) { + const res = await fetch('http://localhost:3003/', { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ p1: userId, p2: request.body.opponent, p1Score: request.body.myScore, p2Score: request.body.opponentScore }) }); + if (!res.ok) { + throw new Error('Internal server error'); + } + const data = await res.json(); + addMatch.run(userId, data.id); +} + export async function pMatchHistory(request, reply, fastify, getUserInfo, addMatch, incWins, incLosses) { try { const userId = request.params.userId; @@ -16,11 +25,7 @@ export async function pMatchHistory(request, reply, fastify, getUserInfo, addMat if (request.body.opponent === userId) { return reply.code(400).send({ error: "Do you have dementia ? You cannot have played a match against yourself gramps" }); } - const res = await fetch('http://localhost:3003/', { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ p1: userId, p2: request.body.opponent, p1Score: request.body.myScore, p2Score: request.body.opponentScore }) }); - if (!res.ok) - return reply.code(500).send({ error: "Internal server error" }); - const data = await res.json(); - addMatch.run(userId, data.id); + await fetchSave(request, reply, userId, addMatch); if (request.body.myScore > request.body.opponentScore) { incWins.run(userId); incLosses.run(request.body.opponent);