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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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})
  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/id": {
  133. params: ":id",
  134. /* GET /api/tag/id/[tag-id]
  135. * -> Tag schema
  136. * Get a particular tag
  137. */
  138. get: function(req, res) {
  139. model.Tags.findById(req.params.id)
  140. .exec(function(err, result) {
  141. if (err) {
  142. console.log (err);
  143. res.status(404).json(err);
  144. } else {
  145. //console.log(JSON.stringify(result));
  146. res.json(result);
  147. }
  148. });
  149. },
  150. },
  151. "usr/id": {
  152. params: ":id",
  153. /* GET /api/usr/[usr-id]
  154. * -> User Schema
  155. * Get a particular user
  156. */
  157. get: function(req, res) {
  158. model.Users.findById(req.params.id)
  159. .exec(function(err, result) {
  160. if (err) {
  161. console.log(err);
  162. res.status(404).json(err);
  163. } else {
  164. //console.log(JSON.stringify(result));
  165. res.json(result);
  166. }
  167. });
  168. },
  169. },
  170. "usr": {
  171. /* POST /api/usr
  172. * <- User schema
  173. * -> User schema
  174. * Create a new user
  175. */
  176. post: function(req, res) {
  177. model.User.create({
  178. _id: req.body.id,
  179. name: req.body.name,
  180. type: req.body.type,
  181. roles: req.body.roles,
  182. hash: req.body.hash,
  183. salt: req.body.salt,
  184. pwd: req.body.pwd,
  185. abos: req.body.abos,
  186. bookmarks: req.body.bookmarks,
  187. }, function(err, result) {
  188. if (err) {
  189. console.log (err);
  190. return res.status(401).json(err.message);
  191. } else {
  192. res.json({message: "User created!!"});
  193. }
  194. if (result == null) {
  195. return res.status(500).json("Can not create user.")
  196. }
  197. });
  198. },
  199. /* PUT /api/usr
  200. * <- User schema
  201. * -> User schema
  202. * Change a user
  203. */
  204. put: function(req, res) {
  205. model.Users.findById(reg.body._id)
  206. .exec(function(err, entry) {
  207. if (err)
  208. console.log (err);
  209. if (entry == null)
  210. return res.status(404).json(err.message);
  211. // TODO Catch edge cases
  212. entry.save(function(err, data) {
  213. // TODO Save/Update user
  214. /*
  215. _id: req.body.id,
  216. name: req.body.name,
  217. type: req.body.type,
  218. roles: req.body.roles,
  219. hash: req.body.hash,
  220. salt: req.body.salt,
  221. pwd: req.body.pwd,
  222. abos: req.body.abos,
  223. bookmarks: req.body.bookmarks,
  224. */
  225. res.json(data);
  226. });
  227. });
  228. },
  229. },
  230. },
  231. /* Initialize requirements
  232. * - DB connection
  233. * - DB schemata
  234. */
  235. init: function (_common) {
  236. common = _common;
  237. /* DB Connection
  238. * Local db: common.config.dbLocalConn
  239. * TH db: common.config.dbConn
  240. */
  241. common.mongoose.connect (common.config.dbLocalConn, {
  242. useNewUrlParser: true
  243. }).then(() => {
  244. console.log("Database connected successfully.");
  245. }).catch(err => {
  246. console.log('Database connection error.');
  247. process.exit();
  248. });
  249. // Default 'collection.ensureIndex' is deprecated; New is '.createIndexes'
  250. common.mongoose.set('useCreateIndex', true);
  251. /* DB Schemata
  252. * Privat fields:
  253. * - per model: _list: Elements that are included in list fetch
  254. * - per entry: _comment: Comment for Admin UI - TODO: not working yet
  255. */
  256. var messageSchema = common.mongoose.Schema({
  257. subject: { type: String, required: true,
  258. _comment: "" },
  259. message: { type: String, required: true,
  260. _comment: "" },
  261. user: { type: String, required: true,
  262. _comment: "" },
  263. tag: { type: [String],
  264. _comment: "" },
  265. //createtime: { type: Date, default: Date.now },
  266. });
  267. messageSchema.index({ "$**":'text' });
  268. model.Messages = common.mongoose.model('messages', messageSchema);
  269. model.Messages._list = [ "" ];
  270. var tagSchema = common.mongoose.Schema({
  271. name: { type: String, required: true,
  272. _comment: "" }, //unique
  273. });
  274. model.Tags = common.mongoose.model('tags', messageSchema);
  275. model.Tags._list = [ "" ];
  276. var userSchema = common.mongoose.Schema({
  277. _id: { type: String },
  278. name: { type: String, required: true,
  279. _comment: "" },
  280. pwd: { type: String,
  281. _comment: "" },
  282. hash: { type: String },
  283. salt: { type: String },
  284. type: { type: String },
  285. roles: { type: [String], required: true,
  286. _comment: "" },
  287. abos: { type: [String],
  288. _comment: "" },
  289. //deactivated: { type: Boolean },
  290. //host: { type: Boolean },
  291. bookmarks: { type: [String],
  292. _comment: "" },
  293. });
  294. model.Users = common.mongoose.model('users', userSchema);
  295. model.Users._list = [ "" ];
  296. },
  297. models: model,
  298. };
  299. /*
  300. app.get ("/api/msg/search/:phrase", function (req, res) {
  301. Message.find ({$text: {$search: req.params.phrase}) .then (function (err, results){
  302. if (err) {
  303. console.log (err);
  304. res .status(404) .json (err);
  305. } else {
  306. console.log(JSON.stringify(results));
  307. res.json(results);
  308. }
  309. });
  310. });
  311. */
  312. module.exports = dbs;