project.controller.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const db = require("../models");
  2. const Project = db.projects;
  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 Project
  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 Project
  16. const Project = {
  17. title: req.body.title,
  18. description: req.body.description,
  19. published: req.body.published ? req.body.published : false
  20. };
  21. // Save Project in the database
  22. Project.create(Project)
  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 Project."
  30. });
  31. });
  32. };
  33. // Retrieve all Projects from the database.
  34. exports.findAll = (req, res) => {
  35. //const title = req.query.title;
  36. //Project.findAll({ where: condition })
  37. //Project.findAll({include: ["titles"]})
  38. //Project.findAll({where: {visible: true,},include: db.License})
  39. Project.findAll({where: {visible: true,}, include: ["license"], attributes: {exclude: ['licenseId']}})
  40. .then(async data => {
  41. data = await localeService.localizeProjects(data, db);
  42. res.send(data);
  43. })
  44. .catch(err => {res.status(500).send({message: err.message || "Some error occurred while retrieving Projects."});});
  45. };
  46. // Find a single Project with an id
  47. exports.findOne = (req, res) => {
  48. const id = req.params.id;
  49. Project.findByPk(id, {where: {visible: true,}, include: ["license"], attributes: {exclude: ['licenseId']}})
  50. .then(async data => {
  51. if (data) {
  52. data = await localeService.localizeProject(data, db);
  53. res.send(data);
  54. }
  55. else {
  56. res.status(404).send({message: `Cannot find Project with id=${id}.`});
  57. }
  58. })
  59. .catch(err => {res.status(500).send({message: "Error retrieving Project with id=" + id});});
  60. };
  61. // Update a Project by the id in the request
  62. exports.update = (req, res) => {
  63. const id = req.params.id;
  64. Project.update(req.body, {
  65. where: { id: id }
  66. })
  67. .then(num => {
  68. if (num == 1) {
  69. res.send({
  70. message: "Project was updated successfully."
  71. });
  72. } else {
  73. res.send({
  74. message: `Cannot update Project with id=${id}. Maybe Project was not found or req.body is empty!`
  75. });
  76. }
  77. })
  78. .catch(err => {
  79. res.status(500).send({
  80. message: "Error updating Project with id=" + id
  81. });
  82. });
  83. };
  84. // Delete a Project with the specified id in the request
  85. exports.delete = (req, res) => {
  86. const id = req.params.id;
  87. Project.destroy({
  88. where: { id: id }
  89. })
  90. .then(num => {
  91. if (num == 1) {
  92. res.send({
  93. message: "Project was deleted successfully!"
  94. });
  95. } else {
  96. res.send({
  97. message: `Cannot delete Project with id=${id}. Maybe Project was not found!`
  98. });
  99. }
  100. })
  101. .catch(err => {
  102. res.status(500).send({
  103. message: "Could not delete Project with id=" + id
  104. });
  105. });
  106. };
  107. // Delete all Projects from the database.
  108. exports.deleteAll = (req, res) => {
  109. Project.destroy({
  110. where: {},
  111. truncate: false
  112. })
  113. .then(nums => {
  114. res.send({ message: `${nums} Projects were deleted successfully!` });
  115. })
  116. .catch(err => {
  117. res.status(500).send({
  118. message:
  119. err.message || "Some error occurred while removing all Projects."
  120. });
  121. });
  122. };
  123. // Find all published Projects
  124. exports.findAllPublished = (req, res) => {
  125. Project.findAll({ where: { published: true } })
  126. .then(data => {
  127. res.send(data);
  128. })
  129. .catch(err => {
  130. res.status(500).send({
  131. message:
  132. err.message || "Some error occurred while retrieving Projects."
  133. });
  134. });
  135. };