|
|
const mongoose = require('mongoose'); |
|
|
|
|
|
const courseSchema = new mongoose.Schema({ |
|
|
title: { |
|
|
type: String, |
|
|
required: [true, 'Please add a course title'], |
|
|
trim: true |
|
|
}, |
|
|
description: { |
|
|
type: String, |
|
|
required: [true, 'Please add a description'] |
|
|
}, |
|
|
category: { |
|
|
type: String, |
|
|
required: [true, 'Please add a category'] |
|
|
}, |
|
|
duration: { |
|
|
type: Number, |
|
|
required: [true, 'Please add course duration in weeks'] |
|
|
}, |
|
|
instructor: { |
|
|
type: mongoose.Schema.ObjectId, |
|
|
ref: 'User', |
|
|
required: true |
|
|
}, |
|
|
price: { |
|
|
type: Number, |
|
|
required: [true, 'Please add a price'] |
|
|
}, |
|
|
image: { |
|
|
type: String, |
|
|
default: 'default-course.jpg' |
|
|
}, |
|
|
isFeatured: { |
|
|
type: Boolean, |
|
|
default: false |
|
|
}, |
|
|
status: { |
|
|
type: String, |
|
|
enum: ['draft', 'published', 'archived'], |
|
|
default: 'draft' |
|
|
}, |
|
|
createdAt: { |
|
|
type: Date, |
|
|
default: Date.now |
|
|
} |
|
|
}, { |
|
|
toJSON: { virtuals: true }, |
|
|
toObject: { virtuals: true } |
|
|
}); |
|
|
|
|
|
|
|
|
courseSchema.virtual('enrollments', { |
|
|
ref: 'Enrollment', |
|
|
localField: '_id', |
|
|
foreignField: 'course', |
|
|
justOne: false |
|
|
}); |
|
|
|
|
|
module.exports = mongoose.model('Course', courseSchema); |