Skip to content

Commit

Permalink
added modes json api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Unreal-Dan committed Mar 15, 2024
1 parent 6e2e998 commit e1e23e4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions routes/modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,34 @@ router.get('/', async (req, res) => {
res.render('modes', { modes: modesForCurrentPage, user: req.user, currentPage: page, search: req.query.search });
});

// show the main modes showcase
router.get('/json', async (req, res) => {
const page = parseInt(req.query.page || 1, 10);
const searchQuery = req.query.search;
var modesForCurrentPage = await Mode.find().sort({ votes: -1 }).exec();
// If search query is present, filter the modes based on the search criteria
if (searchQuery) {
modesForCurrentPage = modesForCurrentPage.filter(mode => {
return mode.name.toLowerCase().includes(searchQuery.toLowerCase());
});
}

// Assuming you want pagination
const pageSize = 10; // Or any other number of items per page you prefer
const totalCount = modesForCurrentPage.length;
const pageCount = Math.ceil(totalCount / pageSize);
const startIndex = (page - 1) * pageSize;
const endIndex = startIndex + pageSize;
const modesOnPage = modesForCurrentPage.slice(startIndex, endIndex);

// Return the modes as JSON
res.json({
data: modesOnPage,
page: page,
pages: pageCount,
totalCount: totalCount
});
});

module.exports = router;

0 comments on commit e1e23e4

Please sign in to comment.