Files
hope-events/backend/src/controllers/bannerController.js
T
joshua 3d381944d2 Initial commit
Next.js + Express event management app for Hope Family Church.
2026-07-23 15:26:47 +02:00

44 lines
1.1 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const BANNER_FILE = path.join(__dirname, '../../data/banner.json');
function readBanner() {
try {
const raw = fs.readFileSync(BANNER_FILE, 'utf8');
return JSON.parse(raw);
} catch {
return { message: '', type: 'info', liveFrom: null, liveTill: null };
}
}
function writeBanner(data) {
fs.writeFileSync(BANNER_FILE, JSON.stringify(data, null, 2), 'utf8');
}
// @desc Get the current banner
// @route GET /api/banner
// @access Public
const getBanner = (req, res) => {
res.json(readBanner());
};
// @desc Set the banner
// @route POST /api/banner
// @access Supervisor / Admin
const setBanner = (req, res) => {
const { message, type, liveFrom, liveTill } = req.body;
const allowed = ['info', 'warning', 'success', 'danger'];
const banner = {
message: typeof message === 'string' ? message.trim() : '',
type: allowed.includes(type) ? type : 'info',
liveFrom: liveFrom || null,
liveTill: liveTill || null,
};
writeBanner(banner);
res.json(banner);
};
module.exports = { getBanner, setBanner };