Skip to content

Commit

Permalink
Added an endpoint for obtaining the leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
IyanRobles committed Mar 14, 2024
1 parent 3735fd2 commit 7131405
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
14 changes: 13 additions & 1 deletion gatewayservice/src/controllers/history-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const getHistory = async (req: Request, res: Response, next: NextFunction) => {
}
};

const getLeaderboard = async (req: Request, res: Response, next: NextFunction) => {
try {
const authResponse = await axios.get(HISTORY_SERVICE_URL + '/history/leaderboard', {
params: req.query,
});

res.json(authResponse.data);
} catch (error: any) {
next(error);
}
};

const updateHistory = async (
req: Request,
res: Response,
Expand Down Expand Up @@ -58,4 +70,4 @@ const incrementHistory = async (
}
};

export { getHistory, updateHistory, incrementHistory };
export { getHistory, updateHistory, incrementHistory, getLeaderboard };
2 changes: 2 additions & 0 deletions gatewayservice/src/routes/history-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {
getHistory,
updateHistory,
incrementHistory,
getLeaderboard
} from '../controllers/history-controller';

const router = express.Router();

router.get('/history', getHistory);
router.post('/history', updateHistory);
router.get('/history/leaderboard', getLeaderboard);
router.post('/history/increment', incrementHistory);

export default router;
35 changes: 34 additions & 1 deletion users/userservice/src/controllers/history-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,39 @@ const getHistory = async (req: Request, res: Response) => {
}
};

const DEFAULT_LEADERBOARD_SIZE = 10;

const getLeaderboard = async (req: Request, res: Response) => {
try {
const sizeParam = req.query.size;
let size = DEFAULT_LEADERBOARD_SIZE; // Default size if no parameter is received
if (sizeParam) {
size = parseInt(sizeParam as string, 10);
}

const users = await User.find({})
.sort({ 'history.points': -1 }) // Sort in descending order of points
.limit(size) // Only take the first (size) users
.select('username history'); // Select only username and history (no password, date, etc.)

const leaderboard = users.map(user => user.toJSON());

res.json({
status: 'success',
data: {
leaderboard
},
});
} catch (error: any) {
res.status(400).json({
status: 'fail',
data: {
error: error.message,
},
});
}
};

const updateHistory = async (req: Request, res: Response) => {
try {
const user = req.user;
Expand Down Expand Up @@ -87,4 +120,4 @@ const incrementHistory = async (req: Request, res: Response) => {
}
};

export { getHistory, updateHistory, incrementHistory };
export { getHistory, updateHistory, incrementHistory, getLeaderboard };
3 changes: 2 additions & 1 deletion users/userservice/src/routes/history-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import express from 'express';
import {
getHistory,
updateHistory,
incrementHistory,
incrementHistory, getLeaderboard,
} from '../controllers/history-controller';
import { protect } from '../middlewares/protect-middleware';

const router = express.Router();

router.get('/history', getHistory);
router.post('/history', protect, updateHistory);
router.get('/history/leaderboard', getLeaderboard);
router.post('/history/increment', protect, incrementHistory);

export default router;

0 comments on commit 7131405

Please sign in to comment.