|
| 1 | +import express from 'express'; |
| 2 | +import { DatabaseQueries } from '../database'; |
| 3 | +import { ApiResponse } from '../types'; |
| 4 | +import { authenticateAdmin } from '../middleware/auth'; |
| 5 | + |
| 6 | +const router = express.Router(); |
| 7 | + |
| 8 | +// GET /api/about - Get all about sections - PUBLIC |
| 9 | +router.get('/', async (req, res) => { |
| 10 | + try { |
| 11 | + const sections = await DatabaseQueries.getAboutSections(); |
| 12 | + |
| 13 | + const response: ApiResponse = { |
| 14 | + success: true, |
| 15 | + data: sections, |
| 16 | + message: `Found ${sections.length} about sections` |
| 17 | + }; |
| 18 | + |
| 19 | + res.json(response); |
| 20 | + } catch (error) { |
| 21 | + console.error('Error fetching about sections:', error); |
| 22 | + res.status(500).json({ |
| 23 | + success: false, |
| 24 | + error: 'Failed to fetch about sections', |
| 25 | + message: error instanceof Error ? error.message : 'Unknown error' |
| 26 | + }); |
| 27 | + } |
| 28 | +}); |
| 29 | + |
| 30 | +// GET /api/about/:language - Get about sections by language - PUBLIC |
| 31 | +router.get('/:language', async (req, res) => { |
| 32 | + try { |
| 33 | + const { language } = req.params; |
| 34 | + |
| 35 | + if (!['en', 'pl', 'default'].includes(language)) { |
| 36 | + return res.status(400).json({ |
| 37 | + success: false, |
| 38 | + error: 'Invalid language. Supported: en, pl, default' |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + const sections = await DatabaseQueries.getAboutSectionsByLanguage(language); |
| 43 | + |
| 44 | + // Transform array into object keyed by sectionKey for easier frontend consumption |
| 45 | + const sectionsMap: Record<string, { title: string; body: string }> = {}; |
| 46 | + sections.forEach((section: any) => { |
| 47 | + sectionsMap[section.sectionKey] = { |
| 48 | + title: section.title, |
| 49 | + body: section.body |
| 50 | + }; |
| 51 | + }); |
| 52 | + |
| 53 | + const response: ApiResponse = { |
| 54 | + success: true, |
| 55 | + data: sectionsMap, |
| 56 | + message: `Found ${sections.length} about sections for ${language}` |
| 57 | + }; |
| 58 | + |
| 59 | + res.json(response); |
| 60 | + } catch (error) { |
| 61 | + console.error('Error fetching about sections by language:', error); |
| 62 | + res.status(500).json({ |
| 63 | + success: false, |
| 64 | + error: 'Failed to fetch about sections', |
| 65 | + message: error instanceof Error ? error.message : 'Unknown error' |
| 66 | + }); |
| 67 | + } |
| 68 | +}); |
| 69 | + |
| 70 | +// PUT /api/about - Create or update about section - PROTECTED |
| 71 | +router.put('/', authenticateAdmin, async (req, res) => { |
| 72 | + try { |
| 73 | + const { sectionKey, language, title, body } = req.body; |
| 74 | + |
| 75 | + if (!sectionKey || !language) { |
| 76 | + return res.status(400).json({ |
| 77 | + success: false, |
| 78 | + error: 'sectionKey and language are required' |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + if (!['en', 'pl', 'default'].includes(language)) { |
| 83 | + return res.status(400).json({ |
| 84 | + success: false, |
| 85 | + error: 'Invalid language. Supported: en, pl, default' |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + const validSections = ['header', 'intro', 'approach', 'expertise', 'personal']; |
| 90 | + if (!validSections.includes(sectionKey)) { |
| 91 | + return res.status(400).json({ |
| 92 | + success: false, |
| 93 | + error: `Invalid sectionKey. Supported: ${validSections.join(', ')}` |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + await DatabaseQueries.upsertAboutSection({ |
| 98 | + sectionKey, |
| 99 | + language, |
| 100 | + title: title || '', |
| 101 | + body: body || '' |
| 102 | + }); |
| 103 | + |
| 104 | + res.json({ |
| 105 | + success: true, |
| 106 | + message: `About section '${sectionKey}' for '${language}' saved successfully` |
| 107 | + }); |
| 108 | + } catch (error) { |
| 109 | + console.error('Error saving about section:', error); |
| 110 | + res.status(500).json({ |
| 111 | + success: false, |
| 112 | + error: 'Failed to save about section', |
| 113 | + message: error instanceof Error ? error.message : 'Unknown error' |
| 114 | + }); |
| 115 | + } |
| 116 | +}); |
| 117 | + |
| 118 | +// PUT /api/about/bulk - Bulk update multiple sections - PROTECTED |
| 119 | +router.put('/bulk', authenticateAdmin, async (req, res) => { |
| 120 | + try { |
| 121 | + const { sections } = req.body; |
| 122 | + |
| 123 | + if (!Array.isArray(sections) || sections.length === 0) { |
| 124 | + return res.status(400).json({ |
| 125 | + success: false, |
| 126 | + error: 'sections array is required' |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + const validSections = ['header', 'intro', 'approach', 'expertise', 'personal']; |
| 131 | + const validLanguages = ['en', 'pl', 'default']; |
| 132 | + |
| 133 | + for (const section of sections) { |
| 134 | + if (!section.sectionKey || !section.language) { |
| 135 | + return res.status(400).json({ |
| 136 | + success: false, |
| 137 | + error: 'Each section must have sectionKey and language' |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + if (!validLanguages.includes(section.language)) { |
| 142 | + return res.status(400).json({ |
| 143 | + success: false, |
| 144 | + error: `Invalid language '${section.language}'. Supported: ${validLanguages.join(', ')}` |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + if (!validSections.includes(section.sectionKey)) { |
| 149 | + return res.status(400).json({ |
| 150 | + success: false, |
| 151 | + error: `Invalid sectionKey '${section.sectionKey}'. Supported: ${validSections.join(', ')}` |
| 152 | + }); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Save all sections |
| 157 | + for (const section of sections) { |
| 158 | + await DatabaseQueries.upsertAboutSection({ |
| 159 | + sectionKey: section.sectionKey, |
| 160 | + language: section.language, |
| 161 | + title: section.title || '', |
| 162 | + body: section.body || '' |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + res.json({ |
| 167 | + success: true, |
| 168 | + message: `Saved ${sections.length} about sections successfully` |
| 169 | + }); |
| 170 | + } catch (error) { |
| 171 | + console.error('Error bulk saving about sections:', error); |
| 172 | + res.status(500).json({ |
| 173 | + success: false, |
| 174 | + error: 'Failed to save about sections', |
| 175 | + message: error instanceof Error ? error.message : 'Unknown error' |
| 176 | + }); |
| 177 | + } |
| 178 | +}); |
| 179 | + |
| 180 | +// DELETE /api/about/:id - Delete about section - PROTECTED |
| 181 | +router.delete('/:id', authenticateAdmin, async (req, res) => { |
| 182 | + try { |
| 183 | + const id = parseInt(req.params.id); |
| 184 | + |
| 185 | + if (isNaN(id)) { |
| 186 | + return res.status(400).json({ |
| 187 | + success: false, |
| 188 | + error: 'Invalid section ID' |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + await DatabaseQueries.deleteAboutSection(id); |
| 193 | + |
| 194 | + res.json({ |
| 195 | + success: true, |
| 196 | + message: 'About section deleted successfully' |
| 197 | + }); |
| 198 | + } catch (error) { |
| 199 | + console.error('Error deleting about section:', error); |
| 200 | + res.status(500).json({ |
| 201 | + success: false, |
| 202 | + error: 'Failed to delete about section', |
| 203 | + message: error instanceof Error ? error.message : 'Unknown error' |
| 204 | + }); |
| 205 | + } |
| 206 | +}); |
| 207 | + |
| 208 | +export default router; |
0 commit comments