lecture.js 1.3 KB
const mongoose = require('mongoose');

// Define Schemes
const lectureSchema = new mongoose.Schema({
  lectureid: { type: String, unique: true },
  lecturename: { type: String},
  proname: { type: String },
  avg_rate: { type: Number }
});

const reviewSchema = new mongoose.Schema({
  lectureid: { type: String, required: true, unique: true },
  review: { type: String, required: true }
});

// // Create new lecture document
lectureSchema.statics.create = function (payload) {
  // this === Model
  const lecture = new this(payload);
  // return Promise
  return lecture.save();
};

// Find All
lectureSchema.statics.findAll = function () {
  // return promise
  // V4부터 exec() 필요없음
  return this.find({});
};

// Find One by lectureid
lectureSchema.statics.findOneBylectureid = function (lectureid) {
  return this.findOne({ lectureid });
};

// Update by lectureid
lectureSchema.statics.updateBylectureid = function (lectureid, payload) {
  // { new: true }: return the modified document rather than the original. defaults to false
  return this.findOneAndUpdate({ lectureid }, payload, { new: true });
};

// Delete by lectureid
lectureSchema.statics.deleteBylectureid = function (lectureid) {
  return this.remove({ lectureid });
};

// Create Model & Export
module.exports = mongoose.model('Lecture', lectureSchema);