text.controller.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const db = require("../models");
  2. const Text = db.texts;
  3. const Op = db.Sequelize.Op;
  4. // Create and Save a new Text
  5. exports.create = (req, res) => {
  6. // Validate request
  7. if (!req.body.title) {
  8. res.status(400).send({
  9. message: "Content can not be empty!"
  10. });
  11. return;
  12. }
  13. // Create a Text
  14. const Text = {
  15. title: req.body.title,
  16. description: req.body.description,
  17. published: req.body.published ? req.body.published : false
  18. };
  19. // Save Text in the database
  20. Text.create(Text)
  21. .then(data => {
  22. res.send(data);
  23. })
  24. .catch(err => {
  25. res.status(500).send({
  26. message:
  27. err.message || "Some error occurred while creating the Text."
  28. });
  29. });
  30. };
  31. // Retrieve all Texts from the database.
  32. exports.findAll = (req, res) => {
  33. //const title = req.query.title;
  34. //var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;
  35. //Text.findAll({ where: condition })
  36. Text.findAll()
  37. .then(data => {
  38. res.send(data);
  39. })
  40. .catch(err => {
  41. res.status(500).send({
  42. message:
  43. err.message || "Some error occurred while retrieving Texts."
  44. });
  45. });
  46. };
  47. // Find a single Text with an id
  48. exports.findOne = (req, res) => {
  49. const id = req.params.id;
  50. Text.findByPk(id)
  51. .then(data => {
  52. if (data) {
  53. res.send(data);
  54. } else {
  55. res.status(404).send({
  56. message: `Cannot find Text with id=${id}.`
  57. });
  58. }
  59. })
  60. .catch(err => {
  61. res.status(500).send({
  62. message: "Error retrieving Text with id=" + id
  63. });
  64. });
  65. };
  66. // Update a Text by the id in the request
  67. exports.update = (req, res) => {
  68. const id = req.params.id;
  69. Text.update(req.body, {
  70. where: { id: id }
  71. })
  72. .then(num => {
  73. if (num == 1) {
  74. res.send({
  75. message: "Text was updated successfully."
  76. });
  77. } else {
  78. res.send({
  79. message: `Cannot update Text with id=${id}. Maybe Text was not found or req.body is empty!`
  80. });
  81. }
  82. })
  83. .catch(err => {
  84. res.status(500).send({
  85. message: "Error updating Text with id=" + id
  86. });
  87. });
  88. };
  89. // Delete a Text with the specified id in the request
  90. exports.delete = (req, res) => {
  91. const id = req.params.id;
  92. Text.destroy({
  93. where: { id: id }
  94. })
  95. .then(num => {
  96. if (num == 1) {
  97. res.send({
  98. message: "Text was deleted successfully!"
  99. });
  100. } else {
  101. res.send({
  102. message: `Cannot delete Text with id=${id}. Maybe Text was not found!`
  103. });
  104. }
  105. })
  106. .catch(err => {
  107. res.status(500).send({
  108. message: "Could not delete Text with id=" + id
  109. });
  110. });
  111. };
  112. // Delete all Texts from the database.
  113. exports.deleteAll = (req, res) => {
  114. Text.destroy({
  115. where: {},
  116. truncate: false
  117. })
  118. .then(nums => {
  119. res.send({ message: `${nums} Texts were deleted successfully!` });
  120. })
  121. .catch(err => {
  122. res.status(500).send({
  123. message:
  124. err.message || "Some error occurred while removing all Texts."
  125. });
  126. });
  127. };
  128. // Find all published Texts
  129. exports.findAllPublished = (req, res) => {
  130. Text.findAll({ where: { published: true } })
  131. .then(data => {
  132. res.send(data);
  133. })
  134. .catch(err => {
  135. res.status(500).send({
  136. message:
  137. err.message || "Some error occurred while retrieving Texts."
  138. });
  139. });
  140. };