Created
August 22, 2013 11:32
-
-
Save radiodario/6306084 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var mongoose = require('mongoose'); | |
var app = module.exports = express.createServer(); | |
var QueryModel = require('../lib/models/query'); | |
// the base route is | |
// /db/queries | |
// Read all queries | |
app.get('/', function(req, res, next){ | |
return QueryModel.find(function (err, queries) { | |
if (!err) { | |
return res.send(queries); | |
} else { | |
return next(err); | |
} | |
}); | |
}); | |
// create a single query | |
app.post('/', function (req, res) { | |
var query, | |
b = req.body | |
//console.log("POST: ", req.body); | |
query = new QueryModel({ | |
index : b.index, | |
type : b.type, | |
filter: b.filter, | |
chart: b.chart, | |
dates: b.dates, | |
title: b.title, | |
data: b.data, | |
total: b.total | |
}); | |
query.save(function(err) { | |
if (!err) { | |
return console.log("created") | |
} else { | |
return console.log(err) | |
} | |
}); | |
return res.send(query); | |
}); | |
// read a query by id | |
app.get ('/:id', function(req, res) { | |
var id = mongoose.Types.ObjectId(req.params.id); | |
//console.log(id, req.params.id); | |
return QueryModel.findById( id, function(err, query) { | |
if (!err) { | |
// console.log(query) | |
return res.send(query) | |
} else { | |
return console.log (err); | |
} | |
}); | |
}); | |
// update a single quer by id | |
app.put ('/:id', function(req, res){ | |
var id = mongoose.Types.ObjectId(req.params.id); | |
// console.log(id, req.params.id); | |
return QueryModel.findById(id, function(err, query) { | |
query.index = req.body.index | |
query.type = req.body.type | |
query.filter = req.body.filter | |
query.chart = req.body.chart | |
query.dates = req.body.dates | |
query.data = req.body.data | |
query.total = req.body.total | |
query.title = req.body.title | |
return query.save(function(err) { | |
if (!err) { | |
return res.send(query) | |
} else { | |
return console.log (err); | |
} | |
}) | |
}); | |
}); | |
// delete a query | |
app.delete ( '/:id', function(req, res) { | |
var id = mongoose.Types.ObjectId(req.params.id); | |
// console.log(id, req.params.id); | |
return QueryModel.findById(id, function(err, query){ | |
return query.remove(function (err) { | |
if (!err) { | |
// console.log("removed"); | |
return res.send('ok!'); | |
} else { | |
// console.log(err); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment