forked from UKSOURCE/cms.hailearning.edu.vn
first commit
This commit is contained in:
129
models/header.js
Normal file
129
models/header.js
Normal file
@@ -0,0 +1,129 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
// Schema cho các link trong topbar
|
||||
const topbarLinkSchema = new mongoose.Schema({
|
||||
text: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
},
|
||||
url: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
}
|
||||
}, { _id: false });
|
||||
|
||||
// Schema cho contact info trong topbar
|
||||
const contactInfoSchema = new mongoose.Schema({
|
||||
phone: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
}
|
||||
}, { _id: false });
|
||||
|
||||
// Schema cho topbar
|
||||
const topbarSchema = new mongoose.Schema({
|
||||
contactInfo: {
|
||||
type: contactInfoSchema,
|
||||
required: true
|
||||
},
|
||||
links: {
|
||||
type: [topbarLinkSchema],
|
||||
default: []
|
||||
}
|
||||
}, { _id: false });
|
||||
|
||||
// Main Header Schema
|
||||
const headerSchema = new mongoose.Schema({
|
||||
name: {
|
||||
type: String,
|
||||
default: 'default',
|
||||
unique: true
|
||||
},
|
||||
topbar: {
|
||||
type: topbarSchema,
|
||||
required: true
|
||||
},
|
||||
logo: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
// Method để lấy menu tree từ collection menuHeader
|
||||
headerSchema.statics.getMenuTree = async function() {
|
||||
try {
|
||||
const Menu = require('./menuHeader');
|
||||
return await Menu.getMenuTree();
|
||||
} catch (error) {
|
||||
console.error('Error getting menu tree:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Method để lấy menu tree với programmes từ collection menuHeader
|
||||
headerSchema.statics.getMenuTreeWithProgrammes = async function() {
|
||||
try {
|
||||
const Menu = require('./menuHeader');
|
||||
return await Menu.getMenuTreeWithProgrammes();
|
||||
} catch (error) {
|
||||
console.error('Error getting menu tree with programmes:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Method để lấy programmes theo menu ID
|
||||
headerSchema.statics.getProgrammesByMenuId = async function(menuId) {
|
||||
try {
|
||||
const Menu = require('./menuHeader');
|
||||
return await Menu.getProgrammesByMenuId(menuId);
|
||||
} catch (error) {
|
||||
console.error('Error getting programmes by menu ID:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Tạo migration script để import dữ liệu từ menu.json
|
||||
headerSchema.statics.migrateFromJson = async function(jsonData) {
|
||||
try {
|
||||
// Kiểm tra xem đã có header mặc định chưa
|
||||
const existingHeader = await this.findOne({ name: 'default' });
|
||||
|
||||
// Chỉ giữ lại topbar và logo, bỏ search và mainMenu
|
||||
const headerData = {
|
||||
topbar: jsonData.topbar,
|
||||
logo: jsonData.logo
|
||||
};
|
||||
|
||||
if (existingHeader) {
|
||||
// Cập nhật header hiện có
|
||||
Object.assign(existingHeader, headerData);
|
||||
await existingHeader.save();
|
||||
console.log('Header data updated successfully');
|
||||
return existingHeader;
|
||||
} else {
|
||||
// Tạo header mới với dữ liệu từ JSON
|
||||
const newHeader = await this.create({
|
||||
name: 'default',
|
||||
...headerData
|
||||
});
|
||||
console.log('Header data imported successfully');
|
||||
return newHeader;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error migrating header data:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('Header', headerSchema);
|
||||
Reference in New Issue
Block a user