project.controller.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @file Provides the model and operation for projects.
  3. * @author Inigo Valentin
  4. * @since 4.0.0
  5. */
  6. const db = require("../models");
  7. const Project = db.projects;
  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 project.
  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 Project
  24. const Project = {
  25. title: req.body.title,
  26. description: req.body.description,
  27. published: req.body.published ? req.body.published : false
  28. };
  29. // Save Project in the database
  30. Project.create(Project)
  31. .then(data => {res.send(data);})
  32. .catch(err => { res.status(500).send({ message: err.message || "Some error occurred while creating the Project." }); });
  33. };
  34. /**
  35. * Retrieve all projects 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. Project.findAll({where: {visible: true,}, include: ["license", "type", "tags", {model: ProjectUrl, include: "type"}], attributes: { exclude: ['licenseId', 'projectTypeId'] }, order: [['idx', 'ASC'], ['id', 'DESC']] })
  42. .then(async data => {
  43. data = await localeService.localizeProjects(data, req);
  44. res.send(data);
  45. })
  46. .catch(err => { res.status(500).send({message: err.message || "Some error occurred while retrieving Projects."}); });
  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. Project.findOne({ where: { visible: true, [Op.or]: {permalink: id, id: id }}, include: ["license", "type", "tags", {model: ProjectUrl, include: "type"}, "project-images"], attributes: { exclude: ['licenseId', 'projectTypeId'] } })
  57. //Project.findByPk(id, { where: { visible: true }, include: ["license"], attributes: {exclude: ['licenseId'] } })
  58. .then(async data => {
  59. if (data) {
  60. data = await localeService.localizeProject(data, req);
  61. res.send(data);
  62. }
  63. else res.status(404).send({ message: `Cannot find Project with permalink or id=${id}.` });
  64. })
  65. .catch(err => { res.status(500).send({ message: "Error retrieving Project with permalink or id=" + id + ": " + err}); });
  66. };
  67. /**
  68. * Update a single project by it's ID.
  69. *
  70. * @param req The received request by the server.
  71. * @param res The request to be sent by the server.
  72. */
  73. exports.update = (req, res) => {
  74. const id = req.params.id;
  75. Project.update(req.body, {
  76. where: { id: id }
  77. })
  78. .then(num => {
  79. if (num == 1) res.send({ message: "Project was updated successfully." });
  80. else res.send({ message: `Cannot update Project with id=${id}. Maybe Project was not found or req.body is empty!` });
  81. })
  82. .catch(err => { res.status(500).send({ message: "Error updating Project with id=" + id }); });
  83. };
  84. /**
  85. * Delete a single project by it's ID.
  86. *
  87. * @param req The received request by the server.
  88. * @param res The request to be sent by the server.
  89. */
  90. exports.delete = (req, res) => {
  91. const id = req.params.id;
  92. Project.destroy({ where: { id: id } })
  93. .then(num => {
  94. if (num == 1) res.send({ message: "Project was deleted successfully!" });
  95. else res.send({ message: `Cannot delete Project with id=${id}. Maybe Project was not found!` });
  96. })
  97. .catch(err => { res.status(500).send({ message: "Could not delete Project with id=" + id }); });
  98. };
  99. /**
  100. * Delete all projects.
  101. *
  102. * @param req The received request by the server.
  103. * @param res The request to be sent by the server.
  104. */
  105. exports.deleteAll = (req, res) => {
  106. Project.destroy({
  107. where: {},
  108. truncate: false
  109. })
  110. .then(nums => { res.send({ message: `${nums} Projects were deleted successfully!` }); })
  111. .catch(err => { res.status(500).send({ message: err.message || "Some error occurred while removing all Projects." }); });
  112. };