Add Context APIs and refactor vocab models
All checks were successful
Deploy to Production / deploy (push) Successful in 25s
All checks were successful
Deploy to Production / deploy (push) Successful in 25s
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.
This commit is contained in:
139
controllers/contextGuideController.js
Normal file
139
controllers/contextGuideController.js
Normal file
@@ -0,0 +1,139 @@
|
||||
const { ContextGuide } = require('../models');
|
||||
|
||||
/**
|
||||
* ContextGuide Controller
|
||||
*/
|
||||
class ContextGuideController {
|
||||
/**
|
||||
* Get all context guides with pagination and filters
|
||||
*/
|
||||
async getAllContextGuides(req, res, next) {
|
||||
try {
|
||||
const { page = 1, limit = 50, name, title } = req.query;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const where = {};
|
||||
if (name) where.name = name;
|
||||
if (title) where.title = title;
|
||||
|
||||
const { count, rows } = await ContextGuide.findAndCountAll({
|
||||
where,
|
||||
limit: parseInt(limit),
|
||||
offset: parseInt(offset),
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
guides: rows,
|
||||
pagination: {
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
limit: parseInt(limit),
|
||||
totalPages: Math.ceil(count / limit)
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get context guide by UUID
|
||||
*/
|
||||
async getContextGuideById(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const guide = await ContextGuide.findByPk(id);
|
||||
|
||||
if (!guide) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Context guide not found'
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: guide
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new context guide
|
||||
*/
|
||||
async createContextGuide(req, res, next) {
|
||||
try {
|
||||
const guide = await ContextGuide.create(req.body);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Context guide created successfully',
|
||||
data: guide
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update context guide
|
||||
*/
|
||||
async updateContextGuide(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const updates = req.body;
|
||||
|
||||
const guide = await ContextGuide.findByPk(id);
|
||||
if (!guide) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Context guide not found'
|
||||
});
|
||||
}
|
||||
|
||||
await guide.update(updates);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Context guide updated successfully',
|
||||
data: guide
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete context guide
|
||||
*/
|
||||
async deleteContextGuide(req, res, next) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const guide = await ContextGuide.findByPk(id);
|
||||
|
||||
if (!guide) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Context guide not found'
|
||||
});
|
||||
}
|
||||
|
||||
await guide.destroy();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Context guide deleted successfully'
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new ContextGuideController();
|
||||
Reference in New Issue
Block a user