-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
101 lines (84 loc) · 2.4 KB
/
actions.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
var express = require('express');
var utils = require('./utils.js');
var MongoClient = require('mongodb').MongoClient;
var router = express.Router();
// DB settings
var assert = require('assert');
var url = 'mongodb://rachel:wir123@ds035563.mongolab.com:35563/rachel';
// The main page
router.get('/', function(req, res){
// load data from DB here
res.render('index')
});
// Get bill by facebook id
router.get('/map/getUserBills', function(req, res){
var id = req.param('userFBId');
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
findBills(db, id, function(docs) {
db.close();
res.json(docs);
});
});
});
// Get bill by id
router.get('/map/getBillById', function(req, res){
var id = req.param('billID');
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
findBillByID(db, id, function(docs) {
db.close();
console.dir(docs);
res.json(docs);
});
});
});
var findBills = function(db, id, callback) {
// Get the documents collection
var collection = db.collection('bills');
// Find some documents
collection.find({places: {$elemMatch: {fbID: id}}}).toArray(function(err, docs) {
if (err) throw err;
callback(docs);
});
};
var insertBill = function(db, bID, fID, username, lati, long, callback) {
var collection = db.collection('bills');
collection.update({billID: bID}, {$push: { places: {name: username, fbID: fID, lng: long, lat: lati}}},
{upsert: true }, function(err, results){
if (err) throw err;
console.log(results);
callback(results);
});
};
var findBillByID = function(db, id, callback) {
// Get the documents collection
var collection = db.collection('bills');
// Find some documents
collection.find({billID: id}).toArray(function(err, docs) {
if (err) throw err;
callback(docs);
});
};
router.post('/map/checkIn', function(req, res){
var billID = req.body.billID;
var lat = req.body.lat;
var lng = req.body.lng;
var fbID = req.body.fbID;
var username = req.body.name;
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertBill(db, billID, fbID, username, lat, lng, function(isSuccess) {
if (!isSuccess) console.log('There was a problem checking in.');
findBillByID(db, billID, function(docs) {
db.close();
res.json(docs);
});
});
});
});
// Use as module
module.exports = router;
router.get('/web/main.html', function(req, res){
res.redirect('http://localhost:3000/')
});