backend/fastify/get-community-stats/server.js
2024-05-26 19:51:33 +02:00

28 lines
787 B
JavaScript

module.exports = function (fastify, opts, done) {
const pb = opts.pb;
fastify.get('/get-community-stats', async (request, reply) => {
let output;
let totalUsers = 0;
let totalPosts = 0;
let totalComments = 0;
try {
totalUsers = (await pb.collection("users").getList(1, 1))?.totalItems;
totalPosts = (await pb.collection("posts").getList(1, 1))?.totalItems;
totalComments = (await pb.collection("comments").getList(1, 1))?.totalItems;
output = { totalUsers, totalPosts, totalComments };
} catch (e) {
console.error(e);
output = { totalUsers, totalPosts, totalComments };
}
reply.send({ items: output });
});
done();
};