Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbs.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Main database access functions
  3. */
  4. var common,
  5. model = {};
  6. const dbs = {
  7. /* Method API route
  8. * <- to server
  9. * -> to client
  10. * Description
  11. */
  12. routes: {
  13. "msg/ids": {
  14. /* GET /api/msg/ids [no args]
  15. * -> Array of message schema object ids
  16. * Get ALL known message ids
  17. */
  18. get: function(req, res) {
  19. model.Messages.find({},{_id: true}).exec()
  20. .then(results => {
  21. //selects id from message:
  22. var parsed = [];
  23. for (var i in results) {
  24. parsed.push (results[i]._id);
  25. }
  26. res.send(parsed);
  27. } )
  28. .catch(err => {
  29. console.log (err);
  30. res.status(500).json(err);
  31. });
  32. },
  33. },
  34. "msg/id": {
  35. params: ":id",
  36. /* GET /api/msg/id/[massage-id]
  37. * -> Message schema
  38. * Get a particular message
  39. */
  40. get: function(req, res) {
  41. model.Messages.findById(req.params.id) .exec(function(err, result) {
  42. if (err) {
  43. console.log (err);
  44. res.status(404).json(err);
  45. } else {
  46. //console.log(JSON.stringify(result));
  47. res.json(result);
  48. }
  49. });
  50. },
  51. },
  52. "msg/search": {
  53. params: ":phrase",
  54. /* GET /api/msg/id/[massage-id]
  55. * -> Message schema
  56. * Get a particular message
  57. */
  58. get: function(req, res) {
  59. model.Messages.find(
  60. {$text: {$search: req.params.phrase}})
  61. .sort({tag:1, user:1, message:1, subject:1})
  62. .exec().then(results => {
  63. //selects id from message:
  64. var parsed = [];
  65. for (var i in results) {
  66. parsed.push (results[i]._id);
  67. }
  68. res.send(parsed);
  69. } )
  70. .catch(err => {
  71. console.log (err);
  72. res.status(500).json(err);
  73. });
  74. },
  75. },
  76. "msg": {
  77. /* POST /api/msg
  78. * <- Message schema
  79. * -> Message schema
  80. * Create a new message
  81. */
  82. post: function(req, res) {
  83. /*
  84. if ( !(req.body.tags instanceof Array) ) {
  85. return res.status(400).json({ error: "bad request" });
  86. }*/
  87. //console.log("Subject: "+JSON.stringify(req.body));
  88. model.Messages.create({
  89. subject: req.body.subject,
  90. message: req.body.message,
  91. user: req.body.user,
  92. tag: req.body.tag
  93. }, function(err, result) {
  94. if (err) {
  95. return res.status(401).json(err.message);
  96. } else {
  97. res.json({message: "Message created!!"});
  98. }
  99. if (result == null) {
  100. return res.status(500).json("Can not create message.")
  101. }
  102. });
  103. },
  104. /* PUT /api/msg
  105. * <-
  106. * ->
  107. * Update a message
  108. */
  109. //put: function(req, res) {},
  110. },
  111. "tag/ids": {
  112. /* GET /api/tag/ids [no args]
  113. * -> Array of tag schema object ids
  114. * Get ALL known tag ids
  115. */
  116. get: function(req, res) {
  117. model.Tags.find({},{_id: true}).exec()
  118. .then(results => {
  119. //selects id from tag:
  120. var parsed = [];
  121. for (var i in results) {
  122. parsed.push (results[i]._id);
  123. }
  124. res.send(parsed);
  125. } )
  126. .catch(err => {
  127. console.log (err);
  128. res.status(500).json(err);
  129. });
  130. },
  131. },
  132. "tag": {
  133. params: ":id",
  134. /* GET /api/tag/[tag-id]
  135. * -> Tag schema
  136. * Get a particular tag
  137. */
  138. get: function(req, res) {
  139. model.Tags.findById(req.params.id) .exec(function(err, result) {
  140. if (err) {
  141. console.log (err);
  142. res.status(404).json(err);
  143. } else {
  144. //console.log(JSON.stringify(result));
  145. res.json(result);
  146. }
  147. });
  148. },
  149. },
  150. },
  151. /* Initialize requirements
  152. * - DB connection
  153. * - DB schemata
  154. */
  155. init: function (_common) {
  156. common = _common;
  157. /* DB Connection
  158. * Local db: common.config.dbLocalConn
  159. * TH db: common.config.dbConn
  160. */
  161. common.mongoose.connect (common.config.dbLocalConn, {
  162. useNewUrlParser: true
  163. }).then(() => {
  164. console.log("Database connected successfully.");
  165. }).catch(err => {
  166. console.log('Database connection error.');
  167. process.exit();
  168. });
  169. /* DB Schemata
  170. * Privat fields:
  171. * - per model: _list: Elements that are included in list fetch
  172. * - per entry: _comment: Comment for Admin UI - TODO: not working yet
  173. */
  174. var messageSchema = common.mongoose.Schema({
  175. subject: { type: String, required: true,
  176. _comment: "" },
  177. message: { type: String, required: true,
  178. _comment: "" },
  179. user: { type: String, required: true,
  180. _comment: "" },
  181. tag: { type: [String],
  182. _comment: "" },
  183. //createtime: { type: Date, default: Date.now },
  184. });
  185. messageSchema.index({ "$**":'text' });
  186. model.Messages = common.mongoose.model('messages', messageSchema);
  187. model.Messages._list = [ "" ];
  188. var tagSchema = common.mongoose.Schema({
  189. name: { type: String, required: true,
  190. _comment: "" }, //unique
  191. });
  192. model.Tags = common.mongoose.model('tags', messageSchema);
  193. model.Tags._list = [ "" ];
  194. var userSchema = common.mongoose.Schema({
  195. //_id: { type: String },
  196. name: { type: String, required: true,
  197. _comment: "" },
  198. pwd: { type: String,
  199. _comment: "" },
  200. //hash: { type: String },
  201. //salt: { type: String },
  202. //type: { type: String },
  203. roles: { type: [String], required: true,
  204. _comment: "" },
  205. tags: { type: [String],
  206. _comment: "" },
  207. //deactivated: { type: Boolean },
  208. //participating: { type: [String] },
  209. //host: { type: Boolean },
  210. bookmarks: { type: [String],
  211. _comment: "" },
  212. });
  213. model.Users = common.mongoose.model('users', userSchema);
  214. model.Users._list = [ "" ];
  215. },
  216. models: model,
  217. };
  218. /*
  219. app.get ("/api/msg/search/:phrase", function (req, res) {
  220. Message.find ({$text: {$search: req.params.phrase}) .then (function (err, results){
  221. if (err) {
  222. console.log (err);
  223. res .status(404) .json (err);
  224. } else {
  225. console.log(JSON.stringify(results));
  226. res.json(results);
  227. }
  228. });
  229. });
  230. */
  231. module.exports = dbs;