license.controller.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @file Provides the model and operation for licenses.
  3. * @author Inigo Valentin
  4. * @since 4.0.0
  5. */
  6. const db = require("../models");
  7. const License = db.licenses;
  8. const Op = db.Sequelize.Op;
  9. const LocaleService = require('../services/locale.service.js');
  10. const localeService = new LocaleService(db);
  11. /**
  12. * Create and Save a new license.
  13. *
  14. * @param req The received request by the server.
  15. * @param res The request to be sent by the server.
  16. */
  17. exports.create = (req, res) => {
  18. // Validate request
  19. if (!req.body.title) {
  20. res.status(400).send({message: "Content can not be empty!"});
  21. return;
  22. }
  23. // Create a License
  24. const License = {
  25. title: req.body.title,
  26. description: req.body.description,
  27. published: req.body.published ? req.body.published : false
  28. };
  29. // Save License in the database
  30. License.create(License)
  31. .then(data => {res.send(data);})
  32. .catch(err => {res.status(500).send({message: err.message || "Some error occurred while creating the License."});});
  33. };
  34. /**
  35. * Retrieve all licenses from the database.
  36. *
  37. * @param req The received request by the server.
  38. * @param res The request to be sent by the server.
  39. */
  40. exports.findAll = (req, res) => {
  41. License.findAll()
  42. .then(async data => {
  43. data = await localeService.localizeLicenses(data, req);
  44. res.send(data);
  45. })
  46. .catch(err => {res.status(500).send({message: err.message || "Some error occurred while retrieving Licenses."});});
  47. };
  48. /**
  49. * Find a single license by it's ID.
  50. *
  51. * @param req The received request by the server.
  52. * @param res The request to be sent by the server.
  53. */
  54. exports.findOne = (req, res) => {
  55. const id = req.params.id;
  56. License.findByPk(id)
  57. .then(async data => {
  58. if (data) {
  59. data = await localeService.localizeLicense(data, req);
  60. res.send(data);
  61. }
  62. else res.status(404).send({message: `Cannot find License with id=${id}.`});}
  63. })
  64. .catch(err => {res.status(500).send({message: "Error retrieving License with id=" + id});});
  65. };
  66. /**
  67. * Update a single license by it's ID.
  68. *
  69. * @param req The received request by the server.
  70. * @param res The request to be sent by the server.
  71. */
  72. exports.update = (req, res) => {
  73. const id = req.params.id;
  74. License.update(req.body, {where: {id: id}})
  75. .then(num => {
  76. if (num == 1) res.send({message: "License was updated successfully."});
  77. else res.send({message: `Cannot update License with id=${id}. Maybe License was not found or req.body is empty!`});
  78. })
  79. .catch(err => {res.status(500).send({message: "Error updating License with id=" + id});});
  80. };
  81. /**
  82. * Delete a single license by it's ID.
  83. *
  84. * @param req The received request by the server.
  85. * @param res The request to be sent by the server.
  86. */
  87. exports.delete = (req, res) => {
  88. const id = req.params.id;
  89. License.destroy({where: {id: id}})
  90. .then(num => {
  91. if (num == 1) res.send({message: "License was deleted successfully!"});
  92. else res.send({message: `Cannot delete License with id=${id}. Maybe License was not found!`});
  93. })
  94. .catch(err => {res.status(500).send({message: "Could not delete License with id=" + id});});
  95. };
  96. /**
  97. * Delete all licenses.
  98. *
  99. * @param req The received request by the server.
  100. * @param res The request to be sent by the server.
  101. */
  102. exports.deleteAll = (req, res) => {
  103. License.destroy({
  104. where: {},
  105. truncate: false
  106. })
  107. .then(nums => {res.send({ message: `${nums} Licenses were deleted successfully!` });})
  108. .catch(err => {res.status(500).send({message: err.message || "Some error occurred while removing all Licenses."});});
  109. };