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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. "msg": {
  84. /* POST /api/msg
  85. * <- Message schema
  86. * -> Message schema
  87. * Create a new message
  88. */
  89. post: function (req, res) {
  90. /*
  91. if ( !(req.body.tags instanceof Array) ) {
  92. return res.status(400).json({ error: "bad request" });
  93. }*/
  94. //console.log("Subject: "+JSON.stringify(req.body));
  95. model.Messages.create({
  96. subject: req.body.subject,
  97. message: req.body.message,
  98. user: req.body.user,
  99. tag: req.body.tag
  100. }, function (err, result) {
  101. if (err) {
  102. return res.status(401).json(err.message);
  103. } else {
  104. res.json({
  105. message: "Message created!!"
  106. });
  107. }
  108. if (result == null) {
  109. return res.status(500).json("Can not create message.")
  110. }
  111. });
  112. },
  113. /* PUT /api/msg
  114. * <-
  115. * ->
  116. * Update a message
  117. */
  118. //put: function(req, res) {},
  119. },
  120. "tag/ids": {
  121. /* GET /api/tag/ids [no args]
  122. * -> Array of tag schema object ids
  123. * Get ALL known tag ids
  124. */
  125. get: function (req, res) {
  126. model.Tags.find({}, {
  127. _id: true
  128. }).exec()
  129. .then(results => {
  130. //selects id from tag:
  131. var parsed = [];
  132. for (var i in results) {
  133. parsed.push(results[i]._id);
  134. }
  135. res.send(parsed);
  136. })
  137. .catch(err => {
  138. console.log(err);
  139. res.status(500).json(err);
  140. });
  141. },
  142. },
  143. "tag/id": {
  144. params: ":id",
  145. /* GET /api/tag/id/[tag-id]
  146. * -> Tag schema
  147. * Get a particular tag
  148. */
  149. get: function (req, res) {
  150. model.Tags.findById(req.params.id)
  151. .exec(function (err, result) {
  152. if (err) {
  153. console.log(err);
  154. res.status(404).json(err);
  155. } else {
  156. //console.log(req.params.id);
  157. res.json(result);
  158. }
  159. });
  160. },
  161. },
  162. "usr/bookmark/ids": {
  163. /* GET /api/usr/bookmark/ids [no args]
  164. * -> Array of message schema object ids
  165. * Get bookmark message ids
  166. */
  167. get: function (req, res) {
  168. model.Users.find({
  169. },
  170. function (err, result) {
  171. if (err) {
  172. return res.status(401).json(err.message);
  173. } else {
  174. var parsed = [];
  175. for (var i in result) {
  176. parsed.push(result[i].bookmarks);
  177. }
  178. console.log("bookmarks:" + JSON.stringify(parsed));
  179. res.send(parsed);
  180. }
  181. if (result == null) {
  182. return res.status(500).json("Can save bookmark.")
  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.dbConn, {
  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. module.exports = dbs;