projectRouter.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const express = require("express");
  2. const cors = require('cors');
  3. const projectData = require("../data/projectData.js");
  4. const router = express.Router();
  5. /**
  6. * Read (GET) all projects, sorted by priority.
  7. *
  8. * Acccpeted request parameters:
  9. *
  10. * - lang: Two letter language code, case insensitive.. If the data exists in
  11. * the requested language it will be served, otherwise the default
  12. * language will be used.
  13. *
  14. * - images: True, false, or a positive integer. If true, projects will
  15. * include all of their images, sorted by priority. If false, none
  16. * will be included. If a number is provided, up to that many images
  17. * will be provided. Default value is true.
  18. */
  19. router.get("/", cors({origin: '*', methods: 'GET'}), async (req, res) => {
  20. var images = true;
  21. if (req.query.images == "false") images = false;
  22. else if (parseInt(req.query.images) != NaN && parseInt(req.query.images) >= 0)
  23. images = parseInt(req.query.images);
  24. var lang = process.env.DEFAULT_LANGUAGE;
  25. if (
  26. req.query.lang
  27. && process.env.AVAILABLE_LANGUAGES.split(" ").includes(req.query.lang.toLowerCase())
  28. ) lang = req.query.lang.toLowerCase();
  29. const data = await projectData.getProjects(lang, images);
  30. res.json(data);
  31. });
  32. // Read (GET) a specific project by ID or permalink
  33. router.get("/:id", cors({origin: '*', methods: 'GET'}), async (req, res) => {
  34. var images = true;
  35. if (req.query.images == "false") images = false;
  36. else if (parseInt(req.query.images) != NaN && parseInt(req.query.images) >= 0)
  37. images = parseInt(req.query.images);
  38. var lang = process.env.DEFAULT_LANGUAGE;
  39. if (
  40. req.query.lang
  41. && process.env.AVAILABLE_LANGUAGES.split(" ").includes(req.query.lang.toLowerCase())
  42. ) lang = req.query.lang.toLowerCase();
  43. const data = await projectData.getProject(req.params.id, lang, images);
  44. if (!data) res.status(404).json({ error: "Project not found" });
  45. else res.json(data);
  46. });
  47. // Read (GET) a specific project images by ID or permalink
  48. router.get("/:id/images", cors({origin: '*', methods: 'GET'}), async (req, res) => {
  49. var lang = process.env.DEFAULT_LANGUAGE;
  50. if (
  51. req.query.lang
  52. && process.env.AVAILABLE_LANGUAGES.split(" ").includes(req.query.lang.toLowerCase())
  53. ) lang = req.query.lang.toLowerCase();
  54. var max = null;
  55. if (parseInt(req.query.max) != NaN && parseInt(req.query.max) >= 0)
  56. max = parseInt(req.query.max);
  57. const data = await projectData.getProjectImages(req.params.id, lang, max);
  58. if (!data) res.status(404).json({ error: "Project not found" });
  59. else res.json(data);
  60. });
  61. // Read (GET) a specific project images by ID or permalink
  62. router.get("/:projectId/images/:imageId", cors({origin: '*', methods: 'GET'}), async (req, res) => {
  63. var lang = process.env.DEFAULT_LANGUAGE;
  64. if (
  65. req.query.lang
  66. && process.env.AVAILABLE_LANGUAGES.split(" ").includes(req.query.lang.toLowerCase())
  67. ) lang = req.query.lang.toLowerCase();
  68. const data
  69. = await projectData.getProjectImage(req.params.projectId, req.params.imageId, lang);
  70. if (!data) res.status(404).json({ error: "Image not found" });
  71. else res.json(data);
  72. });
  73. module.exports = router;