-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from YashdalfTheGray/master-author
REST API, promises, and services layer rewrites
- Loading branch information
Showing
35 changed files
with
1,510 additions
and
1,363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,172 @@ | ||
/* global exports */ | ||
|
||
"use strict"; | ||
|
||
var chalk = require('chalk'), | ||
mongodb = require('mongodb'), | ||
UserModel = require('./server/users/userModel'), | ||
IdeaModel = require('./server/ideas/ideaModel'), | ||
EventModel = require('./server/events/eventModel'); | ||
|
||
function getPreviousWeek(originalDate){ | ||
function getPreviousWeek(originalDate) { | ||
var lastWeek = new Date(originalDate.getFullYear(), originalDate.getMonth(), originalDate.getDate() - 7); | ||
return lastWeek ; | ||
return lastWeek; | ||
} | ||
|
||
function getFollowingWeek(originalDate){ | ||
function getFollowingWeek(originalDate) { | ||
var nextWeek = new Date(originalDate.getFullYear(), originalDate.getMonth(), originalDate.getDate() + 7); | ||
return nextWeek ; | ||
return nextWeek; | ||
} | ||
|
||
var DB = new mongodb.Db('flintandsteel-dev', new mongodb.Server('localhost', 27017)); | ||
|
||
var userIds = []; | ||
var eventIds = []; | ||
var now = ISODate(), | ||
userIds = [], | ||
eventIds = []; | ||
|
||
exports.getUserIds = function getUserIds() { | ||
return userIds; | ||
}; | ||
|
||
exports.getEventIds = function getEventIds() { | ||
return eventIds; | ||
}; | ||
db = connect('localhost:27017/flintandsteel-dev'); | ||
|
||
exports.generateUsers = function generateUsers(cb) { | ||
DB.open(function(err, db) { | ||
var userObjs = []; | ||
userObjs.push(UserModel.create("Guybrush", "Threepwood", "Guybrush Threepwood", "test1", "[email protected]", "Threepy", "Hippy Lumberjack")); | ||
userObjs.push(UserModel.create("Rick", "Sanchez", "Rick Sanchez", "test2", "[email protected]", "Dirty", "Street Pharmacist")); | ||
userObjs.push(UserModel.create("Dick", "Dickerson", "Dick Dickerson", "test3", "[email protected]", "The Fracker", "Money Bags Oil Man")); | ||
db.collection('users').insert(userObjs, | ||
function(err, results) { | ||
db.close(); | ||
if (err) { | ||
console.log(chalk.bgRed(err)); | ||
cb(err); | ||
return; | ||
} | ||
else { | ||
console.log(chalk.bgGreen('Users created in the users collection.')); | ||
userIds = userIds.concat(results.insertedIds); | ||
cb(null, userIds); | ||
} | ||
} | ||
); | ||
}); | ||
}; | ||
// create collections | ||
db.createCollection('users'); | ||
db.createCollection('ideas'); | ||
db.createCollection('comments'); | ||
db.createCollection('events'); | ||
|
||
exports.generateEvents = function generateEvents(cb) { | ||
DB.open(function(err, db) { | ||
var now = new Date(); | ||
var eventObjs = []; | ||
eventObjs.push(EventModel.create("In Progress Event 1", "USMAY", now.toISOString(), getFollowingWeek(now).toISOString())); | ||
eventObjs.push(EventModel.create("In Progress Event 2", "USMKE", now.toISOString(), getFollowingWeek(now).toISOString())); | ||
eventObjs.push(EventModel.create("Completed Event 1", "USTWB", getPreviousWeek(getPreviousWeek(now)).toISOString(), getPreviousWeek(now).toISOString())); | ||
db.collection('events').insert(eventObjs, | ||
function(err, results) { | ||
db.close(); | ||
if (err) { | ||
console.log(chalk.bgRed(err)); | ||
cb(err); | ||
return; | ||
} | ||
else { | ||
console.log(chalk.bgGreen('Events created in the events collection.')); | ||
eventIds = eventIds.concat(results.insertedIds); | ||
cb(null, eventIds); | ||
} | ||
} | ||
); | ||
}); | ||
}; | ||
// create users | ||
var insertResult = db.users.insert( | ||
[ | ||
{ | ||
firstName: 'Guybrush', | ||
lastName: 'Threepwood', | ||
fullName: 'Guybrush Threepwood', | ||
username: 'test1', | ||
email: '[email protected]', | ||
nickname: 'Threepy', | ||
title: 'Hippy Lumberjack' | ||
}, | ||
{ | ||
firstName: 'Rick', | ||
lastName: 'Sanchez', | ||
fullName: 'Rick Sanchez', | ||
username: 'test2', | ||
email: '[email protected]', | ||
nickname: 'Dirty', | ||
title: 'Street Pharmacist' | ||
}, | ||
{ | ||
firstName: 'Dick', | ||
lastName: 'Dickerson', | ||
fullName: 'Dick Dickerson', | ||
username: 'test3', | ||
email: '[email protected]', | ||
nickname: 'The Fracker', | ||
title: 'Money Bags Oil Man' | ||
}, | ||
{ | ||
firstName: 'Test', | ||
lastName: 'testerson', | ||
fullName: 'Test Testerson', | ||
username: 'test4', | ||
email: '[email protected]', | ||
nickname: 'The Tester', | ||
title: 'Testin\' All Day' | ||
} | ||
] | ||
); | ||
print('INSERT RESULT: Inserted ' + insertResult.nInserted + ' documents into users collection.'); | ||
|
||
exports.generateIdeas = function generateIdeas(cb) { | ||
DB.open(function(err, db) { | ||
var ideaObjs = []; | ||
ideaObjs.push(IdeaModel.create("Guybrush's Test Idea", "This is an idea description.", userIds[0], eventIds[0], [], [])); | ||
ideaObjs.push(IdeaModel.create("Rick's Test Idea", "This is Mr. Sanchez\'s brilliant idea.", userIds[1], eventIds[1], [], [])); | ||
ideaObjs.push(IdeaModel.create("Dick's Test Idea", "This is \"The Fracker\'s\" master plan.", userIds[2], eventIds[2], [], [])); | ||
db.collection('ideas').insert(ideaObjs, | ||
function(err) { | ||
db.close(); | ||
if (err) { | ||
console.log(chalk.bgRed(err)); | ||
cb(err); | ||
return; | ||
} | ||
else { | ||
console.log(chalk.bgGreen('Ideas created in the ideas collection.')); | ||
cb(null); | ||
} | ||
} | ||
); | ||
}); | ||
}; | ||
// get the user ids | ||
db.users.find({}, { _id: 1 }).forEach(function(user) { | ||
userIds.push(user._id); | ||
}); | ||
|
||
exports.generateAll = function generateAll(cb) { | ||
exports.generateUsers(function(err) { | ||
if (!err) { | ||
exports.generateEvents(function(err) { | ||
if (!err) { | ||
exports.generateIdeas(function(err) { | ||
cb(null); | ||
return; | ||
}); | ||
} | ||
else { | ||
cb(err); | ||
return; | ||
} | ||
}); | ||
// create events | ||
insertResult = db.events.insert( | ||
[ | ||
{ | ||
name: 'In Progress Event 1', | ||
location: 'USMAY', | ||
startDate: now, | ||
endDate: getFollowingWeek(now) | ||
}, | ||
{ | ||
name: 'In Progress Event 2', | ||
location: 'USMKE', | ||
startDate: now, | ||
endDate: getFollowingWeek(now) | ||
}, | ||
{ | ||
name: 'Completed Event 1', | ||
location: 'USTWB', | ||
startDate: getPreviousWeek(getPreviousWeek(now)), | ||
endDate: getPreviousWeek(now) | ||
} | ||
else { | ||
cb(err); | ||
return; | ||
] | ||
); | ||
print('INSERT RESULT: Inserted ' + insertResult.nInserted + ' documents into events collection.'); | ||
|
||
// get the event ids | ||
db.events.find({}, { _id: 1 }).forEach(function(user) { | ||
eventIds.push(user._id); | ||
}); | ||
|
||
// create ideas | ||
insertResult = db.ideas.insert( | ||
[ | ||
{ | ||
title: 'Guybrush\'s Test Idea', | ||
description: 'This is an idea description.', | ||
authorId: userIds[0], | ||
eventId: eventIds[0], | ||
timeCreated: now, | ||
timeModified: now, | ||
tags: [], | ||
rolesreq: [], | ||
likes: [], | ||
updates: [], | ||
comments: [], | ||
backs: [{ | ||
_id: new ObjectId(), | ||
text: 'Idea Owner', | ||
authorId: userIds[0], | ||
time: ISODate(), | ||
types: [{ name: 'Owner', _lowername: 'owner' }] | ||
}], | ||
team: [{ _id: new ObjectId(), memberId: userIds[0] }] | ||
}, | ||
{ | ||
title: 'Rick\'s Test Idea', | ||
description: 'This is Mr. Sanchez\'s brilliant idea.', | ||
authorId: userIds[1], | ||
eventId: eventIds[1], | ||
timeCreated: now, | ||
timeModified: now, | ||
tags: [], | ||
rolesreq: [], | ||
likes: [], | ||
updates: [], | ||
comments: [], | ||
backs: [{ | ||
_id: new ObjectId(), | ||
text: 'Idea Owner', | ||
authorId: userIds[1], | ||
time: ISODate(), | ||
types: [{ name: 'Owner', _lowername: 'owner' }] | ||
}], | ||
team: [{ _id: new ObjectId(), memberId: userIds[1] }] | ||
}, | ||
{ | ||
title: 'Dick\'s Test Idea', | ||
description: 'This is "The Fracker\'s" master plan.', | ||
authorId: userIds[2], | ||
eventId: eventIds[2], | ||
timeCreated: now, | ||
timeModified: now, | ||
tags: [], | ||
rolesreq: [], | ||
likes: [], | ||
updates: [], | ||
comments: [], | ||
backs: [{ | ||
_id: new ObjectId(), | ||
text: 'Idea Owner', | ||
authorId: userIds[2], | ||
time: ISODate(), | ||
types: [{ name: 'Owner', _lowername: 'owner' }] | ||
}], | ||
team: [{ _id: new ObjectId(), memberId: userIds[2] }] | ||
} | ||
}); | ||
}; | ||
] | ||
); | ||
print('INSERT RESULT: Inserted ' + insertResult.nInserted + ' documents into ideas collection.'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.