Skip to content

Instantly share code, notes, and snippets.

@doncams
Forked from Connorelsea/pleaseHelp.js
Last active November 13, 2015 03:27
Show Gist options
  • Select an option

  • Save doncams/d6e153f74cef5b4b8dab to your computer and use it in GitHub Desktop.

Select an option

Save doncams/d6e153f74cef5b4b8dab to your computer and use it in GitHub Desktop.
Promise.coroutine(function* () {
var currentDate = moment().format();
var dates = [];
for (var i = 1; i < days + 1; i++) {
dates.push(moment().subtract(i, "days"));
}
var dateObjects = [];
dates.forEach(date => dateObjects.push({
date : date,
meals : {}
}));
var query = `SELECT FR.id, FR.rating, FR.body, FR.googleID, FR.date, FR.meal
FROM food AS FR
ORDER BY FR.date DESC`;
let container = yield Promise.fromCallback(function(callback) {
connection.query(query, (err, rows) => {
if (err) callback(err);
else callback(null, {dateObjects, rows});
}
}) );
console.log("CONTAINER: " + container)
var dateObjects = container.dateObjects;
var rows = container.rows;
console.log("HERE: " + dateObjects);
// Cycle through each date object in that range
dateObjects.forEach(object => {
// Get the reviews that match the date of the current date object
// and then fill that date object with those reviews.
var reviews = rows.filter(date => {
return date.toDateString() === object.date.toDateString()
});
// Filter the reviews into their respective meal arrays
var breakfast = reviews.filter(r => { return r.meal === "BREAKFAST" });
var brunch = reviews.filter(r => { return r.meal === "BRUNCH" });
var lunch = reviews.filter(r => { return r.meal === "LUNCH" });
var dinner = reviews.filter(r => { return r.meal === "DINNER" });
// Populate the dataObject with their meal arrays
object.breakfast = breakfast;
object.brunch = brunch;
object.lunch = lunch;
object.dinner = dinner;
});
console.log("DATE OBJECTS: \n\n" + JSON.stringify(dateObjects, 2, 2));
})()
.catch(error => {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment