Files
sena_db_api_layer/controllers/contextController.js
silverpro89 97dbbd4d12
All checks were successful
Deploy to Production / deploy (push) Successful in 25s
Add Context APIs and refactor vocab models
Introduce Context and ContextGuide features: add Sequelize models (models/Context.js, models/ContextGuide.js), controllers (controllers/contextController.js, controllers/contextGuideController.js) and authenticated route handlers (routes/contextRoutes.js, routes/contextGuideRoutes.js). Wire the new routes into app.js and export the models from models/index.js. Refactor vocabulary: remove VocabForm, VocabMapping and VocabRelation models and relationships, update models/Vocab.js schema and indexes, and add migrate-vocab.js to drop/recreate the vocab table for the new schema. Also add a lesson editor UI (public/lesson-editor.html) and a small cleanup in models/Lesson.js.
2026-02-02 10:05:46 +07:00

140 lines
2.7 KiB
JavaScript

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();