history.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Elysia } from 'elysia'
  2. import { Eta } from "eta"
  3. import Meteostanica from '../../utils/meteostanica'
  4. const eta = new Eta({ views: "./templates" })
  5. export default (langName, lang) => new Elysia({ prefix: `/history` })
  6. .get('/', ({ set }) => {
  7. set.headers['content-type'] = 'text/html; charset=utf8'
  8. return eta.render(`${langName}/history/index`, { lang })
  9. })
  10. .get(`/:property`, ({ params: { property }, query: { day, month, year }, set }) => {
  11. const dateMap = Meteostanica.getDateMap()
  12. const years = Object.keys(dateMap)
  13. const months = Object.keys(dateMap[years[years.length - 1]])
  14. const days = dateMap[years[years.length - 1]][months[months.length - 1]]
  15. const lastYear = years[years.length - 1]
  16. const lastMonth = months[months.length - 1]
  17. const lastDay = days[days.length - 1]
  18. const data = Meteostanica.getDataPropertyDaily(property, `${year ?? lastYear}-${month ?? lastMonth}-${day ?? lastDay}`)
  19. if (!data) {
  20. set.headers['content-type'] = 'text/html; charset=utf8'
  21. return eta.render(`${langName}/history/notFound`, { property })
  22. }
  23. set.headers['content-type'] = 'text/html; charset=utf8'
  24. return eta.render(`${langName}/history/property`, { lang, dateMap: { years, months, days, raw: dateMap }, type: `daily`, property, data })
  25. })
  26. .get(`/:property/daily`, ({ params: { property }, query: { day, month, year }, set }) => {
  27. const dateMap = Meteostanica.getDateMap()
  28. const years = Object.keys(dateMap)
  29. const months = Object.keys(dateMap[years[years.length - 1]])
  30. const days = dateMap[years[years.length - 1]][months[months.length - 1]]
  31. const lastYear = years[years.length - 1]
  32. const lastMonth = months[months.length - 1]
  33. const lastDay = days[days.length - 1]
  34. const data = Meteostanica.getDataPropertyDaily(property, `${year ?? lastYear}-${month ?? lastMonth}-${day ?? lastDay}`)
  35. if (!data) {
  36. set.headers['content-type'] = 'text/html; charset=utf8'
  37. return eta.render(`${langName}/history/notFound`, { property })
  38. }
  39. set.headers['content-type'] = 'text/html; charset=utf8'
  40. return eta.render(`${langName}/history/property`, { lang, dateMap: { years, months, days, raw: dateMap }, type: `daily`, property, data })
  41. })
  42. .get(`/:property/monthly`, ({ params: { property }, query: { month, year }, set }) => {
  43. const dateMap = Meteostanica.getDateMap()
  44. const years = Object.keys(dateMap)
  45. const months = Object.keys(dateMap[years[years.length - 1]])
  46. const lastYear = years[years.length - 1]
  47. const lastMonth = months[months.length - 1]
  48. const data = Meteostanica.getDataPropertyMonthly(property, `${year ?? lastYear}-${month ?? lastMonth}`)
  49. if (!data) {
  50. set.headers['content-type'] = 'text/html; charset=utf8'
  51. return eta.render(`${langName}/history/notFound`, { property })
  52. }
  53. set.headers['content-type'] = 'text/html; charset=utf8'
  54. return eta.render(`${langName}/history/property`, { lang, dateMap: { years, months, raw: dateMap }, type: `monthly`, property, data })
  55. })
  56. .get(`/:property/yearly`, ({ params: { property }, query: { year }, set }) => {
  57. const dateMap = Meteostanica.getDateMap()
  58. const years = Object.keys(dateMap)
  59. const lastYear = years[years.length - 1]
  60. const data = Meteostanica.getDataPropertyYearly(property, year ?? lastYear)
  61. if (!data) {
  62. set.headers['content-type'] = 'text/html; charset=utf8'
  63. return eta.render(`${langName}/history/notFound`, { property })
  64. }
  65. set.headers['content-type'] = 'text/html; charset=utf8'
  66. return eta.render(`${langName}/history/property`, { lang, dateMap: { years, raw: dateMap }, type: `yearly`, property, data })
  67. })
  68. .get(`/:property/allTime`, ({ params: { property }, set }) => {
  69. const data = Meteostanica.getDataPropertyAllTime(property)
  70. const dateMap = Meteostanica.getDateMap()
  71. if (!data) {
  72. set.headers['content-type'] = 'text/html; charset=utf8'
  73. return eta.render(`${langName}/history/notFound`, { property })
  74. }
  75. set.headers['content-type'] = 'text/html; charset=utf8'
  76. return eta.render(`${langName}/history/property`, { lang, dateMap: { raw: dateMap }, type: `allTime`, property, data })
  77. })