-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
176 lines (135 loc) · 4.36 KB
/
router.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
176
var Ctrl = require('./controllers/controller.js'),
CardModel = require('./models/cardmodel.js'),
SearchModel = require('./models/searchmodel.js'),
Jade = require('jade'),
MongoClient = require('mongodb').MongoClient,
assert = require('assert'),
url = 'mongodb://localhost:27017/mtg_cards',
// Make it easier to customize your database names by variablizing them.
mtgDB = 'mtgcards',
myCollection = 'mtg_collection'
module.exports = function (app) {
app.get('/test', function(req, res){
Ctrl.test(req, res);
});
app.get('/search', function(req, res){
var searchJadevars = {
pagetitle: "Awesome Aesop",
collectionSearched: mtgDB,
backBtn: '/search'
}
return res.render('views/searchview.jade', searchJadevars);
});
app.get('/search-collection', function(req, res){
var searchJadevars = {
pagetitle: "Awesome Aesop",
collectionSearched: myCollection,
backBtn: '/search-collection'
}
return res.render('views/searchview.jade', searchJadevars);
})
app.post('/card-request', function(req, res){
var searchTerm = req.body.searchterm,
collectionSearched = req.body.collection;
MongoClient.connect(url, function(err, db) {
SearchModel.findDocuments(db, {name: searchTerm}, collectionSearched, function(err, results){
return res.status(200).json(results);
});
});
});
app.post('/card-view', function(req, res){
console.log(req.body);
var searchTerm = req.body.searchterm,
collectionSearched = req.body.collection;
MongoClient.connect(url, function(err, db) {
SearchModel.findDocuments(db, {name: {'$regex': searchTerm}}, collectionSearched, function(err, results){
console.log('---------');
var dumpCheck = false,
data = "words", // JSON.stringify(results);
jadeVars = {};
jadeVars.cardResults = [];
for (var i = 0; i < results.length; i++){
jadeVars.cardResults[i] = {
uuid: results[i]._id,
cardName: results[i].name,
sets: results[i].printings || results[i].ownedPrinting
}
}
// Set Back Button link
if (collectionSearched == myCollection){
jadeVars.backBtn = '/search-collection';
jadeVars.searchTarget = '/collection-update';
}else if(collectionSearched == mtgDB){
jadeVars.backBtn = '/search';
jadeVars.searchTarget = '/collection-save';
}else{
jadeVars.backBtn = '/';
}
jadeVars.searchterm = searchTerm;
jadeVars.dumpCheck = dumpCheck;
jadeVars.dumpData = data;
return res.render('views/searchreturn.jade', jadeVars);
});
});
});
app.post('/collection-save', function(req, res){
var saveRequest = req.body;
console.log(saveRequest);
MongoClient.connect(url, function(err, db){
SearchModel.findDocumentsById(db, saveRequest.uuid, mtgDB, function(err, results){
if (!err){
var dataToSave = results;
dataToSave[0].ownedPrinting = saveRequest.set;
dataToSave[0].numberOwned = saveRequest.qty;
CardModel.saveData(db, dataToSave, myCollection, function(err, results){
var jadeVars = {
pagetitle: "Welcome to Card Artificer",
heading: "Card Saved!",
}
return res.render('views/welcome.jade', jadeVars);
})
}
});
})
});
app.post('/collection-update', function(req, res){
var cardToUpdate = req.body.uuid,
qtyChange = req.body.qty;
console.log(cardToUpdate);
MongoClient.connect(url, function(err, db){
if (!err){
CardModel.updateCollection(db, cardToUpdate, qtyChange, myCollection, function(err, results){
var jadeVars = {
pagetitle: "Welcome to Card Artificer",
heading: "Card Updated!",
}
return res.render('views/welcome.jade', jadeVars);
})
}
})
});
app.get('/view-collection', function(req, res){
console.log('Time to see all the cards in my collection');
MongoClient.connect(url, function(err, db){
if(!err){
CardModel.viewAllCards(db, myCollection, function(err, results){
if (!err){
var cardsInCollection = results,
jadeVars = {
cards: cardsInCollection,
pagetitle: 'Cards in your collection'
}
return res.render('views/collectionview.jade', jadeVars);
}
});
}
})
})
app.post('/dump', function(req, res){
var thingy = JSON.stringify(req.body);
return res.render('views/dump.jade', {dump: thingy});
})
app.get('/*', function(req, res) {
return res.render('views/welcome.jade')
});
};