diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-06-12 15:21:27 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-06-12 15:21:27 -0400 |
| commit | 036427dfe207249ca5d6260ae1f6e8006ff90dd9 (patch) | |
| tree | 1f47f12d1ba04f55571930edde72f0b2dde2ef22 /server/lib/api/projects.js | |
| parent | 5a215e1b00e4fd026a83e81baa7a45c28435f73c (diff) | |
split up api module
Diffstat (limited to 'server/lib/api/projects.js')
| -rw-r--r-- | server/lib/api/projects.js | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/server/lib/api/projects.js b/server/lib/api/projects.js new file mode 100644 index 0000000..99ed9fe --- /dev/null +++ b/server/lib/api/projects.js @@ -0,0 +1,106 @@ +/* jshint node: true */ + +var _ = require('lodash'), + util = require('../util'), + upload = require('../upload'), + config = require('../../../config.json'), + Project = require('../schemas/Project'); + +var projects = { + index: function(req, res){ + Project.find({}, function(err, docs){ + res.json(docs) + }) + }, + + show: function(req, res){ + Project.findOne({ slug: req.params.slug }, function(err, doc){ + if (doc) { + res.json(doc) + return + } + else { + var name = util.sanitize(req.params.slug) + if (name == "new") { + name = "" + } + res.json({ _id: "new", name: name, isNew: true }) + } + }) + }, + + create: function(req, res){ + var data = util.cleanQuery(req.body) + data.name = util.sanitize(data.name) + data.slug = util.slugify(data.name) + data.user_id = req.user._id + data.rooms = JSON.parse(data.rooms) + data.startPosition = JSON.parse(data.startPosition) + + upload.put("projects", req.files.thumbnail, { + unacceptable: function(err){ + res.json({ error: { errors: { thumbnail: { message: "Problem saving thumbnail: " + err } } } }) + }, + success: function(url){ + data.photo = url + done() + } + }) + + function done() { + new Project(data).save(function(err, doc){ + if (err || ! doc) { return res.json({ error: err }) } + res.json(doc) + }) + } + }, + + update: function(req, res){ + var _id = req.body._id + if (_id == "new") { + return docs.create(req, res) + } + + var data = util.cleanQuery(req.body) + data.name = util.sanitize(data.name) + data.slug = util.slugify(data.name) + data.user_id = req.user._id + + upload.put("projects", req.files.thumbnail, { + unacceptable: function(err){ + res.json({ error: { errors: { thumbnail: { message: "Problem saving thumbnail: " + err } } } }) + }, + success: function(url){ + data.photo = url + done() + } + }) + + function done() { + Project.findOne({ _id: _id }, function(err, doc){ + if (err || ! doc) { return res.json({ error: err }) } + _.extend(doc, data) + doc.rooms = JSON.parse(data.rooms) + doc.startPosition = JSON.parse(data.startPosition) + + doc.save(function(err, doc){ + if (err || ! doc) { return res.json({ error: err }) } + res.json(doc) + }) + }) + } + }, + + destroy: function(req, res){ + var _id = req.body._id + if (! id || ! id.length) { + res.json({ error: 404 }) + return + } + Project.remove({ _id: _id }, function(err){ + res.json({ status: "OK" }) + }) + }, +} + +module.exports = projects |
