-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseed.js
175 lines (153 loc) · 5.93 KB
/
seed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
This seed file is only a placeholder. It should be expanded and altered
to fit the development of your application.
It uses the same file the server uses to establish
the database connection:
--- server/db/index.js
The name of the database used is set in your environment files:
--- server/env/*
This seed file has a safety check to see if you already have users
in the database. If you are developing multiple applications with the
fsg scaffolding, keep in mind that fsg always uses the same database
name in the environment files.
*/
var mongoose = require('mongoose');
var Promise = require('bluebird');
var chalk = require('chalk');
var connectToDb = require('./server/db');
var User = Promise.promisifyAll(mongoose.model('User'));
var Event = Promise.promisifyAll(mongoose.model('Event'));
var Chance = require('chance');
var chance = new Chance();
var moment = require('moment');
moment().format();
var getRandomLocation = function() { //change this later to take a sport and return a location that makes sense for the sport input.
var locations = [
{name: 'Central Park', coords: {latitude: 40.771606, longitude: -73.974819}},
{name: 'Chelsea Piers Sports Center', coords: {latitude: 40.746617, longitude: -74.010184}},
{name: 'Hudson River Park', coords: {latitude: 40.727127, longitude: -74.011334}},
{name: 'Central Park Great Lawn', coords: {latitude: 40.781389, longitude: -73.966553}},
{name: 'Nelson Rockefeller Park', coords: {latitude: 40.716920, longitude: -74.016867}},
{name: '"The Cage" W4 St Basketball Courts', coords: {latitude: 40.731041, longitude: -74.001244}},
]
return locations[Math.floor(Math.random()*locations.length)];
}
var getRandomLevel = function() {
var levels = ['Beginner (1-2)', 'Novice (3-4)', 'Intermediate(5-6)', 'Advanced(7-8)', 'Pro(9-10)'];
return levels[Math.floor(Math.random()*levels.length)];
}
var getRandomSport = function() {
var sports = ["Basketball", "Climbing", "Soccer", "Baseball", "Football", "Lifting", "Skiing", "Mountain Biking", "Surfing", "Cycling", 'Tennis']
return sports[Math.floor(Math.random()*sports.length)];
}
var genRandomEvents = function(num) {
var events = [];
for(i=0;i<num;i++) {
var newEvent = {
name: chance.sentence({words: 4}),
host: chance.first(),
sport: getRandomSport(),
date: chance.date({string: true, year: 2015, month: 11}),
time: moment(''+chance.hour()+':'+chance.minute()+'', 'h:mm A'),
location: getRandomLocation(),
level: getRandomLevel(),
description: chance.paragraph({sentences: 2})
}
events.push(newEvent);
}
return events;
}
var seedUsers = function () {
var users = [
{
email: '[email protected]',
password: 'jugal'
},
{
email: '[email protected]',
password: 'june'
},
{
email: '[email protected]',
password: 'mingjie'
},
{
email: '[email protected]',
password: 'bryce'
}
];
return User.createAsync(users);
};
var seedEvents = function () {
var events = genRandomEvents(10);
var staticEvents = [
{
name: 'The best soccer game ever',
host: 'bryce',
sport: 'Soccer',
date: moment('MMM Do YYYY').toString(),
time: moment('3:30', 'h:mm A').toString(),
location: {name: 'Central Park', coords: {latitude: 40.771606, longitude: -73.974819}},
tags: ['Soccer', 'Fun', 'Central Park', 'Easy'],
level: 'Beginner (1-2)',
minAttendees: 10,
maxAttendees: 16,
image: 'https://thomasblondal.files.wordpress.com/2014/10/norge-kampen.jpg',
description: 'A soccer game for beginners. Not intense at all. Basically we\'ll just be kicking around a ball and having fun.'
},
{
name: 'Chelsea Piers Basketball Tournament',
host: 'mingjie',
sport: 'Basketball',
date: moment('MMM Do YYYY').toString(),
time: moment('6:00', 'h:mm A').toString(),
location: {name: 'Chelsea Piers Sports Center', coords: {latitude: 40.746617, longitude: -74.010184}},
tags: ['Basketball', 'Tournament', 'Chelsea Piers'],
level: 'Intermediate (5-6)',
fee: 25.00,
minAttendees: 20,
maxAttendees: 30,
image: 'https://thomasblondal.files.wordpress.com/2014/10/norge-kampen.jpg',
description: 'Basketball tournament at Chelsea Piers! Be good. No scrubs allowed. Teams randomly assigned. Winning team gets $100 each!'
},
{
name: 'Intense Tennis Match',
host: 'jugal',
sport: 'Tennis',
date: moment('MMM Do YYYY').toString(),
time: moment('2:15', 'h:mm A').toString(),
location: {name: 'Hudson River Park Tennis Courts', coords: {latitude: 40.727127, longitude: -74.011334}},
tags: ['Tennis', 'Practice', 'Challenging', 'Match'],
level: 'Pro (9-10)',
minAttendees: 2,
maxAttendees: 2,
image: 'https://thomasblondal.files.wordpress.com/2014/10/norge-kampen.jpg',
description: 'I\'m really good at Tennis and I need a partner who is also really good at Tennis. Looking to play ASAP.'
}
]
staticEvents.forEach(function(staticEvent){
events.push(staticEvent);
});
return Event.createAsync(events);
}
connectToDb.then(function () {
User.findAsync({}).then(function (users) {
if (users.length === 0) {
return seedUsers();
} else {
console.log(chalk.magenta('Seems to already be user data, exiting!'));
process.kill(0);
}
})
.then(function(){
console.log(chalk.yellow('Seeding Events...'));
return seedEvents();
})
.then(function () {
console.log(chalk.green('Seed successful!'));
process.kill(0);
}).catch(function (err) {
console.error(err);
process.kill(1);
});
});