-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilldatabase.js
147 lines (134 loc) · 3.9 KB
/
filldatabase.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
var mongoose = require('mongoose');
var Question = require('./models/questions');
var Categorie = require('./models/categories');
var async = require('async');
var colors = require('colors');
var http =require('http');
async.waterfall([
/*
* Setup the database connection
*/
function(callback) {
var db = mongoose.connection;
db
.on('error', function(err) {
process.stdout.write(['[', '✗'.red, ']', 'Database connection'].join(' ') + '\n');
callback(err);
})
.once('open', function() {
process.stdout.write(['[', '✓'.green, ']', 'Database connection'].join(' ') + '\n');
callback();
});
mongoose.connect('mongodb://localhost/quiz');
},
/*
* Empty the categories
*/
function(callback) {
Categorie.remove({}, function(err,removed) {
if(err){
console.log('somethign went wrong whem emptying database');
callback(err);
} else {
callback();
console.log('emptied' + removed + 'categories');
}
});
/*
* import the categories
*/
}, function(callback) {
// Create the categories database!
Categorie.create(
{ _id: 309, name: "Random questions" },
{ _id: 136, name: "stupid answers" },
{ _id: 42, name: "sports" },
{ _id: 21, name: "animals" },
{ _id: 25, name: "science" },
{ _id: 103, name: "transportation" },
{ _id: 7, name: "u.s. cities" },
{ _id: 253, name: "food & drink" }, function (err) {
if (err) {
console.log('new categorie not saved');
callback(err);
} else {
console.log('saved!');
callback();
}
});
// get questions!
}, function(callback) {
var allQuestions = [];
var categoriesDone = 0;
Categorie.find({}, function(err, categories){
for(var i=0; i<categories.length;i++){
http.get('http://www.jservice.io/api/category?id='+categories[i]._id, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var questions = JSON.parse(body);
// console.log(questions);
allQuestions.push.apply(allQuestions, questions.clues);
console.log('push this many questions' + questions.clues.length);
console.log('total length:' + allQuestions.length);
categoriesDone++;
if(categoriesDone === categories.length) {
console.log('done!');
console.log(allQuestions.length);
callback(null, allQuestions, categories);
}
});
});
}
});
// save all the questions!
}, function(questions,categories, callback) {
console.log("Got the questions: ", questions.length);
var questionsSaved = 0;
callback(null, categories);
for(var i=0; i<questions.length;i++) {
Question.create({
question: questions[i].question,
answer:questions[i].answer,
category_id:questions[i].category_id
}, function(err) {
if(err) {
// callback(err);
} else {
// console.log('new question saved!');
// callback();
questionsSaved++;
if(questionsSaved === questions.length) {
console.log('saved all the questions!');
callback(null, categories);
}
}
})
}
// set our categorie name!
}, function(categories, callback) {
namesChanged = 0;
for(var i=0; i<categories.length;i++){
Question.update({category_id:categories[i]._id},
{categorie:categories[i].name},
{multi:true}, function(err, num){
console.log("updated: "+num);
namesChanged++;
if(namesChanged === categories.length){
callback();
}
});
}
}
], function(err, result) {
if (err) {
process.stdout.write(err.red);
} else {
process.stdout.write(['[', '✓'.green, ']', 'Database successfully imported'].join(' ') + '\n');
}
});
process.on('exit', function(code) {
process.stdout.write(['[', '✗'.red, ']', 'Application quit [', code, ']'].join(' ') + '\n')
});