Skip to content

Commit

Permalink
fixed game page
Browse files Browse the repository at this point in the history
  • Loading branch information
singharaj-usai committed Oct 23, 2024
1 parent 0068a3d commit adf6116
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
32 changes: 23 additions & 9 deletions client/js/game/api.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
const GameAPI = {
fetchGameDetails: function (gameId) {
const token = localStorage.getItem('token');

if (!gameId) {
GameUtils.showError('Invalid game ID');
return;
}

$.ajax({
url: `/api/games/${gameId}`,
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
success: function (game) {
GameDisplay.displayGameDetails(game);
if (game) {
GameDisplay.displayGameDetails(game);
} else {
GameUtils.showError('Game not found');
}
},
error: function (xhr, status, error) {
GameUtils.showError(
'Error fetching game details: ' +
(xhr.responseJSON ? xhr.responseJSON.error : 'Unknown error')
);
},
let errorMessage = 'Error fetching game details';
if (xhr.status === 404) {
errorMessage = 'Game not found';
} else if (xhr.responseJSON && xhr.responseJSON.error) {
errorMessage += ': ' + xhr.responseJSON.error;
}
GameUtils.showError(errorMessage);
}
});
},
};
}
};
15 changes: 7 additions & 8 deletions server/functions/api/routes/games.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,23 @@ router.get('/user/:userId', authenticateToken, async (req, res) => {
router.get('/:id', authenticateToken, async (req, res) => {
try {
const id = req.params.id;
const game = await Game.findOne({ _id: id, AssetType: 'Place' }).populate(
'creator',
'username userId'
);
const game = await Game.findById(id).populate('creator', 'username userId');

if (!game) {
return res.status(404).json({ error: 'Game not found' });
}

res.json(game);
} catch (error) {
console.error('Error fetching game:', error);
res
.status(500)
.json({ error: 'Error fetching game', details: error.message });
res.status(500).json({
error: 'Error fetching game',
details: error.message
});
}
});



router.post(
'/upload',
authenticateToken,
Expand Down

0 comments on commit adf6116

Please sign in to comment.