forked from UKSOURCE/cms.hailearning.edu.vn
Merge branch 'main' of https://gits.techvanguard.vn/UKSOURCE/cms.hailearning.edu.vn into hoanganh-03022026-appointment-contact-pricing
This commit is contained in:
135
models/blog.js
Normal file
135
models/blog.js
Normal file
@@ -0,0 +1,135 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const blogSchema = new mongoose.Schema({
|
||||
// Basic blog information
|
||||
title: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
},
|
||||
slug: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
trim: true
|
||||
},
|
||||
excerpt: {
|
||||
type: String,
|
||||
required: true,
|
||||
maxlength: 500
|
||||
},
|
||||
content: {
|
||||
type: mongoose.Schema.Types.Mixed, // Có thể là string HTML hoặc JSON EditorJS
|
||||
required: true
|
||||
},
|
||||
|
||||
// Media
|
||||
featuredImage: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
galleryImages: [{
|
||||
type: String
|
||||
}], // Mảng URL ảnh cho gallery (details-2, details-3)
|
||||
|
||||
// Author and publishing
|
||||
author: {
|
||||
type: String,
|
||||
default: 'Admin'
|
||||
},
|
||||
publishedAt: {
|
||||
type: String, // Format: "11 March 2025"
|
||||
default: function() {
|
||||
return new Date().toLocaleDateString('en-GB', {
|
||||
day: 'numeric',
|
||||
month: 'long',
|
||||
year: 'numeric'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// Categorization (simple strings, no references)
|
||||
category: [{
|
||||
type: String // ["Visa", "Travel", ...] - Một bài có thể thuộc nhiều category
|
||||
}],
|
||||
tags: [{
|
||||
type: String // ["WorkVisa", "StudentVisa", ...]
|
||||
}],
|
||||
|
||||
// Status and features
|
||||
status: {
|
||||
type: String,
|
||||
enum: ['draft', 'published'],
|
||||
default: 'published'
|
||||
},
|
||||
isFeatured: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
|
||||
// Comments count (có thể fake trước)
|
||||
commentsCount: {
|
||||
type: Number,
|
||||
default: 0
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
// Indexes
|
||||
blogSchema.index({ slug: 1 });
|
||||
blogSchema.index({ status: 1, createdAt: -1 });
|
||||
blogSchema.index({ category: 1, status: 1 });
|
||||
blogSchema.index({ isFeatured: 1, status: 1 });
|
||||
blogSchema.index({ tags: 1, status: 1 });
|
||||
|
||||
// Remove __v from JSON output
|
||||
blogSchema.set('toJSON', {
|
||||
transform: function(doc, ret) {
|
||||
delete ret.__v;
|
||||
return ret;
|
||||
}
|
||||
});
|
||||
|
||||
// Pre-save middleware
|
||||
blogSchema.pre('save', function(next) {
|
||||
// Auto-generate slug if not provided
|
||||
if (!this.slug && this.title) {
|
||||
this.slug = this.title
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9\s-]/g, '')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.trim('-');
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
// Static methods
|
||||
blogSchema.statics.getPublished = function() {
|
||||
return this.find({ status: 'published' }).sort({ createdAt: -1 });
|
||||
};
|
||||
|
||||
blogSchema.statics.getFeatured = function() {
|
||||
return this.find({
|
||||
status: 'published',
|
||||
isFeatured: true
|
||||
}).sort({ createdAt: -1 });
|
||||
};
|
||||
|
||||
blogSchema.statics.getByCategory = function(category) {
|
||||
return this.find({
|
||||
status: 'published',
|
||||
category: { $in: [category] } // Tìm trong array categories
|
||||
}).sort({ createdAt: -1 });
|
||||
};
|
||||
|
||||
blogSchema.statics.getByTag = function(tag) {
|
||||
return this.find({
|
||||
status: 'published',
|
||||
tags: tag
|
||||
}).sort({ createdAt: -1 });
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('Blog', blogSchema);
|
||||
76
models/blogCategory.js
Normal file
76
models/blogCategory.js
Normal file
@@ -0,0 +1,76 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const blogCategorySchema = new mongoose.Schema({
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true,
|
||||
unique: true // "Permanent Residency (PR)"
|
||||
},
|
||||
slug: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
trim: true // "permanent-residency"
|
||||
},
|
||||
postCount: {
|
||||
type: Number,
|
||||
default: 0 // "(04)"
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
isActive: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
// Indexes
|
||||
blogCategorySchema.index({ slug: 1 });
|
||||
blogCategorySchema.index({ isActive: 1, name: 1 });
|
||||
|
||||
// Remove __v from JSON output
|
||||
blogCategorySchema.set('toJSON', {
|
||||
transform: function(doc, ret) {
|
||||
delete ret.__v;
|
||||
return ret;
|
||||
}
|
||||
});
|
||||
|
||||
// Pre-save middleware
|
||||
blogCategorySchema.pre('save', function(next) {
|
||||
// Auto-generate slug if not provided
|
||||
if (!this.slug && this.name) {
|
||||
this.slug = this.name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9\s-]/g, '')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.trim('-');
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
// Static methods
|
||||
blogCategorySchema.statics.getActive = function() {
|
||||
return this.find({ isActive: true }).sort({ name: 1 });
|
||||
};
|
||||
|
||||
// Method to update post count
|
||||
blogCategorySchema.methods.updatePostCount = async function() {
|
||||
const Blog = require('./blog');
|
||||
const count = await Blog.countDocuments({
|
||||
category: { $in: [this.name] }, // Tìm trong array categories
|
||||
status: 'published'
|
||||
});
|
||||
this.postCount = count;
|
||||
await this.save();
|
||||
return count;
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('BlogCategory', blogCategorySchema);
|
||||
84
models/blogComment.js
Normal file
84
models/blogComment.js
Normal file
@@ -0,0 +1,84 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const blogCommentSchema = new mongoose.Schema({
|
||||
postId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'Blog',
|
||||
required: true
|
||||
},
|
||||
authorName: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true // "Frank Flores"
|
||||
},
|
||||
authorAvatar: {
|
||||
type: String,
|
||||
default: '' // "/assets/img/inner-page/news-details/comment-1.png"
|
||||
},
|
||||
content: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
},
|
||||
parentId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'BlogComment',
|
||||
default: null // Cho threaded comments (reply)
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
enum: ['pending', 'approved', 'rejected'],
|
||||
default: 'pending'
|
||||
}
|
||||
}, {
|
||||
timestamps: true // Vẫn giữ timestamps cho admin quản lý
|
||||
});
|
||||
|
||||
// Indexes
|
||||
blogCommentSchema.index({ postId: 1, status: 1, createdAt: -1 });
|
||||
blogCommentSchema.index({ parentId: 1 });
|
||||
blogCommentSchema.index({ status: 1 });
|
||||
|
||||
// Remove __v from JSON output
|
||||
blogCommentSchema.set('toJSON', {
|
||||
transform: function(doc, ret) {
|
||||
delete ret.__v;
|
||||
return ret;
|
||||
}
|
||||
});
|
||||
|
||||
// Static methods
|
||||
blogCommentSchema.statics.getApprovedByPost = function(postId) {
|
||||
return this.find({
|
||||
postId: postId,
|
||||
status: 'approved',
|
||||
parentId: null // Chỉ lấy comments gốc, không lấy replies
|
||||
}).sort({ createdAt: -1 });
|
||||
};
|
||||
|
||||
blogCommentSchema.statics.getReplies = function(parentId) {
|
||||
return this.find({
|
||||
parentId: parentId,
|
||||
status: 'approved'
|
||||
}).sort({ createdAt: 1 }); // Replies sắp xếp theo thời gian tăng dần
|
||||
};
|
||||
|
||||
blogCommentSchema.statics.getByStatus = function(status) {
|
||||
return this.find({ status: status })
|
||||
.populate('postId', 'title slug')
|
||||
.sort({ createdAt: -1 });
|
||||
};
|
||||
|
||||
// Method to approve comment
|
||||
blogCommentSchema.methods.approve = function() {
|
||||
this.status = 'approved';
|
||||
return this.save();
|
||||
};
|
||||
|
||||
// Method to reject comment
|
||||
blogCommentSchema.methods.reject = function() {
|
||||
this.status = 'rejected';
|
||||
return this.save();
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('BlogComment', blogCommentSchema);
|
||||
78
models/blogTag.js
Normal file
78
models/blogTag.js
Normal file
@@ -0,0 +1,78 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const blogTagSchema = new mongoose.Schema({
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true,
|
||||
unique: true // "WorkVisa"
|
||||
},
|
||||
slug: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
trim: true // "work-visa"
|
||||
},
|
||||
postCount: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
isActive: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
// Indexes
|
||||
blogTagSchema.index({ slug: 1 });
|
||||
blogTagSchema.index({ isActive: 1, name: 1 });
|
||||
|
||||
// Remove __v from JSON output
|
||||
blogTagSchema.set('toJSON', {
|
||||
transform: function(doc, ret) {
|
||||
delete ret.__v;
|
||||
return ret;
|
||||
}
|
||||
});
|
||||
|
||||
// Pre-save middleware
|
||||
blogTagSchema.pre('save', function(next) {
|
||||
// Auto-generate slug if not provided
|
||||
if (!this.slug && this.name) {
|
||||
this.slug = this.name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9\s-]/g, '')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.trim('-');
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
// Static methods
|
||||
blogTagSchema.statics.getActive = function() {
|
||||
return this.find({ isActive: true }).sort({ name: 1 });
|
||||
};
|
||||
|
||||
blogTagSchema.statics.getPopular = function(limit = 10) {
|
||||
return this.find({ isActive: true })
|
||||
.sort({ postCount: -1, name: 1 })
|
||||
.limit(limit);
|
||||
};
|
||||
|
||||
// Method to update post count
|
||||
blogTagSchema.methods.updatePostCount = async function() {
|
||||
const Blog = require('./blog');
|
||||
const count = await Blog.countDocuments({
|
||||
tags: { $in: [this.name] }, // Tìm trong array tags
|
||||
status: 'published'
|
||||
});
|
||||
this.postCount = count;
|
||||
await this.save();
|
||||
return count;
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('BlogTag', blogTagSchema);
|
||||
79
models/recentPost.js
Normal file
79
models/recentPost.js
Normal file
@@ -0,0 +1,79 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
// Recent Post model - có thể là view hoặc collection riêng để optimize performance
|
||||
const recentPostSchema = new mongoose.Schema({
|
||||
title: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
},
|
||||
slug: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
},
|
||||
thumbnail: {
|
||||
type: String,
|
||||
default: '' // Ảnh nhỏ ở sidebar
|
||||
},
|
||||
publishedAt: {
|
||||
type: String, // "March 26, 2025"
|
||||
required: true
|
||||
},
|
||||
// Reference to original blog post
|
||||
originalPostId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'Blog',
|
||||
required: true
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
// Indexes
|
||||
recentPostSchema.index({ createdAt: -1 });
|
||||
recentPostSchema.index({ originalPostId: 1 });
|
||||
|
||||
// Remove __v from JSON output
|
||||
recentPostSchema.set('toJSON', {
|
||||
transform: function(doc, ret) {
|
||||
delete ret.__v;
|
||||
return ret;
|
||||
}
|
||||
});
|
||||
|
||||
// Static method to sync with Blog posts
|
||||
recentPostSchema.statics.syncFromBlogs = async function(limit = 5) {
|
||||
const Blog = require('./blog');
|
||||
|
||||
// Get recent published blogs
|
||||
const recentBlogs = await Blog.find({ status: 'published' })
|
||||
.sort({ createdAt: -1 })
|
||||
.limit(limit)
|
||||
.select('title slug featuredImage publishedAt');
|
||||
|
||||
// Clear existing recent posts
|
||||
await this.deleteMany({});
|
||||
|
||||
// Create new recent posts
|
||||
const recentPosts = recentBlogs.map(blog => ({
|
||||
title: blog.title,
|
||||
slug: blog.slug,
|
||||
thumbnail: blog.featuredImage,
|
||||
publishedAt: blog.publishedAt,
|
||||
originalPostId: blog._id
|
||||
}));
|
||||
|
||||
if (recentPosts.length > 0) {
|
||||
await this.insertMany(recentPosts);
|
||||
}
|
||||
|
||||
return recentPosts;
|
||||
};
|
||||
|
||||
// Static method to get recent posts
|
||||
recentPostSchema.statics.getRecent = function(limit = 5) {
|
||||
return this.find({}).sort({ createdAt: -1 }).limit(limit);
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('RecentPost', recentPostSchema);
|
||||
Reference in New Issue
Block a user