|
|
@@ -17,14 +17,13 @@ const dbs = { |
|
|
|
* Get ALL known message ids |
|
|
|
*/ |
|
|
|
get: function(req, res) { |
|
|
|
model.Messages.find({}, {_id: true}).exec() |
|
|
|
model.Messages.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 => { |
|
|
@@ -40,13 +39,13 @@ const dbs = { |
|
|
|
* Get a particular message |
|
|
|
*/ |
|
|
|
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) { |
|
|
|
console.log (err); |
|
|
|
res.status(404).json(err); |
|
|
|
} else { |
|
|
|
//console.log(JSON.stringify(results)); |
|
|
|
res.json(results); |
|
|
|
//console.log(JSON.stringify(result)); |
|
|
|
res.json(result); |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
@@ -63,12 +62,13 @@ const dbs = { |
|
|
|
if ( !(req.body.tags instanceof Array) ) { |
|
|
|
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({ |
|
|
|
subject: req.body.subject, |
|
|
|
message: req.body.message, |
|
|
|
user: req.body.user, |
|
|
|
tags: req.body.tag |
|
|
|
tag: req.body.tag |
|
|
|
}, function(err, result) { |
|
|
|
if (err) { |
|
|
|
return res.status(401).json(err.message); |
|
|
@@ -122,7 +122,7 @@ const dbs = { |
|
|
|
_comment: "" }, |
|
|
|
user: { type: String, required: true, |
|
|
|
_comment: "" }, |
|
|
|
tags: { type: [String], |
|
|
|
tag: { type: [String], |
|
|
|
_comment: "" }, |
|
|
|
//createtime: { type: Date, default: Date.now }, |
|
|
|
}); |
|
|
@@ -163,37 +163,8 @@ const dbs = { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
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){ |
|
|
|
if (err) { |
|
|
|
console.log (err); |
|
|
@@ -205,18 +176,4 @@ app.get ("/api/msg/:id", function (req, res) { |
|
|
|
}); |
|
|
|
}); |
|
|
|
*/ |
|
|
|
/* |
|
|
|
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; |