text.controller.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @file Provides the model and operation for texts.
  3. * @author Inigo Valentin
  4. * @since 4.0.0
  5. */
  6. const logger = require('pino')();
  7. const db = require("../models");
  8. const Text = db.text;
  9. const Op = db.Sequelize.Op;
  10. const AuthService = require("../services/auth.service.js");
  11. const authService = new AuthService(db);
  12. /**
  13. * Create and save a new text.
  14. *
  15. * @param req The received request by the server.
  16. * @param res The request to be sent by the server.
  17. */
  18. exports.create = (req, res) => {
  19. // Check authentication
  20. if (authService.validateToken(req, res).success === false){
  21. res.status(validation.code).send(validation.message);
  22. return;
  23. }
  24. // Validate request
  25. let missing = [];
  26. if (!req.body.id) missing.push("id");
  27. if (!req.body.lang) missing.push("lang");
  28. if (!req.body.section) missing.push("section");
  29. if (!req.body.text && !req.body.file) missing.push("text or file");
  30. if (missing.length > 0){
  31. let message = "Missing required fields: [";
  32. for (let i = 0; i < missing.length; i ++){
  33. message += missing[i];
  34. if (i < missing.length - 1) message += ", ";
  35. }
  36. message += "].";
  37. res.status(400).send(message);
  38. return;
  39. }
  40. // TODO: Validate
  41. // TODO: Accept only text or file
  42. // Create a Text
  43. const text = {id: id, lang: lang, section: section, text: text, file: file};
  44. // Save text in the database
  45. Text.create(text)
  46. .then(data => {res.send(data);})
  47. .catch(err => {
  48. logger.error("Error creating text: " + err);
  49. res.status(500).send("Error creating text.");
  50. });
  51. };
  52. /**
  53. * Retrieve all texts from the database.
  54. *
  55. * @param req The received request by the server.
  56. * @param res The request to be sent by the server.
  57. */
  58. exports.findAll = (req, res) => {
  59. // TODO: There is very little reason for this to exist
  60. // Check authentication
  61. if (authService.validateToken(req, res).success === false){
  62. res.status(validation.code).send(validation.message);
  63. return;
  64. }
  65. Text.findAll()
  66. .then(async data => {res.send(data);})
  67. .catch(err => {
  68. logger.error("Error retrieving texts: " + err);
  69. res.status(500).send("Error retrieving texts.");
  70. });
  71. };
  72. /**
  73. * Find a single project.
  74. *
  75. * @param req The received request by the server.
  76. * @param res The request to be sent by the server.
  77. */
  78. exports.findOne = (req, res) => {
  79. const id = req.params.id;
  80. const lang = req.params.lang;
  81. // TODO: Validate id and lang
  82. Text.findOne({where: {id: id, lang: lang}})
  83. .then(async data => {
  84. if (data) res.send(data);
  85. else res.status(404).send(`No text with id ${id} nd lang ${lang}.`);
  86. })
  87. .catch(err => {
  88. logger.error("Error retrieving text with id " + id + " in lang " + lang + ": " + err);
  89. res.status(500).send("Error retrieving text with id " + id + " in lang " + lang + ".");
  90. });
  91. };
  92. /**
  93. * Update a single text.
  94. *
  95. * @param req The received request by the server.
  96. * @param res The request to be sent by the server.
  97. */
  98. exports.update = (req, res) => {
  99. // Check authentication
  100. if (authService.validateToken(req, res).success === false){
  101. res.status(validation.code).send(validation.message);
  102. return;
  103. }
  104. const id = req.body.id;
  105. const lang = req.body.lang;
  106. Text.update(req.body, {where: {id: id, lang: lang}})
  107. .then(num => {
  108. if (num == 1) res.send("Text updated successfully.");
  109. else res.send(`Cannot update text with id ${id} nd lang ${lang}.`);
  110. })
  111. .catch(err => {
  112. logger.error("Error updating text with id " + id + " in lang " + lang + ": " + err);
  113. res.status(500).send("Error updating text with id " + id + " in lang " + lang + ".");
  114. });
  115. };
  116. /**
  117. * Delete a single text.
  118. *
  119. * @param req The received request by the server.
  120. * @param res The request to be sent by the server.
  121. */
  122. exports.delete = (req, res) => {
  123. // Check authentication
  124. if (authService.validateToken(req, res).success === false){
  125. res.status(validation.code).send(validation.message);
  126. return;
  127. }
  128. const id = req.body.id;
  129. const lang = req.body.lang;
  130. Project.destroy({where: {id: id, lang: lang}})
  131. .then(num => {
  132. if (num == 1) res.send("Text deleted successfully.");
  133. else res.send(`Cannot delete text with id ${id} nd lang ${lang}.`);
  134. })
  135. .catch(err => {
  136. logger.error("Error deleting text with id " + id + " in lang " + lang + ": " + err);
  137. res.status(500).send("Error deleting text with id " + id + " in lang " + lang + ".");
  138. });
  139. };
  140. /**
  141. * Delete all texts.
  142. *
  143. * @param req The received request by the server.
  144. * @param res The request to be sent by the server.
  145. */
  146. exports.deleteAll = (req, res) => {
  147. // TODO: There is very little reason for this to exist
  148. // Check authentication
  149. if (authService.validateToken(req, res).success === false){
  150. res.status(validation.code).send(validation.message);
  151. return;
  152. }
  153. Text.destroy({truncate: false})
  154. .then(nums => {res.send(`${nums} texts deleted successfully.`);})
  155. .catch(err => {
  156. logger.error("Error deleting texts: " + err);
  157. res.status(500).send("Error deleting texts.");
  158. });
  159. };