license.controller.js 3.9 KB

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