Merged PetitTetonApps with the website now that the deployment server always uses SSL. Used the Apps more recent implementation of NodeJS/Express.
This commit is contained in:
114
app/initialData.js
Normal file
114
app/initialData.js
Normal file
@@ -0,0 +1,114 @@
|
||||
|
||||
module.exports = function(sequelize) {
|
||||
var models = sequelize.models;
|
||||
|
||||
//Pre-populate a new database with some data.
|
||||
models.Category.count().then(function(count) {
|
||||
if(count == 0) {
|
||||
var basicJarIds = [];
|
||||
|
||||
models.Venue.create({name: 'Boonville'});
|
||||
models.Venue.create({name: 'Clement St Farmers Market in SF'});
|
||||
models.Venue.create({name: 'Ukiah Farmers Market'});
|
||||
models.Venue.create({name: 'Mendocino Farmers Market'});
|
||||
models.Venue.create({name: 'Ft Bragg Farmers Market'});
|
||||
models.Venue.create({name: 'Healdsburg Farmers Market'});
|
||||
|
||||
Promise.each([
|
||||
models.Measure.create({name: 'Jar 4oz', postfix: '4oz'}),
|
||||
models.Measure.create({name: 'Jar 8oz', postfix: '8oz'}),
|
||||
models.Measure.create({name: 'Jar 12oz', postfix: '12oz'}),
|
||||
models.Measure.create({name: 'Jar 16oz', postfix: '16oz'}),
|
||||
models.Measure.create({name: 'Jar 32oz', postfix: '32oz'}),
|
||||
models.Measure.create({name: 'Jar 64oz', postfix: '64oz'}),
|
||||
models.Measure.create({name: 'Pounds', postfix: 'lbs'}),
|
||||
models.Measure.create({name: 'Each', postfix: ''}),
|
||||
models.Measure.create({name: 'Bags', postfix: 'bags'}),
|
||||
], function(value, index, length) {
|
||||
//Collect the first 5 jar ids.
|
||||
if(index < 5) basicJarIds.push(value.id);
|
||||
}).then(function() {
|
||||
models.Category.create({name: 'VAP'}).then(function(category) {
|
||||
models.Subcategory.create({name: 'Soups'}).then(function(subcategory) {
|
||||
category.addSubcategory(subcategory);
|
||||
models.Item.create({name: 'Fava Bean Bisque', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Tomato Basil Soup', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Winter Squash Soup', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
});
|
||||
models.Subcategory.create({name: 'Drink Mixes & Syrups'}).then(function(subcategory) {
|
||||
category.addSubcategory(subcategory);
|
||||
models.Item.create({name: 'Bloody Mary Mix', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Grape Syrup', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Prickly Pear Syrup', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Quince Syrup', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Strawberry Syrup', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Wild Plum Syrup', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
});
|
||||
models.Subcategory.create({name: 'Fermented'}).then(function(subcategory) {
|
||||
category.addSubcategory(subcategory);
|
||||
models.Item.create({name: 'Napa Cabbage Sauerkraut', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Napa Cabbage Sauerkraut w/ Watercress & Espelette Pepper', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Red Sauerkraut', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
});
|
||||
models.Subcategory.create({name: 'Dried Goods'}).then(function(subcategory) {
|
||||
category.addSubcategory(subcategory);
|
||||
models.Item.create({name: 'Dried Strawberries', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Membrillo', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Sugared Jalape<70>os', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
});
|
||||
models.Subcategory.create({name: 'Spices'}).then(function(subcategory) {
|
||||
category.addSubcategory(subcategory);
|
||||
models.Item.create({name: 'Basque Pepper Powder', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Korean Pepper Powder', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Smoked Basque Pepper Powder', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
models.Item.create({name: 'Smoked Korean Pepper Powder', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
});
|
||||
models.Subcategory.create({name: 'Specialty'}).then(function(subcategory) {
|
||||
category.addSubcategory(subcategory);
|
||||
models.Item.create({name: 'Pure Lard', counts: COUNTS_JAR}).then(function(item) {
|
||||
subcategory.addItem(item);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
31
app/models/category.js
Normal file
31
app/models/category.js
Normal file
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
//The id field is auto added and made primary key.
|
||||
var Category = sequelize.define('Category', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
autoIncrement: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
visible: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: true
|
||||
}
|
||||
}, {
|
||||
freezeTableName: true, // Model tableName will be the same as the model name
|
||||
classMethods: {
|
||||
associate: function(models) {
|
||||
Category.hasMany(models.Subcategory, {as: 'subcategories', foreignKey: {name: 'categoryId', field: 'categoryId'}});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Category;
|
||||
};
|
||||
32
app/models/index.js
Normal file
32
app/models/index.js
Normal file
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var Sequelize = require("sequelize");
|
||||
var env = process.env.NODE_ENV || "development";
|
||||
//var config = require(__dirname + '/../config/config.json')[env];
|
||||
var configDB = require('./../../config/database.js');
|
||||
//var sequelize = new Sequelize(config.database, config.username, config.password, config);
|
||||
var sequelize = new Sequelize(configDB.url);
|
||||
var db = {};
|
||||
|
||||
fs
|
||||
.readdirSync(__dirname)
|
||||
.filter(function(file) {
|
||||
return (file.indexOf(".") !== 0) && (file !== "index.js");
|
||||
})
|
||||
.forEach(function(file) {
|
||||
var model = sequelize.import(path.join(__dirname, file));
|
||||
db[model.name] = model;
|
||||
});
|
||||
|
||||
Object.keys(db).forEach(function(modelName) {
|
||||
if ("associate" in db[modelName]) {
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
db.sequelize = sequelize;
|
||||
db.Sequelize = Sequelize;
|
||||
|
||||
module.exports = db;
|
||||
35
app/models/item.js
Normal file
35
app/models/item.js
Normal file
@@ -0,0 +1,35 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
//The id field is auto added and made primary key.
|
||||
var Item = sequelize.define('Item', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
autoIncrement: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
counts: {
|
||||
type: DataTypes.JSON,
|
||||
allowNull: false
|
||||
},
|
||||
visible: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: true
|
||||
}
|
||||
}, {
|
||||
freezeTableName: true, // Model tableName will be the same as the model name
|
||||
classMethods: {
|
||||
associate: function(models) {
|
||||
Item.belongsTo(models.Subcategory, {as: 'subcategory', foreignKey: {name: 'subcategoryId', field: 'subcategoryId'}});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Item;
|
||||
};
|
||||
32
app/models/measure.js
Normal file
32
app/models/measure.js
Normal file
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
//The id field is auto added and made primary key.
|
||||
return sequelize.define('Measure', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
autoIncrement: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
image: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true
|
||||
},
|
||||
postfix: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
visible: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: true
|
||||
}
|
||||
}, {
|
||||
freezeTableName: true // Model tableName will be the same as the model name
|
||||
});
|
||||
};
|
||||
33
app/models/sale.js
Normal file
33
app/models/sale.js
Normal file
@@ -0,0 +1,33 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
//The id field is auto added and made primary key.
|
||||
var Sale = sequelize.define('Sale', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
autoIncrement: true
|
||||
},
|
||||
date: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false
|
||||
},
|
||||
measure: {
|
||||
type: DataTypes.JSONB,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
freezeTableName: true, // Model tableName will be the same as the model name
|
||||
classMethods: {
|
||||
associate: function(models) {
|
||||
//Sale.hasOne(models.Category, {as: 'category'});
|
||||
//Sale.hasOne(models.Subcategory, {as: 'subcategory'});
|
||||
Sale.belongsTo(models.Item, {as: 'item', foreignKey: {name: 'itemId', field: 'itemId'}});
|
||||
Sale.belongsTo(models.Venue, {as: 'venue', foreignKey: {name: 'venueId', field: 'venueId'}});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Sale;
|
||||
};
|
||||
34
app/models/subcategory.js
Normal file
34
app/models/subcategory.js
Normal file
@@ -0,0 +1,34 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
//The id field is auto added and made primary key.
|
||||
var Subcategory = sequelize.define('Subcategory', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
field: 'id',
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
autoIncrement: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
field: 'name',
|
||||
allowNull: false
|
||||
},
|
||||
visible: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: true
|
||||
}
|
||||
}, {
|
||||
freezeTableName: true, // Model tableName will be the same as the model name
|
||||
classMethods: {
|
||||
associate: function(models) {
|
||||
Subcategory.belongsTo(models.Category, {as: 'category', foreignKey: {name: 'categoryId', field: 'categoryId'}});
|
||||
Subcategory.hasMany(models.Item, {as: 'items', foreignKey: {name: 'subcategoryId', field: 'subcategoryId'}});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Subcategory;
|
||||
};
|
||||
25
app/models/user.js
Normal file
25
app/models/user.js
Normal file
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
|
||||
var bcrypt = require('bcrypt-nodejs');
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
//The id field is auto added and made primary key.
|
||||
return sequelize.define('User', {
|
||||
login: {
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING
|
||||
}
|
||||
}, {
|
||||
freezeTableName: true, // Model tableName will be the same as the model name
|
||||
instanceMethods: {
|
||||
generateHash: function(password) {
|
||||
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
|
||||
},
|
||||
validPassword: function(password) {
|
||||
return bcrypt.compareSync(password, this.password);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
24
app/models/venue.js
Normal file
24
app/models/venue.js
Normal file
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function(sequelize, DataTypes) {
|
||||
//The id field is auto added and made primary key.
|
||||
return sequelize.define('Venue', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
autoIncrement: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
visible: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: true
|
||||
}
|
||||
}, {
|
||||
freezeTableName: true // Model tableName will be the same as the model name
|
||||
});
|
||||
};
|
||||
242
app/routes.js
Normal file
242
app/routes.js
Normal file
@@ -0,0 +1,242 @@
|
||||
var ejs = require('ejs');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var adminPath;
|
||||
|
||||
//Notes:
|
||||
//Use res.send or res.sendFile for static resources (like images or html)
|
||||
//Use res.send(ejs.render(htmlStr, viewArgs)) to manually render EJS files
|
||||
//Use res.render("my.ejs", {root: adminPath}) to render EJS files (if you have setup the ejs renderer
|
||||
|
||||
module.exports = function(app, rootPath, passport, smtpTransport, sequelize) {
|
||||
adminPath = path.join(rootPath, 'admin');
|
||||
|
||||
// =====================================
|
||||
// HOME PAGE (with login links)
|
||||
// =====================================
|
||||
//app.get('/', isLoggedIn, function(req, res) {
|
||||
// res.render('index.ejs'); // load the index.ejs file
|
||||
//});
|
||||
//Handle the root being requested, and the search engine requesting a static page with content.
|
||||
app.get('/', function(req, res) {
|
||||
try {
|
||||
//Note: This is for search engines.
|
||||
if(typeof(req.query._escaped_fragment_) !== "undefined") {
|
||||
console.log("Search Engine Detected");
|
||||
var viewArgs = {}; //What args to use for a search engine?
|
||||
|
||||
//The DIY method which is somewhat brittle since it relies on <!--CONTENT--> existing in the index file, and it replaces that with the contents of the passed parameter (what is after the #!) for the content html which is inserted into the index file in place of <!--CONTENT-->.
|
||||
fs.readFile(rootPath + '/index.html', {encoding: "UTF8"}, function(err, indexContent) {
|
||||
if(!err) {
|
||||
var file = rootPath + '/' + req.query._escaped_fragment_ + '.html';
|
||||
|
||||
fs.readFile(file, {encoding: "UTF8"}, function(err, content) {
|
||||
if(!err) {
|
||||
//Non-regex method.//
|
||||
if(content.indexOf("<runonce>") != -1 && content.indexOf("</runonce>") != -1) {
|
||||
content = content.substr(0, content.indexOf("<runonce>")) + content.substr(content.indexOf("</runonce>") + 10, -1);
|
||||
}
|
||||
//Doesn't work? Not sure why. Works in the regex test tools.//
|
||||
//content = content.replace(/<runonce>(.|\n)*?<\x2frunonce>/, " ");
|
||||
|
||||
//Doesn't work? Based on the regex failure above, I think that replace is failing.//
|
||||
var html = indexContent.replace(/<!--CONTENT-->/g, content);
|
||||
|
||||
//console.log(html);
|
||||
//res.send(ejs.render(html, viewArgs));
|
||||
res.send(html);
|
||||
}
|
||||
else console.log("Error reading the content file '" + file + "'. " + err);
|
||||
});
|
||||
}
|
||||
else console.log("Error reading the index.html file. " + err);
|
||||
});
|
||||
}
|
||||
else {
|
||||
//res.render("index.html", {root: rootPath});
|
||||
res.sendFile("index.html", {root: rootPath});
|
||||
}
|
||||
} catch(e) {
|
||||
console.log(e);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/admin', isLoggedIn, function(req, res) {
|
||||
try {
|
||||
//Note: This is for search engines.
|
||||
if(typeof(req.query._escaped_fragment_) !== "undefined") {
|
||||
var viewArgs = {}; //What args to use for a search engine?
|
||||
|
||||
//The DIY method which is somewhat brittle since it relies on <!--CONTENT--> existing in the index file, and it replaces that with the contents of the passed parameter (what is after the #!) for the content html which is inserted into the index file in place of <!--CONTENT-->.
|
||||
fs.readFile(adminPath + '/index.ejs', {encoding: "UTF8"}, function(err, indexContent) {
|
||||
if(!err) {
|
||||
var file = adminPath + '/' + req.query._escaped_fragment_ + '.ejs';
|
||||
|
||||
fs.readFile(file, {encoding: "UTF8"}, function(err, content) {
|
||||
if(!err) {
|
||||
//Non-regex method.//
|
||||
if(content.indexOf("<runonce>") != -1 && content.indexOf("</runonce>") != -1) {
|
||||
content = content.substr(0, content.indexOf("<runonce>")) + content.substr(content.indexOf("</runonce>") + 10, -1);
|
||||
}
|
||||
//Doesn't work? Not sure why. Works in the regex test tools.//
|
||||
//content = content.replace(/<runonce>(.|\n)*?<\x2frunonce>/, " ");
|
||||
|
||||
//Doesn't work? Based on the regex failure above, I think that replace is failing.//
|
||||
var html = indexContent.replace(/<!--CONTENT-->/g, content);
|
||||
|
||||
//console.log(html);
|
||||
res.send(ejs.render(html, viewArgs));
|
||||
}
|
||||
else console.log("Error reading the content file '" + file + "'. " + err);
|
||||
});
|
||||
}
|
||||
else console.log("Error reading the index.ejs file. " + err);
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log("Looking for index.ejs in " + adminPath);
|
||||
//res.render("index.ejs", {root: adminPath});
|
||||
res.render(path.join(adminPath, req.baseUrl, "index"));
|
||||
}
|
||||
} catch(e) {console.log(e);}
|
||||
});
|
||||
|
||||
app.use('/ContactUs', function(req, res) {
|
||||
try {
|
||||
var firstName = req.body.FirstName;
|
||||
var lastName = req.body.LastName;
|
||||
var email = req.body.Email;
|
||||
var message = req.body.Text;
|
||||
var params = {from: config.fromAddress, to: config.contactUsRecipient, subject: "Contact Us", text: "A user has commented via the Petit Teton website.\n\nFirst Name: " + firstName + "\nLast Name: " + lastName + "\nEmail: " + email + "\n" + message};
|
||||
|
||||
smtpTransport.sendMail(params, function(error, response) {
|
||||
if(error) {
|
||||
try {
|
||||
console.log("Received an error while sending the contact us email to the admin. " + error);
|
||||
fs.appendFile(rootPath + '/emailFailures.txt', JSON.stringify(params) + '\n', function(err) {if(err) {console.log("Failed to write email data to file! (contact us)");}});
|
||||
} catch(e) {console.log(e);}
|
||||
}
|
||||
});
|
||||
|
||||
res.status(200).send('success');
|
||||
} catch(e) {console.log(e);}
|
||||
});
|
||||
|
||||
// =====================================
|
||||
// LOGIN
|
||||
// =====================================
|
||||
// show the login form
|
||||
app.get('/admin/login', function(req, res) {
|
||||
// render the page and pass in any flash data if it exists
|
||||
res.render(path.join(adminPath, req.baseUrl, 'login.ejs'), { message: req.flash('loginMessage') });
|
||||
});
|
||||
|
||||
// process the login form
|
||||
app.post('/admin/login', passport.authenticate('local-login', {successRedirect: '/admin', failureRedirect: '/admin/login', failureFlash: true}));
|
||||
|
||||
// =====================================
|
||||
// SIGNUP
|
||||
// =====================================
|
||||
// show the signup form
|
||||
/* Turned off since only admin users can add admin users.
|
||||
app.get('/admin/signup', function(req, res) {
|
||||
|
||||
// render the page and pass in any flash data if it exists
|
||||
res.render(path.join(adminPath, req.baseUrl, 'signup.ejs'), { message: req.flash('signupMessage') });
|
||||
});
|
||||
|
||||
app.post('/admin/signup', passport.authenticate('local-signup', {successRedirect: '/admin', failureRedirect: '/admin/signup', failureFlash: true}));
|
||||
*/
|
||||
// =====================================
|
||||
// PROFILE SECTION
|
||||
// =====================================
|
||||
// we will want this protected so you have to be logged in to visit
|
||||
// we will use route middleware to verify this (the isLoggedIn function)
|
||||
app.get('/admin/profile', isLoggedIn, function(req, res) {
|
||||
res.render(path.join(adminPath, req.baseUrl, 'profile.ejs'), {
|
||||
user : req.user // get the user out of session and pass to template
|
||||
});
|
||||
});
|
||||
|
||||
// =====================================
|
||||
// LOGOUT
|
||||
// =====================================
|
||||
app.get('/admin/logout', function(req, res) {
|
||||
req.logout();
|
||||
res.redirect('/');
|
||||
});
|
||||
|
||||
// Check for an ejs first even if an html is requested.
|
||||
app.get('/admin/**/*.html', isLoggedIn, function(req, res) {
|
||||
var ejs = req.path.substring(0, req.path.length - 4) + ".ejs";
|
||||
|
||||
//console.log("Checking for an ejs: " + ejs);
|
||||
|
||||
fs.stat(ejs, function(err, stats) {
|
||||
if(!err) {
|
||||
res.render(ejs);
|
||||
}
|
||||
else {
|
||||
res.sendFile(req.path);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/admin/getCategories', isLoggedIn, function(req, res) {
|
||||
sequelize.models.Category.findAll({attributes: ['id', 'name', 'visible'], order: [['name', 'DESC'], ['visible', 'DESC']]}).then(function(values) {
|
||||
res.json(values);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/admin/getSubcategories', isLoggedIn, function(req, res) {
|
||||
sequelize.models.Subcategory.findAll({where: {categoryId: req.query.id}, attributes: ['id', 'name', 'visible'], order: [['name', 'DESC'], ['visible', 'DESC']]}).then(function(values) {
|
||||
res.json(values);
|
||||
}).catch(function(error) {
|
||||
console.log(error);
|
||||
res.json("[]");
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/admin/getItems', isLoggedIn, function(req, res) {
|
||||
sequelize.models.Item.findAll({where: {subcategoryId: req.query.id}, attributes: ['id', 'name', 'counts', 'visible', 'subcategoryId'], order: [['name', 'DESC'], ['visible', 'DESC']]}).then(function(values) {
|
||||
res.json(values);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/admin/getMeasures', isLoggedIn, function(req, res) {
|
||||
sequelize.models.Measure.findAll({attributes: ['id', 'name', "image", 'postfix', 'visible'], order: [['name', 'DESC'], ['visible', 'DESC']]}).then(function(values) {
|
||||
res.json(values);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/admin/getVenues', isLoggedIn, function(req, res) {
|
||||
sequelize.models.Venue.findAll({attributes: ['id', 'name', 'visible'], order: [['name', 'DESC'], ['visible', 'DESC']]}).then(function(values) {
|
||||
res.json(values);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/admin/toggleVenueVisibility', isLoggedIn, function(req, res) {
|
||||
sequelize.models.Venue.find({where: {id: req.query.id}, attributes: ['id', 'name', 'visible']}).then(function(venue) {
|
||||
if(venue) {
|
||||
venue.visible = venue.visible ? false : true;
|
||||
venue.save().then(function() {
|
||||
res.json({visible: venue.visible});
|
||||
}).catch(function(error) {
|
||||
res.json({error: error});
|
||||
});
|
||||
}
|
||||
else {
|
||||
res.json({error: "Can't find the venue!"});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
// route middleware to make sure a user is logged in
|
||||
function isLoggedIn(req, res, next) {
|
||||
if(req.isAuthenticated()) return next();
|
||||
|
||||
//Redirect if the user isn't logged in.
|
||||
res.redirect('/admin/login');
|
||||
}
|
||||
Reference in New Issue
Block a user