This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
const { Lesson, Chapter, Subject, Game } = require('../models');
|
||||
const { Op } = require('sequelize');
|
||||
const { Story, LessonStory } = require('../models');
|
||||
const { cacheUtils } = require('../config/redis');
|
||||
|
||||
/**
|
||||
* Lesson Controller
|
||||
@@ -384,6 +386,243 @@ class LessonController {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lấy danh sách stories trong một lesson
|
||||
*/
|
||||
async getStoriesByLesson(req, res, next) {
|
||||
try {
|
||||
const { lessonId } = req.params;
|
||||
const { page = 1, limit = 20 } = req.query;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const cacheKey = `lesson:${lessonId}:stories:${page}:${limit}`;
|
||||
|
||||
const cached = await cacheUtils.get(cacheKey);
|
||||
if (cached) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: cached,
|
||||
cached: true,
|
||||
});
|
||||
}
|
||||
|
||||
// Check if lesson exists
|
||||
const lesson = await Lesson.findByPk(lessonId);
|
||||
if (!lesson) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Không tìm thấy bài học',
|
||||
});
|
||||
}
|
||||
|
||||
// Get stories with pivot data
|
||||
const { count, rows } = await Story.findAndCountAll({
|
||||
include: [
|
||||
{
|
||||
model: Lesson,
|
||||
as: 'lessons',
|
||||
where: { id: lessonId },
|
||||
through: {
|
||||
attributes: ['display_order', 'is_required'],
|
||||
},
|
||||
attributes: [],
|
||||
},
|
||||
],
|
||||
limit: parseInt(limit),
|
||||
offset: parseInt(offset),
|
||||
order: [[{ model: Lesson, as: 'lessons' }, LessonStory, 'display_order', 'ASC']],
|
||||
});
|
||||
|
||||
const result = {
|
||||
lesson: {
|
||||
id: lesson.id,
|
||||
lesson_title: lesson.lesson_title,
|
||||
lesson_number: lesson.lesson_number,
|
||||
},
|
||||
stories: rows.map(story => ({
|
||||
...story.toJSON(),
|
||||
display_order: story.lessons[0]?.LessonStory?.display_order,
|
||||
is_required: story.lessons[0]?.LessonStory?.is_required,
|
||||
})),
|
||||
pagination: {
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
limit: parseInt(limit),
|
||||
totalPages: Math.ceil(count / limit),
|
||||
},
|
||||
};
|
||||
|
||||
await cacheUtils.set(cacheKey, result, 1800);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: result,
|
||||
cached: false,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Thêm story vào lesson
|
||||
*/
|
||||
async addStoryToLesson(req, res, next) {
|
||||
try {
|
||||
const { lessonId } = req.params;
|
||||
const { story_id, display_order = 0, is_required = true } = req.body;
|
||||
|
||||
// Validate required fields
|
||||
if (!story_id) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'story_id is required',
|
||||
});
|
||||
}
|
||||
|
||||
// Check if lesson exists
|
||||
const lesson = await Lesson.findByPk(lessonId);
|
||||
if (!lesson) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Không tìm thấy bài học',
|
||||
});
|
||||
}
|
||||
|
||||
// Check if story exists
|
||||
const story = await Story.findByPk(story_id);
|
||||
if (!story) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Không tìm thấy story',
|
||||
});
|
||||
}
|
||||
|
||||
// Check if relationship already exists
|
||||
const existing = await LessonStory.findOne({
|
||||
where: {
|
||||
lesson_id: lessonId,
|
||||
story_id: story_id,
|
||||
},
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Story đã tồn tại trong bài học này',
|
||||
});
|
||||
}
|
||||
|
||||
// Create relationship
|
||||
const lessonStory = await LessonStory.create({
|
||||
lesson_id: lessonId,
|
||||
story_id: story_id,
|
||||
display_order,
|
||||
is_required,
|
||||
});
|
||||
|
||||
// Clear cache
|
||||
await cacheUtils.deletePattern(`lesson:${lessonId}:stories:*`);
|
||||
await cacheUtils.deletePattern(`story:${story_id}:lessons:*`);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Story đã được thêm vào bài học',
|
||||
data: lessonStory,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Xóa story khỏi lesson
|
||||
*/
|
||||
async removeStoryFromLesson(req, res, next) {
|
||||
try {
|
||||
const { lessonId, storyId } = req.params;
|
||||
|
||||
// Check if lesson exists
|
||||
const lesson = await Lesson.findByPk(lessonId);
|
||||
if (!lesson) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Không tìm thấy bài học',
|
||||
});
|
||||
}
|
||||
|
||||
// Find relationship
|
||||
const lessonStory = await LessonStory.findOne({
|
||||
where: {
|
||||
lesson_id: lessonId,
|
||||
story_id: storyId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!lessonStory) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Story không tồn tại trong bài học này',
|
||||
});
|
||||
}
|
||||
|
||||
await lessonStory.destroy();
|
||||
|
||||
// Clear cache
|
||||
await cacheUtils.deletePattern(`lesson:${lessonId}:stories:*`);
|
||||
await cacheUtils.deletePattern(`story:${storyId}:lessons:*`);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Story đã được xóa khỏi bài học',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cập nhật thông tin story trong lesson (display_order, is_required)
|
||||
*/
|
||||
async updateStoryInLesson(req, res, next) {
|
||||
try {
|
||||
const { lessonId, storyId } = req.params;
|
||||
const { display_order, is_required } = req.body;
|
||||
|
||||
// Find relationship
|
||||
const lessonStory = await LessonStory.findOne({
|
||||
where: {
|
||||
lesson_id: lessonId,
|
||||
story_id: storyId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!lessonStory) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Story không tồn tại trong bài học này',
|
||||
});
|
||||
}
|
||||
|
||||
// Update
|
||||
await lessonStory.update({
|
||||
...(display_order !== undefined && { display_order }),
|
||||
...(is_required !== undefined && { is_required }),
|
||||
});
|
||||
|
||||
// Clear cache
|
||||
await cacheUtils.deletePattern(`lesson:${lessonId}:stories:*`);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Cập nhật thành công',
|
||||
data: lessonStory,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new LessonController();
|
||||
|
||||
Reference in New Issue
Block a user