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.
464 lines
17 KiB
HTML
464 lines
17 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="vi">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Lesson Editor - SENA</title>
|
|
<style>
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
body {
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
padding: 20px;
|
|
color: #1f2937;
|
|
}
|
|
.container {
|
|
max-width: 1100px;
|
|
margin: 0 auto;
|
|
background: #fff;
|
|
border-radius: 16px;
|
|
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
|
|
overflow: hidden;
|
|
}
|
|
.header {
|
|
padding: 24px 30px;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: #fff;
|
|
}
|
|
.header h1 { font-size: 2rem; margin-bottom: 6px; }
|
|
.header p { opacity: 0.9; }
|
|
.content { padding: 24px 30px; }
|
|
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
|
|
.form-group { margin-bottom: 16px; }
|
|
label { display: block; margin-bottom: 6px; font-weight: 600; }
|
|
input, select, textarea {
|
|
width: 100%;
|
|
padding: 12px 14px;
|
|
border: 1px solid #e5e7eb;
|
|
border-radius: 10px;
|
|
font-size: 14px;
|
|
background: #fff;
|
|
transition: border-color 0.2s;
|
|
}
|
|
input:focus, select:focus, textarea:focus { outline: none; border-color: #667eea; }
|
|
textarea { min-height: 120px; resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; }
|
|
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
|
|
.btn {
|
|
padding: 10px 16px;
|
|
border: none;
|
|
border-radius: 10px;
|
|
cursor: pointer;
|
|
font-weight: 600;
|
|
transition: transform 0.2s, box-shadow 0.2s;
|
|
}
|
|
.btn-primary { background: #4f46e5; color: #fff; }
|
|
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 6px 15px rgba(79,70,229,0.35); }
|
|
.btn-secondary { background: #6b7280; color: #fff; }
|
|
.btn-danger { background: #dc2626; color: #fff; }
|
|
.badge { display: inline-block; padding: 6px 10px; border-radius: 999px; font-size: 12px; }
|
|
.badge.ok { background: #dcfce7; color: #166534; }
|
|
.badge.err { background: #fee2e2; color: #991b1b; }
|
|
.section-title { margin: 18px 0 8px; font-weight: 700; color: #374151; }
|
|
.hint { color: #6b7280; font-size: 12px; margin-top: 6px; }
|
|
.status { margin-top: 10px; }
|
|
.full { grid-column: 1 / -1; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="header">
|
|
<h1>Lesson Editor</h1>
|
|
<p>Cập nhật nội dung Lesson qua API /api/lessons/:id</p>
|
|
</div>
|
|
<div class="content">
|
|
<div class="grid">
|
|
<div class="form-group">
|
|
<label for="baseUrl">API Base URL</label>
|
|
<input id="baseUrl" placeholder="http://localhost:3000" />
|
|
<div class="hint">Mặc định theo domain hiện tại</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="token">Bearer Token</label>
|
|
<input id="token" placeholder="Nhập token" />
|
|
</div>
|
|
<div class="form-group full">
|
|
<label for="subjectSelect">Chọn Subject</label>
|
|
<select id="subjectSelect"></select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="chapterSelect">Chọn Chapter</label>
|
|
<select id="chapterSelect"></select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="lessonSelect">Chọn Lesson</label>
|
|
<select id="lessonSelect"></select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="lessonId">Lesson ID (UUID)</label>
|
|
<input id="lessonId" placeholder="e.g. 550e8400-e29b-41d4-a716-446655440000" />
|
|
</div>
|
|
<div class="form-group row">
|
|
<button class="btn btn-primary" id="loadBtn">Tải Lesson</button>
|
|
<button class="btn btn-secondary" id="clearBtn">Xóa Form</button>
|
|
<span class="badge" id="statusBadge" style="display:none"></span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="section-title">Thông tin cơ bản</div>
|
|
<div class="grid">
|
|
<div class="form-group">
|
|
<label for="chapterId">Chapter ID</label>
|
|
<input id="chapterId" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="lessonNumber">Lesson Number</label>
|
|
<input id="lessonNumber" type="number" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="lessonTitle">Lesson Title</label>
|
|
<input id="lessonTitle" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="lessonType">Lesson Type</label>
|
|
<select id="lessonType">
|
|
<option value="json_content">json_content</option>
|
|
<option value="url_content">url_content</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="lessonContentType">Lesson Content Type</label>
|
|
<select id="lessonContentType">
|
|
<option value="">(none)</option>
|
|
<option value="vocabulary">vocabulary</option>
|
|
<option value="grammar">grammar</option>
|
|
<option value="phonics">phonics</option>
|
|
<option value="review">review</option>
|
|
<option value="mixed">mixed</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="displayOrder">Display Order</label>
|
|
<input id="displayOrder" type="number" />
|
|
</div>
|
|
<div class="form-group full">
|
|
<label for="lessonDescription">Lesson Description</label>
|
|
<textarea id="lessonDescription"></textarea>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="section-title">Content JSON</div>
|
|
<div class="form-group">
|
|
<label for="contentJson">content_json (JSON)</label>
|
|
<textarea id="contentJson" placeholder='{"type":"vocabulary","vocabulary_ids":[]}'></textarea>
|
|
<div class="hint">Nếu rỗng thì không gửi content_json</div>
|
|
</div>
|
|
|
|
<div class="section-title">Content URL</div>
|
|
<div class="grid">
|
|
<div class="form-group full">
|
|
<label for="contentUrl">content_url</label>
|
|
<input id="contentUrl" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="contentType">content_type</label>
|
|
<input id="contentType" placeholder="video, audio, pdf..." />
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="thumbnailUrl">thumbnail_url</label>
|
|
<input id="thumbnailUrl" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="durationMinutes">duration_minutes</label>
|
|
<input id="durationMinutes" type="number" />
|
|
</div>
|
|
</div>
|
|
|
|
<div class="section-title">Trạng thái</div>
|
|
<div class="grid">
|
|
<div class="form-group">
|
|
<label for="isPublished">is_published</label>
|
|
<select id="isPublished">
|
|
<option value="">(không đổi)</option>
|
|
<option value="true">true</option>
|
|
<option value="false">false</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="isFree">is_free</label>
|
|
<select id="isFree">
|
|
<option value="">(không đổi)</option>
|
|
<option value="true">true</option>
|
|
<option value="false">false</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<button class="btn btn-primary" id="saveBtn">Cập nhật Lesson</button>
|
|
<button class="btn btn-danger" id="deleteBtn">Xóa Lesson</button>
|
|
</div>
|
|
|
|
<div class="status" id="status"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const $ = (id) => document.getElementById(id);
|
|
|
|
const baseUrlInput = $('baseUrl');
|
|
const tokenInput = $('token');
|
|
|
|
baseUrlInput.value = localStorage.getItem('lessonEditorBaseUrl') || window.location.origin;
|
|
tokenInput.value = localStorage.getItem('lessonEditorToken') || '';
|
|
|
|
const setStatus = (ok, message) => {
|
|
const badge = $('statusBadge');
|
|
const status = $('status');
|
|
badge.style.display = 'inline-block';
|
|
badge.className = `badge ${ok ? 'ok' : 'err'}`;
|
|
badge.textContent = ok ? 'OK' : 'ERROR';
|
|
status.textContent = message || '';
|
|
};
|
|
|
|
const setSelectOptions = (selectEl, items, placeholder) => {
|
|
selectEl.innerHTML = '';
|
|
const ph = document.createElement('option');
|
|
ph.value = '';
|
|
ph.textContent = placeholder;
|
|
selectEl.appendChild(ph);
|
|
items.forEach((item) => {
|
|
const opt = document.createElement('option');
|
|
opt.value = item.value;
|
|
opt.textContent = item.label;
|
|
selectEl.appendChild(opt);
|
|
});
|
|
};
|
|
|
|
const fetchJson = async (url) => {
|
|
const res = await fetch(url, { headers: getHeaders() });
|
|
const data = await res.json();
|
|
if (!res.ok || !data.success) {
|
|
throw new Error(data.message || 'Request failed');
|
|
}
|
|
return data.data;
|
|
};
|
|
|
|
const loadSubjects = async () => {
|
|
const data = await fetchJson(`${getBaseUrl()}/api/subjects?limit=2000`);
|
|
const subjects = data.subjects || [];
|
|
setSelectOptions(
|
|
$('subjectSelect'),
|
|
subjects.map((s) => ({
|
|
value: s.id,
|
|
label: `${s.subject_code || ''} ${s.subject_name || ''}`.trim()
|
|
})),
|
|
'Chọn subject'
|
|
);
|
|
};
|
|
|
|
const loadChaptersBySubject = async (subjectId) => {
|
|
if (!subjectId) {
|
|
setSelectOptions($('chapterSelect'), [], 'Chọn chapter');
|
|
setSelectOptions($('lessonSelect'), [], 'Chọn lesson');
|
|
return;
|
|
}
|
|
const data = await fetchJson(`${getBaseUrl()}/api/subjects/${subjectId}/chapters?limit=2000`);
|
|
const chapters = data.chapters || [];
|
|
setSelectOptions(
|
|
$('chapterSelect'),
|
|
chapters.map((c) => ({
|
|
value: c.id,
|
|
label: `Ch${c.chapter_number || ''} - ${c.chapter_title || ''}`.trim()
|
|
})),
|
|
'Chọn chapter'
|
|
);
|
|
setSelectOptions($('lessonSelect'), [], 'Chọn lesson');
|
|
};
|
|
|
|
const loadLessonsByChapter = async (chapterId) => {
|
|
if (!chapterId) {
|
|
setSelectOptions($('lessonSelect'), [], 'Chọn lesson');
|
|
return;
|
|
}
|
|
const data = await fetchJson(`${getBaseUrl()}/api/chapters/${chapterId}/lessons?limit=2000`);
|
|
const lessons = data.lessons || [];
|
|
setSelectOptions(
|
|
$('lessonSelect'),
|
|
lessons.map((l) => ({
|
|
value: l.id,
|
|
label: `L${l.lesson_number || ''} - ${l.lesson_title || ''}`.trim()
|
|
})),
|
|
'Chọn lesson'
|
|
);
|
|
};
|
|
|
|
const getHeaders = () => {
|
|
const token = tokenInput.value.trim();
|
|
const headers = { 'Content-Type': 'application/json' };
|
|
if (token) headers.Authorization = `Bearer ${token}`;
|
|
return headers;
|
|
};
|
|
|
|
const getBaseUrl = () => baseUrlInput.value.trim().replace(/\/$/, '');
|
|
|
|
const loadLessonById = async () => {
|
|
try {
|
|
const id = $('lessonId').value.trim();
|
|
if (!id) return setStatus(false, 'Vui lòng nhập Lesson ID');
|
|
|
|
localStorage.setItem('lessonEditorBaseUrl', getBaseUrl());
|
|
localStorage.setItem('lessonEditorToken', tokenInput.value.trim());
|
|
|
|
const res = await fetch(`${getBaseUrl()}/api/lessons/${id}`, {
|
|
headers: getHeaders()
|
|
});
|
|
const data = await res.json();
|
|
if (!res.ok || !data.success) {
|
|
return setStatus(false, data.message || 'Không tải được lesson');
|
|
}
|
|
|
|
const lesson = data.data;
|
|
$('chapterId').value = lesson.chapter_id || '';
|
|
$('lessonNumber').value = lesson.lesson_number ?? '';
|
|
$('lessonTitle').value = lesson.lesson_title || '';
|
|
$('lessonType').value = lesson.lesson_type || 'json_content';
|
|
$('lessonDescription').value = lesson.lesson_description || '';
|
|
$('lessonContentType').value = lesson.lesson_content_type || '';
|
|
$('displayOrder').value = lesson.display_order ?? '';
|
|
$('contentUrl').value = lesson.content_url || '';
|
|
$('contentType').value = lesson.content_type || '';
|
|
$('thumbnailUrl').value = lesson.thumbnail_url || '';
|
|
$('durationMinutes').value = lesson.duration_minutes ?? '';
|
|
$('isPublished').value = lesson.is_published === true ? 'true' : lesson.is_published === false ? 'false' : '';
|
|
$('isFree').value = lesson.is_free === true ? 'true' : lesson.is_free === false ? 'false' : '';
|
|
$('contentJson').value = lesson.content_json ? JSON.stringify(lesson.content_json, null, 2) : '';
|
|
|
|
setStatus(true, 'Đã tải lesson');
|
|
} catch (err) {
|
|
setStatus(false, err.message || 'Lỗi không xác định');
|
|
}
|
|
};
|
|
|
|
$('loadBtn').addEventListener('click', loadLessonById);
|
|
|
|
$('subjectSelect').addEventListener('change', async (e) => {
|
|
try {
|
|
await loadChaptersBySubject(e.target.value);
|
|
} catch (err) {
|
|
setStatus(false, err.message || 'Không tải được chapter');
|
|
}
|
|
});
|
|
|
|
$('chapterSelect').addEventListener('change', async (e) => {
|
|
try {
|
|
await loadLessonsByChapter(e.target.value);
|
|
} catch (err) {
|
|
setStatus(false, err.message || 'Không tải được lesson');
|
|
}
|
|
});
|
|
|
|
$('lessonSelect').addEventListener('change', async (e) => {
|
|
if (!e.target.value) return;
|
|
$('lessonId').value = e.target.value;
|
|
await loadLessonById();
|
|
});
|
|
|
|
$('saveBtn').addEventListener('click', async () => {
|
|
try {
|
|
const id = $('lessonId').value.trim();
|
|
if (!id) return setStatus(false, 'Vui lòng nhập Lesson ID');
|
|
|
|
localStorage.setItem('lessonEditorBaseUrl', getBaseUrl());
|
|
localStorage.setItem('lessonEditorToken', tokenInput.value.trim());
|
|
|
|
const payload = {
|
|
chapter_id: $('chapterId').value.trim() || undefined,
|
|
lesson_number: $('lessonNumber').value ? parseInt($('lessonNumber').value, 10) : undefined,
|
|
lesson_title: $('lessonTitle').value.trim() || undefined,
|
|
lesson_type: $('lessonType').value || undefined,
|
|
lesson_description: $('lessonDescription').value.trim() || undefined,
|
|
lesson_content_type: $('lessonContentType').value || undefined,
|
|
display_order: $('displayOrder').value ? parseInt($('displayOrder').value, 10) : undefined,
|
|
content_url: $('contentUrl').value.trim() || undefined,
|
|
content_type: $('contentType').value.trim() || undefined,
|
|
thumbnail_url: $('thumbnailUrl').value.trim() || undefined,
|
|
duration_minutes: $('durationMinutes').value ? parseInt($('durationMinutes').value, 10) : undefined
|
|
};
|
|
|
|
const contentJsonText = $('contentJson').value.trim();
|
|
if (contentJsonText) {
|
|
try {
|
|
payload.content_json = JSON.parse(contentJsonText);
|
|
} catch (e) {
|
|
return setStatus(false, 'content_json không hợp lệ (JSON)');
|
|
}
|
|
}
|
|
|
|
const isPublished = $('isPublished').value;
|
|
if (isPublished !== '') payload.is_published = isPublished === 'true';
|
|
|
|
const isFree = $('isFree').value;
|
|
if (isFree !== '') payload.is_free = isFree === 'true';
|
|
|
|
Object.keys(payload).forEach((k) => payload[k] === undefined && delete payload[k]);
|
|
|
|
const res = await fetch(`${getBaseUrl()}/api/lessons/${id}`, {
|
|
method: 'PUT',
|
|
headers: getHeaders(),
|
|
body: JSON.stringify(payload)
|
|
});
|
|
const data = await res.json();
|
|
if (!res.ok || !data.success) {
|
|
return setStatus(false, data.message || 'Cập nhật thất bại');
|
|
}
|
|
|
|
setStatus(true, 'Cập nhật thành công');
|
|
} catch (err) {
|
|
setStatus(false, err.message || 'Lỗi không xác định');
|
|
}
|
|
});
|
|
|
|
$('deleteBtn').addEventListener('click', async () => {
|
|
try {
|
|
const id = $('lessonId').value.trim();
|
|
if (!id) return setStatus(false, 'Vui lòng nhập Lesson ID');
|
|
|
|
if (!confirm('Bạn chắc chắn muốn xóa lesson này?')) return;
|
|
|
|
const res = await fetch(`${getBaseUrl()}/api/lessons/${id}`, {
|
|
method: 'DELETE',
|
|
headers: getHeaders()
|
|
});
|
|
const data = await res.json();
|
|
if (!res.ok || !data.success) {
|
|
return setStatus(false, data.message || 'Xóa thất bại');
|
|
}
|
|
|
|
setStatus(true, 'Đã xóa lesson');
|
|
} catch (err) {
|
|
setStatus(false, err.message || 'Lỗi không xác định');
|
|
}
|
|
});
|
|
|
|
$('clearBtn').addEventListener('click', () => {
|
|
['chapterId','lessonNumber','lessonTitle','lessonType','lessonDescription','lessonContentType','displayOrder','contentJson','contentUrl','contentType','thumbnailUrl','durationMinutes','isPublished','isFree'].forEach((id) => {
|
|
const el = $(id);
|
|
if (!el) return;
|
|
if (el.tagName === 'SELECT') el.value = '';
|
|
else el.value = '';
|
|
});
|
|
setStatus(true, 'Đã xóa form');
|
|
});
|
|
|
|
(async () => {
|
|
try {
|
|
await loadSubjects();
|
|
} catch (err) {
|
|
setStatus(false, err.message || 'Không tải được subject');
|
|
}
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|