Skip to content

Instantly share code, notes, and snippets.

@tascott
Created October 5, 2016 09:49
Show Gist options
  • Save tascott/0e9fd572bc8b9e6ea74637c84f6cdcff to your computer and use it in GitHub Desktop.
Save tascott/0e9fd572bc8b9e6ea74637c84f6cdcff to your computer and use it in GitHub Desktop.
var mongoose = require("mongoose");
var mealSchema = mongoose.Schema({
name: String,
upvotes: { type: Number },
downvotes: { type: Number },
favourited_by: [{ type: mongoose.Schema.ObjectId, ref: 'User' }],
price: Number,
remove_for_safe: [{ type: String }],
add_for_taste: [{ type: String }],
suitable_for: [{ type: String }],
ingredients: [{ type: String }],
images: [{ type: String }],
official: Boolean
});
module.exports = mongoose.model('Meal', mealSchema);
var Meal = require("../models/meal");
var Vendor = require("../models/vendor");
var User = require("../models/user");
function mealsIndex(req, res){
Meal.find({}).exec(function(err, meals) {
if (err) return res.status(404).send(err);
res.status(200).send(meals);
});
}
function mealsCreate(req, res){
var newMeal = new Meal({
event: req.body.meal.event,
date: req.body.meal.date,
price: req.body.meal.price
})
newMeal.save(function(err){
if (err) return res.status(500).send(err);
var id = req.body.meal.id;
Vendor.findByIdAndUpdate(id, { $push: { meals: newMeal._id} }, function(err, vendor){
if (err) return res.status(404).json({message : err});
return res.status(200).send(newMeal);
});
});
}
// This creates takes the meal id and puts it to the vendor
function mealsTransfer(req, res){
var id = req.params.id;
var vendorID = req.body.package;
Meal.findById({ _id: id }, function(err, meal) {
if (err) return res.status(500).send(err);
if (!meal) return res.status(404).send(err);
meal.sold = true;
meal.save();
// res.status(200).send(meal);
User.findByIdAndUpdate(meal.userID, { $push: { meals: meal._id} }, function(err, user){
if (err) return res.status(404).json({message : err});
Vendor.findByIdAndUpdate(vendorID.vendor ,{ $pull: { meals: meal._id } }, function(err, vendor){
if (err) return res.status(404).json({message : err});
return res.status(200).json({ message: "Meal Released for sale to vendor ID: " + vendorID.vendor , vendor });
});
// Vendor.findById({ _id: vendorID.vendor }).populate("meals").exec(function(err, vendor) {
// if (err) return res.status(500).send(err);
// if (!vendor) return res.status(404).send(err);
// res.status(200).send(vendor);
// });
});
});
}
function mealsShow(req, res){
var id = req.params.id;
Meal.findById({ _id: id }, function(err, meal) {
if (err) return res.status(500).send(err);
if (!meal) return res.status(404).send(err);
res.status(200).send(meal);
})
}
function mealsUpdate(req, res){
var id = req.params.id;
Meal.findByIdAndUpdate({ _id: id }, req.body.meal, function(err, meal){
if (err) return res.status(500).send(err);
if (!meal) return res.status(404).send(err);
res.status(200).send(meal);
})
}
function mealsDelete(req, res){
var id = req.params.id;
Meal.findByIdAndRemove({ _id: id }, function(err) {
if (err) return res.status(500).send(err);
res.status(200).json({ message: "Meal deleted"});
})
}
module.exports = {
mealsIndex: mealsIndex,
mealsCreate: mealsCreate,
mealsShow: mealsShow,
mealsUpdate: mealsUpdate,
mealsDelete: mealsDelete,
mealsTransfer : mealsTransfer
}
var mongoose = require("mongoose");
var databaseURL = 'mongodb://localhost:27017/tda';
mongoose.connect(databaseURL);
// var Project = require("../models/project");
var User = require("../models/user");
var Vendor = require("../models/vendor");
var Meal = require("../models/meal");
// This will clear what ever we have in the database :)
User.collection.drop();
Vendor.collection.drop();
Meal.collection.drop();
var meal1 = new Meal({
name: "Nice Meal",
upvotes: 15,
downvotes: 3,
vendor: "Flat Iron",
favourited_by: [user1, user2],
price: 12,
remove_for_safe: ["onions", "cheese"],
add_for_taste: ["carrots"],
suitable_for: ["paleo", "fodmap", "SCD"],
ingredients: ["egg", "water", "cress"],
images: ["google.com/url"],
official: true
})
var meal2 = new Meal({
name: "Good meal",
upvotes: 35,
downvotes: 23,
vendor: "McDonalds",
favourited_by: [user1, user2, user3],
price: 6,
remove_for_safe: ["onions", "gherkins"],
add_for_taste: ["carrots", "peas", "water"],
suitable_for: ["paleo", "fodmap"],
ingredients: ["egg", "water", "GF Bread", "burger"],
images: ["google.com/url2"],
official: false
})
var meal3 = new Meal({
name: "Burger and chips",
upvotes: 5,
downvotes: 20,
vendor: "Reds True BBQ",
favourited_by: [user3],
price: 16,
remove_for_safe: ["garlic", "wheat"],
add_for_taste: ["asoefida", "orange", "pineapple"],
suitable_for: ["fodmap"],
ingredients: ["egg", "water", "GF dough", "beef"],
images: ["google.com/url3"],
official: true
})
var user1 = new User({
firstName: "Mike",
lastName: "Freyer",
userName: "aussieMike",
email: "[email protected]",
diet: "fodmap",
location: "London",
profile_photo: "imageurl",
saved_meals: [meal1, meal2],
saved_vendors: [vendor1]
})
var user2 = new User({
firstName: "Tom",
lastName: "Scott",
userName: "tascott",
email: "[email protected]",
diet: "fodmap",
location: "Leeds",
profile_photo: "imageurl2",
saved_meals: [meal1, meal3],
saved_vendors: [vendor1]
})
var user3 = new User({
firstName: "Paul",
lastName: "Crosby",
userName: "Crozza",
email: "[email protected]",
diet: "SCD",
location: "London",
profile_photo: "imageurl3",
saved_meals: [meal3],
saved_vendors: [vendor1, vendor2, vendor3]
})
var vendor1 = new Vendor({
companyName: "Reds",
contactName: "Butch",
userName: "Hook",
email: "[email protected]",
phone: "12345678910",
image: "url4",
caters_for: ["scd", "paleo", "fodmap"],
locations: ["N213BQ", "EN41RT"],
meals: [meal1, meal2]
})
var vendor2 = new Vendor({
companyName: "McDonalds",
contactName: "Tim Applebee",
userName: "TimA",
email: "[email protected]",
phone: "12345678910",
image: "url5",
caters_for: ["scd", "paleo", "fodmap"],
locations: ["N15TG", "EC41RT"],
meals: [meal2]
})
var vendor3 = new Vendor({
companyName: "Flat Iron",
contactName: "Phil",
userName: "Hook",
email: "bill@reds",
phone: "12345678910",
image: "url6",
caters_for: ["scd", "paleo", "fodmap"],
locations: ["NW16BG", "EN41RT"],
meals: [meal3]
})
// Save the users
user1.save(function(err, user) {
if (err) return console.log(err);
console.log("User saved! ", user);
})
user2.save(function(err, user) {
if (err) return console.log(err);
console.log("User saved! ", user);
})
user3.save(function(err, user) {
if (err) return console.log(err);
console.log("User saved! ", user);
})
// Save the Vendors
vendor1.save(function(err, vendor) {
if (err) return console.log(err);
console.log("Vendor saved! ", vendor);
})
vendor2.save(function(err, vendor) {
if (err) return console.log(err);
console.log("Vendor saved! ", vendor);
})
vendor3.save(function(err, vendor) {
if (err) return console.log(err);
console.log("Vendor saved! ", vendor);
})
meal1.save(function(err, meal) {
if (err) return console.log(err);
console.log("Meal saved! ", meal);
})
meal2.save(function(err, meal) {
if (err) return console.log(err);
console.log("Meal saved! ", meal);
})
meal3.save(function(err, meal) {
if (err) return console.log(err);
console.log("Meal saved! ", meal);
})
var mongoose = require("mongoose");
var bcrypt = require("bcrypt-nodejs");
var userSchema = mongoose.Schema({
firstName: { type: String, required: true },
lastName: { type: String, required: true },
userName: { type: String, required: true },
email: { type: String, required: true },
diet: { type: String, required: true },
location: { type: String, required: true },
profile_photo: String,
saved_meals: [{ type: mongoose.Schema.ObjectId, ref: 'Meal' }],
saved_vendors: [{ type: mongoose.Schema.ObjectId, ref: 'Vendor' }],
passwordHash: String
})
userSchema.set('toJSON', {
transform: function(document, json) {
delete json.passwordHash;
delete json.__v;
return json;
}
});
userSchema.virtual('password')
.set(function(password) {
this._password = password;
this.passwordHash = bcrypt.hashSync(this._password, bcrypt.genSaltSync(8));
});
userSchema.virtual('passwordConfirmation')
.set(function(passwordConfirmation) {
this._passwordConfirmation = passwordConfirmation;
});
userSchema.path('passwordHash')
.validate(function(passwordHash) {
if(!this._password) {
return this.invalidate('password', 'A password is required');
}
if(this._password !== this._passwordConfirmation) {
return this.invalidate('passwordConfirmation', 'Passwords do not match');
}
});
userSchema.methods.validatePassword = function(password) {
return bcrypt.compareSync(password, this.passwordHash);
}
module.exports = mongoose.model('User', userSchema);
var User = require("../models/user");
function usersIndex(req, res){
User.find({}).populate("meals").exec(function(err, users) {
if (err) return res.status(404).send(err);
res.status(200).send(users);
});
}
function usersCreate(req, res){
var user = new User(req.body.user);
user.save(function(err, user) {
if (err) return res.status(500).send(err);
res.status(201).send(user)
})
}
function usersShow(req, res){
var id = req.params.id;
User.findById({ _id: id }).populate("meals").exec(function(err, user) {
if (err) return res.status(500).send(err);
if (!user) return res.status(404).send(err);
res.status(200).send(user);
})
}
function usersUpdate(req, res){
var id = req.params.id;
User.findByIdAndUpdate({ _id: id }, req.body.user, function(err, user){
if (err) return res.status(500).send(err);
if (!user) return res.status(404).send(err);
res.status(200).send(user);
})
}
function usersDelete(req, res){
var id = req.params.id;
User.findByIdAndRemove({ _id: id }, function(err) {
if (err) return res.status(500).send(err);
res.status(200).json({ message: "User deleted"});
})
}
module.exports = {
usersIndex: usersIndex,
usersCreate: usersCreate,
usersShow: usersShow,
usersUpdate: usersUpdate,
usersDelete: usersDelete
}
var mongoose = require("mongoose");
var bcrypt = require("bcrypt-nodejs");
var vendorSchema = mongoose.Schema({
companyName: { type: String, required: true },
contactName: { type: String, required: true},
userName: { type: String, required: true },
email: String,
phone: String,
image: String,
caters_for: [{ type: String }],
locations: [{ type: String }],
meals: [{ type: mongoose.Schema.ObjectId, ref: 'Meal' }],
passwordHash: String
});
vendorSchema.set('toJSON', {
transform: function(document, json) {
delete json.passwordHash;
delete json.__v;
return json;
}
});
vendorSchema.virtual('password')
.set(function(password) {
this._password = password;
this.passwordHash = bcrypt.hashSync(this._password, bcrypt.genSaltSync(8));
});
vendorSchema.virtual('passwordConfirmation')
.set(function(passwordConfirmation) {
this._passwordConfirmation = passwordConfirmation;
});
vendorSchema.path('passwordHash')
.validate(function(passwordHash) {
if(!this._password) {
return this.invalidate('password', 'A password is required');
}
if(this._password !== this._passwordConfirmation) {
return this.invalidate('passwordConfirmation', 'Passwords do not match');
}
});
vendorSchema.methods.validatePassword = function(password) {
return bcrypt.compareSync(password, this.passwordHash);
}
module.exports = mongoose.model('Vendor', vendorSchema);
var Vendor = require("../models/vendor");
function vendorsIndex(req, res){
Vendor.find({}).populate("meals").exec(function(err, vendors) {
if (err) return res.status(404).send(err);
res.status(200).send(vendors);
});
}
function vendorsCreate(req, res){
var vendor = new Vendor(req.body.vendor);
vendor.save(function(err, vendor) {
if (err) return res.status(500).send(err);
res.status(201).send(vendor)
})
}
function vendorsShow(req, res){
var id = req.params.id;
Vendor.findById({ _id: id }).populate("meals").exec(function(err, vendor) {
if (err) return res.status(500).send(err);
if (!vendor) return res.status(404).send(err);
res.status(200).send(vendor);
})
}
function vendorsUpdate(req, res){
var id = req.params.id;
Vendor.findByIdAndUpdate({ _id: id }, req.body.vendor, function(err, vendor){
if (err) return res.status(500).send(err);
if (!vendor) return res.status(404).send(err);
res.status(200).send(vendor);
})
}
function vendorsDelete(req, res){
var id = req.params.id;
Vendor.findByIdAndRemove({ _id: id }, function(err) {
if (err) return res.status(500).send(err);
res.status(200).json({ message: "Vendor deleted"});
})
}
module.exports = {
vendorsIndex: vendorsIndex,
vendorsCreate: vendorsCreate,
vendorsShow: vendorsShow,
vendorsUpdate: vendorsUpdate,
vendorsDelete: vendorsDelete
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment