|
|
|
|
|
|
|
|
* -> Array of message schema object ids |
|
|
* -> Array of message schema object ids |
|
|
* Get ALL known message ids |
|
|
* Get ALL known message ids |
|
|
*/ |
|
|
*/ |
|
|
get: function(req, res) { |
|
|
|
|
|
model.Messages.find({},{_id: true}).exec() |
|
|
|
|
|
|
|
|
get: function (req, res) { |
|
|
|
|
|
model.Messages.find({}, { |
|
|
|
|
|
_id: true |
|
|
|
|
|
}).exec() |
|
|
.then(results => { |
|
|
.then(results => { |
|
|
//selects id from message: |
|
|
|
|
|
var parsed = []; |
|
|
|
|
|
for (var i in results) { |
|
|
|
|
|
parsed.push (results[i]._id); |
|
|
|
|
|
} |
|
|
|
|
|
res.send(parsed); |
|
|
|
|
|
} ) |
|
|
|
|
|
.catch(err => { |
|
|
|
|
|
console.log (err); |
|
|
|
|
|
res.status(500).json(err); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
//selects id from message: |
|
|
|
|
|
var parsed = []; |
|
|
|
|
|
for (var i in results) { |
|
|
|
|
|
parsed.push(results[i]._id); |
|
|
|
|
|
} |
|
|
|
|
|
res.send(parsed); |
|
|
|
|
|
}) |
|
|
|
|
|
.catch(err => { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
res.status(500).json(err); |
|
|
|
|
|
}); |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
"msg/id": { |
|
|
"msg/id": { |
|
|
|
|
|
|
|
|
* -> Message schema |
|
|
* -> Message schema |
|
|
* Get a particular message |
|
|
* Get a particular message |
|
|
*/ |
|
|
*/ |
|
|
get: function(req, res) { |
|
|
|
|
|
model.Messages.findById(req.params.id) .exec(function(err, result) { |
|
|
|
|
|
|
|
|
get: function (req, res) { |
|
|
|
|
|
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(result)); |
|
|
//console.log(JSON.stringify(result)); |
|
|
|
|
|
|
|
|
* -> Message schema |
|
|
* -> Message schema |
|
|
* Get a particular message |
|
|
* Get a particular message |
|
|
*/ |
|
|
*/ |
|
|
get: function(req, res) { |
|
|
|
|
|
model.Messages.find( |
|
|
|
|
|
{$text: {$search: req.params.phrase}}) |
|
|
|
|
|
.sort({tag:-1}) |
|
|
|
|
|
.exec().then(results => { |
|
|
|
|
|
//selects id from message: |
|
|
|
|
|
var parsed = []; |
|
|
|
|
|
for (var i in results) { |
|
|
|
|
|
parsed.push (results[i]._id); |
|
|
|
|
|
} |
|
|
|
|
|
res.send(parsed); |
|
|
|
|
|
} ) |
|
|
|
|
|
.catch(err => { |
|
|
|
|
|
console.log (err); |
|
|
|
|
|
res.status(500).json(err); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
get: function (req, res) { |
|
|
|
|
|
model.Messages.find({ |
|
|
|
|
|
$text: { |
|
|
|
|
|
$search: req.params.phrase |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
.sort({ |
|
|
|
|
|
tag: -1 |
|
|
|
|
|
}) |
|
|
|
|
|
.exec().then(results => { |
|
|
|
|
|
//selects id from message: |
|
|
|
|
|
var parsed = []; |
|
|
|
|
|
for (var i in results) { |
|
|
|
|
|
parsed.push(results[i]._id); |
|
|
|
|
|
} |
|
|
|
|
|
res.send(parsed); |
|
|
|
|
|
}) |
|
|
|
|
|
.catch(err => { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
res.status(500).json(err); |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
"usr/bookmark/ids": { |
|
|
|
|
|
/* GET /api/usr/bookmark/ids [no args] |
|
|
|
|
|
* -> Array of message schema object ids |
|
|
|
|
|
* Get bookmark message ids |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
get: function (req, res) { |
|
|
|
|
|
|
|
|
|
|
|
model.Users.find({ |
|
|
|
|
|
|
|
|
|
|
|
}, |
|
|
|
|
|
function (err, result) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
return res.status(401).json(err.message); |
|
|
|
|
|
} else { |
|
|
|
|
|
var parsed = []; |
|
|
|
|
|
for (var i in result) { |
|
|
|
|
|
parsed.push(result[i].bookmarks); |
|
|
|
|
|
} |
|
|
|
|
|
console.log("bookmarks:" + JSON.stringify(parsed)); |
|
|
|
|
|
res.send(parsed); |
|
|
|
|
|
} |
|
|
|
|
|
if (result == null) { |
|
|
|
|
|
return res.status(500).json("Can save bookmark.") |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
"msg": { |
|
|
"msg": { |
|
|
|
|
|
|
|
|
* -> Message schema |
|
|
* -> Message schema |
|
|
* Create a new message |
|
|
* Create a new message |
|
|
*/ |
|
|
*/ |
|
|
post: function(req, res) { |
|
|
|
|
|
|
|
|
post: function (req, res) { |
|
|
/* |
|
|
/* |
|
|
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" }); |
|
|
|
|
|
|
|
|
message: req.body.message, |
|
|
message: req.body.message, |
|
|
user: req.body.user, |
|
|
user: req.body.user, |
|
|
tag: 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); |
|
|
} else { |
|
|
} else { |
|
|
res.json({message: "Message created!!"}); |
|
|
|
|
|
|
|
|
res.json({ |
|
|
|
|
|
message: "Message created!!" |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
if (result == null) { |
|
|
if (result == null) { |
|
|
return res.status(500).json("Can not create message.") |
|
|
return res.status(500).json("Can not create message.") |
|
|
|
|
|
|
|
|
* -> Array of tag schema object ids |
|
|
* -> Array of tag schema object ids |
|
|
* Get ALL known tag ids |
|
|
* Get ALL known tag ids |
|
|
*/ |
|
|
*/ |
|
|
get: function(req, res) { |
|
|
|
|
|
model.Tags.find({},{_id: true}).exec() |
|
|
|
|
|
|
|
|
get: function (req, res) { |
|
|
|
|
|
model.Tags.find({}, { |
|
|
|
|
|
_id: true |
|
|
|
|
|
}).exec() |
|
|
.then(results => { |
|
|
.then(results => { |
|
|
//selects id from tag: |
|
|
|
|
|
var parsed = []; |
|
|
|
|
|
for (var i in results) { |
|
|
|
|
|
parsed.push (results[i]._id); |
|
|
|
|
|
} |
|
|
|
|
|
res.send(parsed); |
|
|
|
|
|
} ) |
|
|
|
|
|
.catch(err => { |
|
|
|
|
|
console.log (err); |
|
|
|
|
|
res.status(500).json(err); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
//selects id from tag: |
|
|
|
|
|
var parsed = []; |
|
|
|
|
|
for (var i in results) { |
|
|
|
|
|
parsed.push(results[i]._id); |
|
|
|
|
|
} |
|
|
|
|
|
res.send(parsed); |
|
|
|
|
|
}) |
|
|
|
|
|
.catch(err => { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
res.status(500).json(err); |
|
|
|
|
|
}); |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
"tag/id": { |
|
|
"tag/id": { |
|
|
|
|
|
|
|
|
* -> Tag schema |
|
|
* -> Tag schema |
|
|
* Get a particular tag |
|
|
* Get a particular tag |
|
|
*/ |
|
|
*/ |
|
|
get: function(req, res) { |
|
|
|
|
|
|
|
|
get: function (req, res) { |
|
|
model.Tags.findById(req.params.id) |
|
|
model.Tags.findById(req.params.id) |
|
|
.exec(function(err, result) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.log (err); |
|
|
|
|
|
res.status(404).json(err); |
|
|
|
|
|
} else { |
|
|
|
|
|
//console.log(JSON.stringify(result)); |
|
|
|
|
|
res.json(result); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
.exec(function (err, result) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
res.status(404).json(err); |
|
|
|
|
|
} else { |
|
|
|
|
|
//console.log(req.params.id); |
|
|
|
|
|
res.json(result); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
"usr/id": { |
|
|
"usr/id": { |
|
|
|
|
|
|
|
|
* -> User Schema |
|
|
* -> User Schema |
|
|
* Get a particular user |
|
|
* Get a particular user |
|
|
*/ |
|
|
*/ |
|
|
get: function(req, res) { |
|
|
|
|
|
model.Users.findById(req.params.id) |
|
|
|
|
|
.exec(function(err, result) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
res.status(404).json(err); |
|
|
|
|
|
} else { |
|
|
|
|
|
//console.log(JSON.stringify(result)); |
|
|
|
|
|
res.json(result); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
get: function (req, res) { |
|
|
|
|
|
model.Users.find({ |
|
|
|
|
|
_id: req.params.id |
|
|
|
|
|
}) |
|
|
|
|
|
.exec(function (err, result) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
res.status(404).json(err); |
|
|
|
|
|
} else { |
|
|
|
|
|
console.log(result); |
|
|
|
|
|
res.json(result); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
"usr": { |
|
|
"usr": { |
|
|
|
|
|
|
|
|
* -> User schema |
|
|
* -> User schema |
|
|
* Create a new user |
|
|
* Create a new user |
|
|
*/ |
|
|
*/ |
|
|
post: function(req, res) { |
|
|
|
|
|
model.User.create({ |
|
|
|
|
|
|
|
|
post: function (req, res) { |
|
|
|
|
|
model.Users.create({ |
|
|
_id: req.body.id, |
|
|
_id: req.body.id, |
|
|
name: req.body.name, |
|
|
name: req.body.name, |
|
|
type: req.body.type, |
|
|
type: req.body.type, |
|
|
|
|
|
|
|
|
pwd: req.body.pwd, |
|
|
pwd: req.body.pwd, |
|
|
abos: req.body.abos, |
|
|
abos: req.body.abos, |
|
|
bookmarks: req.body.bookmarks, |
|
|
bookmarks: req.body.bookmarks, |
|
|
}, function(err, result) { |
|
|
|
|
|
|
|
|
}, function (err, result) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
console.log (err); |
|
|
|
|
|
|
|
|
console.log(err); |
|
|
return res.status(401).json(err.message); |
|
|
return res.status(401).json(err.message); |
|
|
} else { |
|
|
} else { |
|
|
res.json({message: "User created!!"}); |
|
|
|
|
|
|
|
|
res.json({ |
|
|
|
|
|
message: "User created!!" |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
if (result == null) { |
|
|
if (result == null) { |
|
|
return res.status(500).json("Can not create user.") |
|
|
return res.status(500).json("Can not create user.") |
|
|
|
|
|
|
|
|
* -> User schema |
|
|
* -> User schema |
|
|
* Change a user |
|
|
* Change a user |
|
|
*/ |
|
|
*/ |
|
|
put: function(req, res) { |
|
|
|
|
|
model.Users.findById(reg.body._id) |
|
|
|
|
|
.exec(function(err, entry) { |
|
|
|
|
|
if (err) |
|
|
|
|
|
console.log (err); |
|
|
|
|
|
if (entry == null) |
|
|
|
|
|
return res.status(404).json(err.message); |
|
|
|
|
|
// TODO Catch edge cases |
|
|
|
|
|
entry.save(function(err, data) { |
|
|
|
|
|
|
|
|
put: function (req, res) { |
|
|
|
|
|
|
|
|
|
|
|
model.Users.find({ |
|
|
|
|
|
_id: req.body.userid |
|
|
|
|
|
}) |
|
|
|
|
|
.exec(function (err, entry) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
model.Users.update({ |
|
|
|
|
|
_id: req.body.userid |
|
|
|
|
|
}, { |
|
|
|
|
|
bookmarks: req.body.bookmark |
|
|
|
|
|
}, function (err, data) { |
|
|
|
|
|
|
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
} else { |
|
|
|
|
|
res.json({ |
|
|
|
|
|
id: "Bookmark saved" |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
if (data == null) { |
|
|
|
|
|
return res.status(404).json(err.message); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (entry == null) |
|
|
|
|
|
return res.status(404).json(err.message); |
|
|
|
|
|
// TODO Catch edge cases |
|
|
|
|
|
|
|
|
// TODO Save/Update user |
|
|
// TODO Save/Update user |
|
|
/* |
|
|
/* |
|
|
_id: req.body.id, |
|
|
_id: req.body.id, |
|
|
|
|
|
|
|
|
abos: req.body.abos, |
|
|
abos: req.body.abos, |
|
|
bookmarks: req.body.bookmarks, |
|
|
bookmarks: req.body.bookmarks, |
|
|
*/ |
|
|
*/ |
|
|
res.json(data); |
|
|
|
|
|
|
|
|
// res.json(data); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
* Local db: common.config.dbLocalConn |
|
|
* Local db: common.config.dbLocalConn |
|
|
* TH db: common.config.dbConn |
|
|
* TH db: common.config.dbConn |
|
|
*/ |
|
|
*/ |
|
|
common.mongoose.connect (common.config.dbLocalConn, { |
|
|
|
|
|
|
|
|
common.mongoose.connect(common.config.dbLocalConn, { |
|
|
useNewUrlParser: true |
|
|
useNewUrlParser: true |
|
|
}).then(() => { |
|
|
}).then(() => { |
|
|
console.log("Database connected successfully."); |
|
|
console.log("Database connected successfully."); |
|
|
|
|
|
|
|
|
* - per entry: _comment: Comment for Admin UI - TODO: not working yet |
|
|
* - per entry: _comment: Comment for Admin UI - TODO: not working yet |
|
|
*/ |
|
|
*/ |
|
|
var messageSchema = common.mongoose.Schema({ |
|
|
var messageSchema = common.mongoose.Schema({ |
|
|
subject: { type: String, required: true, |
|
|
|
|
|
_comment: "" }, |
|
|
|
|
|
message: { type: String, required: true, |
|
|
|
|
|
_comment: "" }, |
|
|
|
|
|
user: { type: String, required: true, |
|
|
|
|
|
_comment: "" }, |
|
|
|
|
|
tag: { type: [String], |
|
|
|
|
|
_comment: "" }, |
|
|
|
|
|
|
|
|
subject: { |
|
|
|
|
|
type: String, |
|
|
|
|
|
required: true, |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, |
|
|
|
|
|
message: { |
|
|
|
|
|
type: String, |
|
|
|
|
|
required: true, |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, |
|
|
|
|
|
user: { |
|
|
|
|
|
type: String, |
|
|
|
|
|
required: true, |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, |
|
|
|
|
|
tag: { |
|
|
|
|
|
type: [String], |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, |
|
|
//createtime: { type: Date, default: Date.now }, |
|
|
//createtime: { type: Date, default: Date.now }, |
|
|
}); |
|
|
}); |
|
|
messageSchema.index({ "$**":'text' }); |
|
|
|
|
|
|
|
|
messageSchema.index({ |
|
|
|
|
|
"$**": 'text' |
|
|
|
|
|
}); |
|
|
model.Messages = common.mongoose.model('messages', messageSchema); |
|
|
model.Messages = common.mongoose.model('messages', messageSchema); |
|
|
model.Messages._list = [ "" ]; |
|
|
|
|
|
|
|
|
model.Messages._list = [""]; |
|
|
|
|
|
|
|
|
var tagSchema = common.mongoose.Schema({ |
|
|
var tagSchema = common.mongoose.Schema({ |
|
|
name: { type: String, required: true, |
|
|
|
|
|
_comment: "" }, //unique |
|
|
|
|
|
|
|
|
name: { |
|
|
|
|
|
type: String, |
|
|
|
|
|
required: true, |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, //unique |
|
|
}); |
|
|
}); |
|
|
model.Tags = common.mongoose.model('tags', messageSchema); |
|
|
model.Tags = common.mongoose.model('tags', messageSchema); |
|
|
model.Tags._list = [ "" ]; |
|
|
|
|
|
|
|
|
model.Tags._list = [""]; |
|
|
|
|
|
|
|
|
var userSchema = common.mongoose.Schema({ |
|
|
var userSchema = common.mongoose.Schema({ |
|
|
_id: { type: String }, |
|
|
|
|
|
name: { type: String, required: true, |
|
|
|
|
|
_comment: "" }, |
|
|
|
|
|
pwd: { type: String, |
|
|
|
|
|
_comment: "" }, |
|
|
|
|
|
hash: { type: String }, |
|
|
|
|
|
salt: { type: String }, |
|
|
|
|
|
type: { type: String }, |
|
|
|
|
|
roles: { type: [String], required: true, |
|
|
|
|
|
_comment: "" }, |
|
|
|
|
|
abos: { type: [String], |
|
|
|
|
|
_comment: "" }, |
|
|
|
|
|
|
|
|
/*_id: { |
|
|
|
|
|
type: String |
|
|
|
|
|
},*/ |
|
|
|
|
|
name: { |
|
|
|
|
|
type: String, |
|
|
|
|
|
required: true, |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, |
|
|
|
|
|
pwd: { |
|
|
|
|
|
type: String, |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, |
|
|
|
|
|
hash: { |
|
|
|
|
|
type: String |
|
|
|
|
|
}, |
|
|
|
|
|
salt: { |
|
|
|
|
|
type: String |
|
|
|
|
|
}, |
|
|
|
|
|
type: { |
|
|
|
|
|
type: String |
|
|
|
|
|
}, |
|
|
|
|
|
roles: { |
|
|
|
|
|
type: [String], |
|
|
|
|
|
required: true, |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, |
|
|
|
|
|
abos: { |
|
|
|
|
|
type: [String], |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, |
|
|
//deactivated: { type: Boolean }, |
|
|
//deactivated: { type: Boolean }, |
|
|
//host: { type: Boolean }, |
|
|
//host: { type: Boolean }, |
|
|
bookmarks: { type: [String], |
|
|
|
|
|
_comment: "" }, |
|
|
|
|
|
|
|
|
bookmarks: { |
|
|
|
|
|
type: [String], |
|
|
|
|
|
_comment: "" |
|
|
|
|
|
}, |
|
|
}); |
|
|
}); |
|
|
model.Users = common.mongoose.model('users', userSchema); |
|
|
model.Users = common.mongoose.model('users', userSchema); |
|
|
model.Users._list = [ "" ]; |
|
|
|
|
|
|
|
|
model.Users._list = [""]; |
|
|
}, |
|
|
}, |
|
|
models: model, |
|
|
models: model, |
|
|
}; |
|
|
}; |