-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (90 loc) · 2.5 KB
/
app.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
var mongoUrl = 'mongodb://localhost:27017/board';
var express = require('express')
var path = require('path')
var app = express()
var bodyParser = require('body-parser')
var MongoClient = require('mongodb').MongoClient
var assert = require('assert')
app.use(bodyParser.json());
app.use('/node_modules', express.static('node_modules'))
app.use('/js', express.static('js'))
app.use('/img', express.static('img'))
app.get('/', function (req, res) {
res.sendFile('index.html', { root : __dirname})
})
app.get('/item', function (req, res) {
query(res, {
x: Number(req.query.x),
y: Number(req.query.y),
width: Number(req.query.width),
height: Number(req.query.height)
})
})
function query(res, bounds) {
mongoConnect(function (db) {
var coll = db.collection('coordinates')
coll.find(
{
pos: { $geoWithin: { $box: [
[ bounds.x, bounds.y ],
[ bounds.x + bounds.width, bounds.y + bounds.width ]
] } }
}, { obj: 1, _id: 0 }
).map(function (o) { return o.obj })
.toArray(function (err, itemIds) {
queryDetails(res, db, itemIds)
})
})
}
function queryDetails(res, db, itemIds) {
var coll = db.collection('items')
coll.find({ _id: { $in: itemIds } }, { _id: 0 })
.toArray(function (err, data) {
db.close()
res.send(data)
})
}
app.post('/item', function(req, res) {
// { x: 439.23049, y: 286.64455, width: 68.71089, height: 68.71089 }
var body = req.body
var bounds = body.bounds
var item = body.item
var posMin = [ bounds.x, bounds.y ]
var posMax = [ bounds.x + bounds.width, bounds.y + bounds.height ]
console.log(body)
store({
posMin: posMin,
posMax: posMax,
item: body
})
res.send({'ok': true})
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
function store(item) {
mongoConnect(function(db) {
var coll = db.collection('items')
coll.insertOne(item.item, function(err, result) {
if (err !== null) return
var objId = result.insertedId
storeCoordinates(db, objId, item)
})
})
}
function storeCoordinates(db, objId, item) {
var coll = db.collection('coordinates')
coll.insertMany([
{ pos: item.posMin, obj: objId },
{ pos: item.posMax, obj: objId, isMax: true }
], function() { db.close(); })
}
function mongoConnect(callback) {
MongoClient.connect(mongoUrl, function(err, db) {
assert.equal(null, err)
//console.log("Connected correctly to server")
if (err === null) {
callback(db)
}
})
}