Home Reference Source Repository

src/models/beer.js

// Import the neccesary modules.
import mongoose from 'mongoose';

/**
 * The beer schema used by mongoose.
 * @type {Schema}
 * @see http://mongoosejs.com/docs/guide.html
 */
const _beerSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    index: {
      unique: true
    }
  },
  style: {
    type: String,
    required: true
  },
  type: {
    type: String,
    required: true
  },
  abv: {
    type: Number,
    required: true
  },
  ebu: {
    type: Number,
    required: true
  },
  thumbnail: {
    type: String,
    required: true
  },
  votes: {
    type: Number,
    default: 0
  },
  user: {
    type: String,
    ref: 'User'
  }
});

/**
 * Decrease the votes of a beer.
 * @returns {Promise} - Promise to save the beer.
 */
function downvote() {
  this.votes--;
  return this.save();
}

/**
 * Increase the votes of a beer.
 * @returns {Promise} - Promise to save the beer.
 */
function upvote() {
  this.votes++;
  return this.save();
}

// Adding the functions to the beer model.
_beerSchema.methods.downvote = downvote;
_beerSchema.methods.upvote = upvote;

/**
 * A model object for beers.
 * @type {Beer}
 */
export default mongoose.model('Beer', _beerSchema);