Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 2x 2x 2x 2x | // Dashboard controller
const { db } = require("../database");
const { getOne } = require("../models/query");
const DashboardController = {
async getStats(req, res) {
try {
const stats = {};
// Total books
const totalBooks = await getOne("SELECT COUNT(*) as total FROM books");
stats.totalBooks = totalBooks ? totalBooks.total : 0;
// Available books
const availableBooks = await getOne(
"SELECT SUM(available_copies) as total FROM books",
);
stats.availableBooks = availableBooks ? availableBooks.total : 0;
// Total active members
const activeMembers = await getOne(
'SELECT COUNT(*) as total FROM members WHERE status = "active"',
);
stats.activeMembers = activeMembers ? activeMembers.total : 0;
// Currently borrowed
const borrowedBooks = await getOne(
'SELECT COUNT(*) as total FROM borrowing WHERE status IN ("borrowed", "overdue")',
);
stats.borrowedBooks = borrowedBooks ? borrowedBooks.total : 0;
// Overdue books
const overdueBooks = await getOne(
'SELECT COUNT(*) as total FROM borrowing WHERE status = "overdue"',
);
stats.overdueBooks = overdueBooks ? overdueBooks.total : 0;
// Unreturned books that are overdue
const unreturned = await getOne(`
SELECT COUNT(*) as total FROM borrowing
WHERE status IN ('borrowed', 'overdue')
AND due_date < date('now')
`);
stats.unreturned = unreturned ? unreturned.total : 0;
res.json(stats);
} catch (error) {
res.status(500).json({ error: error.message });
}
},
};
module.exports = DashboardController;
|