first commit

This commit is contained in:
r2xrzh9q2z-lab
2026-02-02 11:07:09 +07:00
commit d1b931d547
286 changed files with 53992 additions and 0 deletions

View File

@@ -0,0 +1,228 @@
const { readJsonFile, writeJsonFile } = require('../utils/jsonHelper');
const slugify = require('slugify');
// Hiển thị tất cả các trang
exports.getAllPages = async (req, res) => {
try {
const content = readJsonFile('content');
const pages = content.pages || [];
res.render('admin/pages/index', {
title: 'Quản lý trang',
pages
});
} catch (err) {
console.error(err);
req.flash('error_msg', 'Error loading page list');
res.redirect('/admin/dashboard');
}
};
// Hiển thị form tạo trang mới
exports.getAddPage = (req, res) => {
res.render('admin/pages/add', {
title: 'Thêm trang mới'
});
};
// Xử lý tạo trang mới
exports.addPage = async (req, res) => {
try {
const { title, content } = req.body;
// Kiểm tra dữ liệu
if (!title || !content) {
req.flash('error_msg', 'Please fill in all required fields');
return res.redirect('/admin/pages/add');
}
// Tạo slug từ tiêu đề
const slug = slugify(title, {
lower: true,
strict: true,
locale: 'vi'
});
// Lấy dữ liệu hiện tại
const contentData = readJsonFile('content');
const pages = contentData.pages || [];
// Kiểm tra slug đã tồn tại chưa
const existingPage = pages.find(page => page.slug === slug);
if (existingPage) {
req.flash('error_msg', 'This title is already in use. Please choose a different title.');
return res.redirect('/admin/pages/add');
}
// Tạo trang mới
const newPage = {
id: Date.now().toString(), // Sử dụng timestamp làm ID
title,
slug,
content,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
// Thêm trang mới vào danh sách
pages.push(newPage);
contentData.pages = pages;
// Lưu lại dữ liệu
writeJsonFile('content', contentData);
req.flash('success_msg', 'New page created successfully');
res.redirect('/admin/pages');
} catch (err) {
console.error(err);
req.flash('error_msg', 'Error creating new page');
res.redirect('/admin/pages/add');
}
};
// Hiển thị form chỉnh sửa trang
exports.getEditPage = async (req, res) => {
try {
const pageId = req.params.id;
const contentData = readJsonFile('content');
const pages = contentData.pages || [];
const page = pages.find(p => p.id === pageId);
if (!page) {
req.flash('error_msg', 'Page not found');
return res.redirect('/admin/pages');
}
res.render('admin/pages/edit', {
title: 'Chỉnh sửa trang',
page
});
} catch (err) {
console.error(err);
req.flash('error_msg', 'Error loading page');
res.redirect('/admin/pages');
}
};
// Xử lý chỉnh sửa trang
exports.updatePage = async (req, res) => {
try {
const pageId = req.params.id;
const { title, content } = req.body;
// Kiểm tra dữ liệu
if (!title || !content) {
req.flash('error_msg', 'Please fill in all required fields');
return res.redirect(`/admin/pages/edit/${pageId}`);
}
// Lấy dữ liệu hiện tại
const contentData = readJsonFile('content');
const pages = contentData.pages || [];
// Tìm trang cần cập nhật
const pageIndex = pages.findIndex(p => p.id === pageId);
if (pageIndex === -1) {
req.flash('error_msg', 'Page not found');
return res.redirect('/admin/pages');
}
const page = pages[pageIndex];
// Kiểm tra nếu tiêu đề thay đổi thì cập nhật slug
let newSlug = page.slug;
if (page.title !== title) {
newSlug = slugify(title, {
lower: true,
strict: true,
locale: 'vi'
});
// Kiểm tra slug đã tồn tại chưa (trừ trang hiện tại)
const existingPage = pages.find(p => p.slug === newSlug && p.id !== pageId);
if (existingPage) {
req.flash('error_msg', 'This title is already in use. Please choose a different title.');
return res.redirect(`/admin/pages/edit/${pageId}`);
}
}
// Cập nhật thông tin trang
pages[pageIndex] = {
...page,
title,
slug: newSlug,
content,
updatedAt: new Date().toISOString()
};
// Lưu lại dữ liệu
contentData.pages = pages;
writeJsonFile('content', contentData);
req.flash('success_msg', 'Page updated successfully');
res.redirect('/admin/pages');
} catch (err) {
console.error(err);
req.flash('error_msg', 'Error updating page');
res.redirect(`/admin/pages/edit/${req.params.id}`);
}
};
// Xử lý xóa trang
exports.deletePage = async (req, res) => {
try {
const pageId = req.params.id;
// Lấy dữ liệu hiện tại
const contentData = readJsonFile('content');
const pages = contentData.pages || [];
// Lọc bỏ trang cần xóa
contentData.pages = pages.filter(p => p.id !== pageId);
// Lưu lại dữ liệu
writeJsonFile('content', contentData);
req.flash('success_msg', 'Page deleted successfully');
res.redirect('/admin/pages');
} catch (err) {
console.error(err);
req.flash('error_msg', 'Error deleting page');
res.redirect('/admin/pages');
}
};
// Hiển thị trang theo slug
exports.getPageBySlug = async (req, res) => {
try {
const { slug } = req.params;
// Lấy dữ liệu từ content.json
const contentData = readJsonFile('content');
const pages = contentData.pages || [];
// Tìm trang theo slug
const page = pages.find(p => p.slug === slug);
if (!page) {
return res.status(404).render('page/not-found', {
title: 'Page Not Found',
message: 'The page you are looking for does not exist or has been deleted.'
});
}
// Hiển thị trang
res.render('page/view', {
title: page.title,
page
});
} catch (err) {
console.error(err);
res.status(500).render('page/error', {
title: 'Error',
message: 'An error occurred while loading the page. Please try again later.'
});
}
};