Spaces:
Sleeping
Sleeping
File size: 4,622 Bytes
0d3ef02 | 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | const express = require('express');
const { requireAuth, getTenantDb } = require('./auth');
const router = express.Router();
// GET /api/courses
router.get('/', requireAuth, async (req, res) => {
try {
const db = getTenantDb(req);
const { data, error } = await db
.from('courses')
.select('*')
.is('deleted_at', null)
.order('created_at', { ascending: false });
if (error) {
// If table doesn't exist yet, return empty array gracefully
if (error.code === '42P01') {
return res.json([]);
}
throw error;
}
res.json(data || []);
} catch (error) {
console.error('Error fetching courses:', error);
res.status(500).json({ error: 'Failed to fetch courses' });
}
});
// POST /api/courses
router.post('/', requireAuth, async (req, res) => {
try {
const {
title, category, duration, fee, max_price, discount, mode, trainer_name, admission_status,
trainer_experience, placement_percentage, rating, syllabus,
batch_timings, placement_partners, emi_options, ai_insight
} = req.body;
if (!title || !category || !fee) {
return res.status(400).json({ error: 'Missing required fields (title, category, fee)' });
}
const db = getTenantDb(req);
const orgId = req.user?.user_metadata?.organization_id || '00000000-0000-0000-0000-000000000001';
const { data, error } = await db.from('courses').insert([
{
organization_id: orgId,
title,
category,
duration: duration || '',
fee,
max_price: max_price || fee,
discount: discount || 0,
mode: mode || 'Online / Offline',
admission_status: admission_status || 'Open',
trainer_name: trainer_name || 'TBD',
trainer_experience: trainer_experience || '5+ Years',
placement_percentage: placement_percentage || 80,
rating: rating || 5.0,
syllabus: syllabus || [],
batch_timings: batch_timings || [],
placement_partners: placement_partners || [],
emi_options: emi_options || 'Available on request',
ai_insight: ai_insight || 'New course offering.'
}
]).select();
if (error) throw error;
res.json(data[0]);
} catch (error) {
console.error('Error creating course:', error);
res.status(500).json({ error: 'Failed to create course' });
}
});
// PUT /api/courses/:id
router.put('/:id', requireAuth, async (req, res) => {
try {
const { id } = req.params;
const {
title, category, duration, fee, max_price, discount, mode, trainer_name, admission_status,
trainer_experience, placement_percentage, rating, syllabus,
batch_timings, placement_partners, emi_options, ai_insight
} = req.body;
if (!title || !category || !fee) {
return res.status(400).json({ error: 'Missing required fields (title, category, fee)' });
}
const db = getTenantDb(req);
const { data, error } = await db.from('courses').update({
title,
category,
duration: duration || '',
fee,
max_price: max_price || fee,
discount: discount || 0,
mode: mode || 'Online / Offline',
admission_status: admission_status || 'Open',
trainer_name: trainer_name || 'TBD',
trainer_experience: trainer_experience || '5+ Years',
placement_percentage: placement_percentage || 80,
rating: rating || 5.0,
syllabus: syllabus || [],
batch_timings: batch_timings || [],
placement_partners: placement_partners || [],
emi_options: emi_options || 'Available on request',
ai_insight: ai_insight || 'Course updated.'
}).eq('id', id).select();
if (error) throw error;
res.json(data[0]);
} catch (error) {
console.error('Error updating course:', error);
res.status(500).json({ error: 'Failed to update course' });
}
});
// DELETE /api/courses/:id
router.delete('/:id', requireAuth, async (req, res) => {
try {
const { id } = req.params;
const db = getTenantDb(req);
// Soft delete
const { error } = await db.from('courses').update({ deleted_at: new Date().toISOString() }).eq('id', id);
if (error) throw error;
res.json({ success: true });
} catch (error) {
console.error('Error deleting course:', error);
res.status(500).json({ error: 'Failed to delete course' });
}
});
module.exports = router;
|