|
|
|
|
|
|
|
|
* Get ALL known message ids |
|
|
* Get ALL known message ids |
|
|
*/ |
|
|
*/ |
|
|
get: function(req, res) { |
|
|
get: function(req, res) { |
|
|
model.Messages.find({}, {_id: true}).exec() |
|
|
|
|
|
|
|
|
model.Messages.find({},{_id: true}).exec() |
|
|
.then(results => { |
|
|
.then(results => { |
|
|
//selects id from message: |
|
|
//selects id from message: |
|
|
var parsed = []; |
|
|
var parsed = []; |
|
|
for (var i in results) { |
|
|
for (var i in results) { |
|
|
parsed.push (results[i]._id); |
|
|
parsed.push (results[i]._id); |
|
|
} |
|
|
} |
|
|
//var parsed = results.map (x => x._id); |
|
|
|
|
|
res.send(parsed); |
|
|
res.send(parsed); |
|
|
} ) |
|
|
} ) |
|
|
.catch(err => { |
|
|
.catch(err => { |
|
|
|
|
|
|
|
|
* Get a particular message |
|
|
* Get a particular message |
|
|
*/ |
|
|
*/ |
|
|
get: function(req, res) { |
|
|
get: function(req, res) { |
|
|
model.Messages.findById(req.params.id) .exec(function(err, results) { |
|
|
|
|
|
|
|
|
model.Messages.findById(req.params.id) .exec(function(err, result) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
console.log (err); |
|
|
console.log (err); |
|
|
res.status(404).json(err); |
|
|
res.status(404).json(err); |
|
|
} else { |
|
|
} else { |
|
|
//console.log(JSON.stringify(results)); |
|
|
|
|
|
res.json(results); |
|
|
|
|
|
|
|
|
//console.log(JSON.stringify(result)); |
|
|
|
|
|
res.json(result); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
if ( !(req.body.tags instanceof Array) ) { |
|
|
if ( !(req.body.tags instanceof Array) ) { |
|
|
return res.status(400).json({ error: "bad request" }); |
|
|
return res.status(400).json({ error: "bad request" }); |
|
|
}*/ |
|
|
}*/ |
|
|
console.log("Subject: "+JSON.stringify(req.body)); |
|
|
|
|
|
|
|
|
//console.log("Subject: "+JSON.stringify(req.body)); |
|
|
|
|
|
|
|
|
model.Messages.create({ |
|
|
model.Messages.create({ |
|
|
subject: req.body.subject, |
|
|
subject: req.body.subject, |
|
|
message: req.body.message, |
|
|
message: req.body.message, |
|
|
user: req.body.user, |
|
|
user: req.body.user, |
|
|
tags: req.body.tag |
|
|
|
|
|
|
|
|
tag: req.body.tag |
|
|
}, function(err, result) { |
|
|
}, function(err, result) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
return res.status(401).json(err.message); |
|
|
return res.status(401).json(err.message); |
|
|
|
|
|
|
|
|
_comment: "" }, |
|
|
_comment: "" }, |
|
|
user: { type: String, required: true, |
|
|
user: { type: String, required: true, |
|
|
_comment: "" }, |
|
|
_comment: "" }, |
|
|
tags: { type: [String], |
|
|
|
|
|
|
|
|
tag: { type: [String], |
|
|
_comment: "" }, |
|
|
_comment: "" }, |
|
|
//createtime: { type: Date, default: Date.now }, |
|
|
//createtime: { type: Date, default: Date.now }, |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
app.get ('/api/ids', function (req, res) { |
|
|
|
|
|
Message.find({},{id: true}) .exec () .then(results => { |
|
|
|
|
|
//selects id from message: |
|
|
|
|
|
var parsed = []; |
|
|
|
|
|
for (var i in results) { |
|
|
|
|
|
parsed.push (results[i].id); |
|
|
|
|
|
} |
|
|
|
|
|
//var parsed = results.map (x => x._id); |
|
|
|
|
|
res.send(parsed); |
|
|
|
|
|
} ) |
|
|
|
|
|
.catch(err => { |
|
|
|
|
|
console.log (err); |
|
|
|
|
|
res .status(500) .json (err); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
app.get ("/api/msg/:id", function (req, res) { |
|
|
|
|
|
Message.findOne ({_id: req.params.id}) .exec (function (err, results){ |
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.log (err); |
|
|
|
|
|
res .status(404) .json (err); |
|
|
|
|
|
} else { |
|
|
|
|
|
console.log(JSON.stringify(results)); |
|
|
|
|
|
res.json(results); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
/*app.get ("/api/msg/search/:phrase", function (req, res) { |
|
|
|
|
|
|
|
|
app.get ("/api/msg/search/:phrase", function (req, res) { |
|
|
Message.find ({$text: {$search: req.params.phrase}) .then (function (err, results){ |
|
|
Message.find ({$text: {$search: req.params.phrase}) .then (function (err, results){ |
|
|
if (err) { |
|
|
if (err) { |
|
|
console.log (err); |
|
|
console.log (err); |
|
|
|
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
*/ |
|
|
*/ |
|
|
/* |
|
|
|
|
|
app.post("/api/createMsg", function(req, res){ |
|
|
|
|
|
console.log("Subject: "+JSON.stringify(req.body)); |
|
|
|
|
|
var message = new Message( {subject: req.body.sub, message: req.body.mess, user: req.body.use, tag: req.body.ta } ); |
|
|
|
|
|
|
|
|
|
|
|
message.save(function(err,result){ |
|
|
|
|
|
if(err){ |
|
|
|
|
|
return res .status(401) .send(err.message); |
|
|
|
|
|
}else{ |
|
|
|
|
|
res.json({message: "Message created!!"}); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
*/ |
|
|
|
|
|
module.exports = dbs; |
|
|
module.exports = dbs; |