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


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

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

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

// Find One by reviewid
reviewSchema.statics.findOneByreviewid = function (reviewid) {
  return this.findOne({ reviewid });
};

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

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

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