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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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({}, {
  20. _id: true
  21. }).exec()
  22. .then(results => {
  23. //selects id from message:
  24. var parsed = [];
  25. for (var i in results) {
  26. parsed.push(results[i]._id);
  27. }
  28. res.send(parsed);
  29. })
  30. .catch(err => {
  31. console.log(err);
  32. res.status(500).json(err);
  33. });
  34. },
  35. },
  36. "msg/id": {
  37. params: ":id",
  38. /* GET /api/msg/id/[massage-id]
  39. * -> Message schema
  40. * Get a particular message
  41. */
  42. get: function (req, res) {
  43. model.Messages.findById(req.params.id).exec(function (err, result) {
  44. if (err) {
  45. console.log(err);
  46. res.status(404).json(err);
  47. } else {
  48. //console.log(JSON.stringify(result));
  49. res.json(result);
  50. }
  51. });
  52. },
  53. },
  54. "msg/search": {
  55. params: ":phrase",
  56. /* GET /api/msg/id/[massage-id]
  57. * -> Message schema
  58. * Get a particular message
  59. */
  60. get: function (req, res) {
  61. model.Messages.find({
  62. $text: {
  63. $search: req.params.phrase
  64. }
  65. })
  66. .sort({
  67. tag: -1
  68. })
  69. .exec().then(results => {
  70. //selects id from message:
  71. var parsed = [];
  72. for (var i in results) {
  73. parsed.push(results[i]._id);
  74. }
  75. res.send(parsed);
  76. })
  77. .catch(err => {
  78. console.log(err);
  79. res.status(500).json(err);
  80. });
  81. },
  82. },
  83. "usr/bookmark/ids": {
  84. /* GET /api/usr/bookmark/ids [no args]
  85. * -> Array of message schema object ids
  86. * Get bookmark message ids
  87. */
  88. get: function (req, res) {
  89. model.Users.find({
  90. },
  91. function (err, result) {
  92. if (err) {
  93. return res.status(401).json(err.message);
  94. } else {
  95. var parsed = [];
  96. for (var i in result) {
  97. parsed.push(result[i].bookmarks);
  98. }
  99. console.log("bookmarks:" + JSON.stringify(parsed));
  100. res.send(parsed);
  101. }
  102. if (result == null) {
  103. return res.status(500).json("Can save bookmark.")
  104. }
  105. });
  106. },
  107. },
  108. "msg": {
  109. /* POST /api/msg
  110. * <- Message schema
  111. * -> Message schema
  112. * Create a new message
  113. */
  114. post: function (req, res) {
  115. /*
  116. if ( !(req.body.tags instanceof Array) ) {
  117. return res.status(400).json({ error: "bad request" });
  118. }*/
  119. //console.log("Subject: "+JSON.stringify(req.body));
  120. model.Messages.create({
  121. subject: req.body.subject,
  122. message: req.body.message,
  123. user: req.body.user,
  124. tag: req.body.tag
  125. }, function (err, result) {
  126. if (err) {
  127. return res.status(401).json(err.message);
  128. } else {
  129. res.json({
  130. message: "Message created!!"
  131. });
  132. }
  133. if (result == null) {
  134. return res.status(500).json("Can not create message.")
  135. }
  136. });
  137. },
  138. /* PUT /api/msg
  139. * <-
  140. * ->
  141. * Update a message
  142. */
  143. //put: function(req, res) {},
  144. },
  145. "tag/ids": {
  146. /* GET /api/tag/ids [no args]
  147. * -> Array of tag schema object ids
  148. * Get ALL known tag ids
  149. */
  150. get: function (req, res) {
  151. model.Tags.find({}, {
  152. _id: true
  153. }).exec()
  154. .then(results => {
  155. //selects id from tag:
  156. var parsed = [];
  157. for (var i in results) {
  158. parsed.push(results[i]._id);
  159. }
  160. res.send(parsed);
  161. })
  162. .catch(err => {
  163. console.log(err);
  164. res.status(500).json(err);
  165. });
  166. },
  167. },
  168. "tag/id": {
  169. params: ":id",
  170. /* GET /api/tag/id/[tag-id]
  171. * -> Tag schema
  172. * Get a particular tag
  173. */
  174. get: function (req, res) {
  175. model.Tags.findById(req.params.id)
  176. .exec(function (err, result) {
  177. if (err) {
  178. console.log(err);
  179. res.status(404).json(err);
  180. } else {
  181. //console.log(req.params.id);
  182. res.json(result);
  183. }
  184. });
  185. },
  186. },
  187. "usr/id": {
  188. params: ":id",
  189. /* GET /api/usr/[usr-id]
  190. * -> User Schema
  191. * Get a particular user
  192. */
  193. get: function (req, res) {
  194. model.Users.find({
  195. _id: req.params.id
  196. })
  197. .exec(function (err, result) {
  198. if (err) {
  199. console.log(err);
  200. res.status(404).json(err);
  201. } else {
  202. console.log(result);
  203. res.json(result);
  204. }
  205. });
  206. },
  207. },
  208. "usr": {
  209. /* POST /api/usr
  210. * <- User schema
  211. * -> User schema
  212. * Create a new user
  213. */
  214. post: function(req, res) {
  215. // console.info(req.body);
  216. model.Users.create({
  217. _id: req.body._id,
  218. name: req.body.name,
  219. mail: req.body.mail,
  220. type: req.body.type,
  221. gender: req.body.gender,
  222. abos: req.body.abos,
  223. bookmarks: req.body.bookmarks,
  224. roles: req.body.roles,
  225. }, function(err, result) {
  226. if (err) {
  227. console.log(err);
  228. return res.status(401).json(err.message);
  229. } else {
  230. res.json({message: "User created!", user: req.body});
  231. }
  232. if (result == null) {
  233. return res.status(500).json("Can not create user.")
  234. }
  235. });
  236. },
  237. /* PUT /api/usr
  238. * <- User schema
  239. * -> User schema
  240. * Change a user
  241. */
  242. put: function (req, res) {
  243. model.Users.find({
  244. _id: req.body.userid
  245. })
  246. .exec(function (err, entry) {
  247. if (err) {
  248. console.log(err);
  249. } else {
  250. model.Users.update({
  251. _id: req.body.userid
  252. }, {
  253. bookmarks: req.body.bookmark
  254. }, function (err, data) {
  255. if (err) {
  256. console.log(err);
  257. } else {
  258. res.json({
  259. id: "Bookmark saved"
  260. });
  261. }
  262. if (data == null) {
  263. return res.status(404).json(err.message);
  264. }
  265. });
  266. }
  267. if (entry == null)
  268. return res.status(404).json(err.message);
  269. // TODO Catch edge cases
  270. // TODO Save/Update user
  271. /*
  272. _id: req.body.id,
  273. name: req.body.name,
  274. type: req.body.type,
  275. roles: req.body.roles,
  276. hash: req.body.hash,
  277. salt: req.body.salt,
  278. pwd: req.body.pwd,
  279. abos: req.body.abos,
  280. bookmarks: req.body.bookmarks,
  281. */
  282. // res.json(data);
  283. });
  284. },
  285. },
  286. },
  287. /* Initialize requirements
  288. * - DB connection
  289. * - DB schemata
  290. */
  291. init: function (_common) {
  292. common = _common;
  293. /* DB Connection
  294. * Local db: common.config.dbLocalConn
  295. * TH db: common.config.dbConn
  296. */
  297. common.mongoose.connect(common.config.dbLocalConn, {
  298. useNewUrlParser: true
  299. }).then(() => {
  300. console.log("Database connected successfully.");
  301. }).catch(err => {
  302. console.log('Database connection error.');
  303. process.exit();
  304. });
  305. // Default 'collection.ensureIndex' is deprecated; New is '.createIndexes'
  306. common.mongoose.set('useCreateIndex', true);
  307. /* DB Schemata
  308. * Privat fields:
  309. * - per model: _list: Elements that are included in list fetch
  310. * - per entry: _comment: Comment for Admin UI - TODO: not working yet
  311. */
  312. var messageSchema = common.mongoose.Schema({
  313. subject: {
  314. type: String,
  315. required: true,
  316. _comment: ""
  317. },
  318. message: {
  319. type: String,
  320. required: true,
  321. _comment: ""
  322. },
  323. user: {
  324. type: String,
  325. required: true,
  326. _comment: ""
  327. },
  328. tag: {
  329. type: [String],
  330. _comment: ""
  331. },
  332. //createtime: { type: Date, default: Date.now },
  333. });
  334. messageSchema.index({
  335. "$**": 'text'
  336. });
  337. model.Messages = common.mongoose.model('messages', messageSchema);
  338. model.Messages._list = [""];
  339. var tagSchema = common.mongoose.Schema({
  340. name: {
  341. type: String,
  342. required: true,
  343. _comment: ""
  344. }, //unique
  345. });
  346. model.Tags = common.mongoose.model('tags', messageSchema);
  347. model.Tags._list = [""];
  348. var userSchema = common.mongoose.Schema({
  349. _id: { type: String, required: true },
  350. name: { type: String,
  351. _comment: "" },
  352. mail: { type: String },
  353. type: { type: String },
  354. gender: { type: String },
  355. pwd: { type: String,
  356. _comment: "" },
  357. hash: { type: String },
  358. salt: { type: String },
  359. abos: { type: [String],
  360. _comment: "" },
  361. //deactivated: { type: Boolean },
  362. //host: { type: Boolean },
  363. bookmarks: { type: [String],
  364. _comment: "" },
  365. sessionid: { type: String },
  366. roles: { type: [String], required: true,
  367. _comment: "" },
  368. });
  369. model.Users = common.mongoose.model('users', userSchema);
  370. model.Users._list = [""];
  371. },
  372. models: model,
  373. };
  374. /*
  375. app.get ("/api/msg/search/:phrase", function (req, res) {
  376. Message.find ({$text: {$search: req.params.phrase}) .then (function (err, results){
  377. if (err) {
  378. console.log (err);
  379. res .status(404) .json (err);
  380. } else {
  381. console.log(JSON.stringify(results));
  382. res.json(results);
  383. }
  384. });
  385. });
  386. */
  387. module.exports = dbs;