File size: 1,870 Bytes
92a138e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { getDb } = require('../config/database');

async function listTemplates(req, res) {
  const { category, layout, ats_only, premium_only } = req.query;
  const db = await getDb();
  
  let query = 'SELECT id, name, description, category, layout, thumbnail_url, is_ats_safe, is_premium, tags FROM templates WHERE 1=1';
  const params = [];

  if (category) { query += ' AND category = ?'; params.push(category); }
  if (layout) { query += ' AND layout = ?'; params.push(layout); }
  if (ats_only === 'true') { query += ' AND is_ats_safe = 1'; }
  if (premium_only === 'true') { query += ' AND is_premium = 1'; }

  query += ' ORDER BY sort_order ASC, created_at DESC';
  const templates = await db.all(query, ...params);

  return res.json({
    success: true,
    count: templates.length,
    templates: templates.map(t => ({ ...t, tags: JSON.parse(t.tags || '[]'), isAtsSafe: Boolean(t.is_ats_safe), isPremium: Boolean(t.is_premium) }))
  });
}

async function getTemplate(req, res) {
  const db = await getDb();
  const template = await db.get('SELECT * FROM templates WHERE id = ?', req.params.id);
  
  if (!template) {
    return res.status(404).json({ success: false, message: 'Template not found' });
  }

  return res.json({
    success: true,
    template: {
      ...template,
      tags: JSON.parse(template.tags || '[]'),
      isAtsSafe: Boolean(template.is_ats_safe),
      isPremium: Boolean(template.is_premium)
    }
  });
}

async function getCategories(req, res) {
  const db = await getDb();
  const categories = await db.all('SELECT DISTINCT category FROM templates');
  const layouts = await db.all('SELECT DISTINCT layout FROM templates');

  return res.json({
    success: true,
    categories: categories.map(c => c.category),
    layouts: layouts.map(l => l.layout)
  });
}

module.exports = { listTemplates, getTemplate, getCategories };