Advertisement
TiagoMiguel

Untitled

Sep 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const Item = require('../model/Item');
  4. const config = require('../data/config');
  5.  
  6. async function addItem(request, h) {
  7. const cache = request.server.plugins.Cache.cache;
  8. const config = request.server.plugins.Config.config;
  9.  
  10. const payload = request.payload;
  11.  
  12. let id_number = await cache.get('id_number');
  13. id_number = id_number ? id_number : "0";
  14.  
  15. let item = new Item(id_number, payload.description);
  16. const key = {
  17. "segment": "to-do",
  18. "id": id_number
  19. };
  20. await cache.set(key, item, config.cacheTimeline); // ttl- 1 Day
  21.  
  22. id_number = "" + (parseInt(id_number) + 1);
  23.  
  24. await cache.set('id_number', id_number, config.cacheTimeline);
  25.  
  26. return item;
  27. }
  28.  
  29. async function removeItem(request, h) {
  30.  
  31. try {
  32. const cache = request.server.plugins.Cache.cache;
  33.  
  34. const id = request.params.id;
  35. const item = await cache.get({"id": "" + id});
  36.  
  37. if (item === null)
  38. return h.response("Item doesn't exists").code(404);
  39.  
  40. await cache.drop({'id': "" + id});
  41.  
  42. return item;
  43. } catch (e) {
  44. console.log(e);
  45. return e;
  46. }
  47. }
  48.  
  49. async function editItem(request, h) {
  50.  
  51. try {
  52.  
  53. const cache = request.server.plugins.Cache.cache;
  54. const config = request.server.plugins.Config.config;
  55.  
  56. const payload = request.payload;
  57.  
  58. const id = request.params.id + "";
  59. const item = await cache.get({'id': id});
  60. if (item === null)
  61. return h.response("Item doesn't exist").code(404);
  62. if (item.state === "COMPLETE")
  63. return h.response("Item already complete").code(400);
  64.  
  65. item.state = payload.state;
  66. item.description = payload.description;
  67.  
  68. const key = {
  69. "segment": "to-do",
  70. "id": id
  71. };
  72. await cache.set(key, item, config.cacheTimeline); // ttl- 1 Day
  73. return item;
  74.  
  75. } catch (e) {
  76. console.log(e)
  77. return e;
  78. }
  79. }
  80.  
  81. async function listItens(request, h) {
  82. const cache = request.server.plugins.Cache.cache;
  83.  
  84. let id_number = await cache.get('id_number');
  85. id_number = id_number ? parseInt(id_number) : 0;
  86.  
  87. const query = request.query;
  88. const orderBy = query.orderBy;
  89. const filter = query.filter;
  90.  
  91. let list = [];
  92. for (let i = 0; i <= id_number; i++) {
  93. let item = await cache.get({
  94. 'id': '' + i
  95. });
  96. if (item)
  97. list.push(item);
  98. }
  99. if (orderBy) {
  100. const property = (() => {
  101. switch (query.orderBy) {
  102. case 'DESCRIPTION':
  103. return "description";
  104. case 'DATE_ADDED':
  105. default:
  106. return "dateAdded";
  107. }
  108. })();
  109. list.sort((a, b) => (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0);
  110. }
  111.  
  112. if (filter)
  113. list = list.filter((item) => item.state === query.filter || query.filter === "ALL");
  114.  
  115. return list;
  116. }
  117.  
  118. module.exports = {
  119. addItem,
  120. removeItem,
  121. editItem,
  122. listItens
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement