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
|
/* jshint node: true */
var _ = require('lodash'),
crypto = require('crypto'),
util = require('../util'),
upload = require('../upload'),
config = require('../../../config.json'),
Blueprint = require('../schemas/Blueprint');
var blueprint = {
user: function(req, res){
var offset = Number(req.query.offset) || 0
var limit = Math.min( Number(req.query.limit), 50 ) || 20
var query = { user_id: req.user._id }
if (req.query.tag) {
query.tag = req.query.tag
}
Blueprint.find(query)
.sort({'created_at': -1})
.skip(offset)
.limit(limit)
.exec(function(err, media){
res.json(media || [])
})
},
create: function(req, res){
var data = util.cleanQuery(req.body)
data.user_id = req.user._id
data.created_at = new Date ()
if (data.tag) {
data.tag = util.sanitize(data.tag)
}
new Blueprint(data).save(function(err, rec){
if (err || ! rec) { return res.json({ error: err }) }
return res.json(rec)
})
},
upload: function(req, res){
var data = util.cleanQuery(req.body)
data.user_id = req.user._id
data.created_at = new Date ()
data.type = "image"
upload.put("media", req.files.image, {
username: req.user.username,
unacceptable: function(err){
res.json({ error: { errors: { media: { message: "Problem saving image: " + err } } } })
},
success: function(url){
data.url = url
done()
}
})
function done () {
new Blueprint(data).save(function(err, rec) {
if (err || ! rec) { return res.json({ error: err }) }
res.json(rec)
})
}
},
scale: function(req, res){
var _id = req.body._id
var data = util.cleanQuery(req.body)
if (! _id) { return res.json({ error: 404 }) }
Blueprint.findOne({ _id: _id }, function(err, doc){
if (! doc) { return res.json({ error: 404 }) }
if (String(doc.user_id) !== String(req.user._id)) { return res.json({ error: 404 }) }
doc.scale = data.scale
doc.units = data.units
doc.line = data.line
doc.save(function(err, rec){
if (err || ! rec) { return res.json({ error: err }) }
res.json(rec)
})
})
},
update: function(req, res){
var _id = req.body._id
var data = util.cleanQuery(req.body)
if (! _id) { return res.json({ error: 404 }) }
Blueprint.findOne({ _id: _id }, function(err, doc){
if (! doc) { return res.json({ error: 404 }) }
if (String(doc.user_id) !== String(req.user._id)) { return res.json({ error: 404 }) }
doc.name = util.sanitize(data.name)
doc.slug = util.slugify(data.name)
doc.units = util.sanitize(data.units)
doc.viewHeight = util.sanitizeNumber(data.viewHeight)
doc.wallHeight = util.sanitizeNumber(data.wallHeight)
doc.shapes = JSON.parse(data.shapes)
doc.startPosition = JSON.parse(data.startPosition)
doc.save(function(err, rec){
if (err || ! rec) { return res.json({ error: err }) }
res.json(rec)
})
})
},
destroy: function(req, res){
var _id = util.sanitize(req.body._id)
if (! _id || ! _id.length) {
res.json({ error: 404 })
return
}
Blueprint.findOne({ _id: _id }, function(err, doc){
if (! doc) { return res.json({ error: 404 }) }
if (String(doc.user_id) !== String(req.user._id)) {
return res.json({ error: "access denied" })
}
Blueprint.remove({ _id: _id }, function(err){
res.json({ status: "OK" })
})
})
}
}
module.exports = blueprint
|