review.js 1.18 KB
const mongoose = require('mongoose');

// Define Schemes
const reviewSchema = new mongoose.Schema({
  lecturename: { type: String },
  proname: { type: String },
  avg_rate: { type: Number },
  review: {type : String}
});


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

// Find lecturename
reviewSchema.statics.findBylecturename = function (lecturename) {
  // return promise
  // V4부터 exec() 필요없음
  return this.find({ lecturename });
};

// Find by professor name
reviewSchema.statics.findByproname = function (proname) {
  return this.find({ proname });
};

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

// Delete by lecturename
reviewSchema.statics.deleteBylecturename = function (lecturename) {
  return this.remove({ lecturename });
};

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