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

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