const { Context } = require('../models'); /** * Context Controller */ class ContextController { /** * Get all contexts with pagination and filters */ async getAllContexts(req, res, next) { try { const { page = 1, limit = 50, type, title } = req.query; const offset = (page - 1) * limit; const where = {}; if (type) where.type = type; if (title) where.title = title; const { count, rows } = await Context.findAndCountAll({ where, limit: parseInt(limit), offset: parseInt(offset), order: [['created_at', 'DESC']] }); res.json({ success: true, data: { contexts: rows, pagination: { total: count, page: parseInt(page), limit: parseInt(limit), totalPages: Math.ceil(count / limit) } } }); } catch (error) { next(error); } } /** * Get context by UUID */ async getContextById(req, res, next) { try { const { id } = req.params; const context = await Context.findByPk(id); if (!context) { return res.status(404).json({ success: false, message: 'Context not found' }); } res.json({ success: true, data: context }); } catch (error) { next(error); } } /** * Create new context */ async createContext(req, res, next) { try { const context = await Context.create(req.body); res.status(201).json({ success: true, message: 'Context created successfully', data: context }); } catch (error) { next(error); } } /** * Update context */ async updateContext(req, res, next) { try { const { id } = req.params; const updates = req.body; const context = await Context.findByPk(id); if (!context) { return res.status(404).json({ success: false, message: 'Context not found' }); } await context.update(updates); res.json({ success: true, message: 'Context updated successfully', data: context }); } catch (error) { next(error); } } /** * Delete context */ async deleteContext(req, res, next) { try { const { id } = req.params; const context = await Context.findByPk(id); if (!context) { return res.status(404).json({ success: false, message: 'Context not found' }); } await context.destroy(); res.json({ success: true, message: 'Context deleted successfully' }); } catch (error) { next(error); } } } module.exports = new ContextController();