import { Elysia } from 'elysia' import { Eta } from "eta" import Meteostanica from '../../utils/meteostanica' const eta = new Eta({ views: "./templates" }) export default (langName, lang) => new Elysia({ prefix: `/history` }) .get('/', ({ set }) => { set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/index`, { lang }) }) .get(`/:property`, ({ params: { property }, query: { day, month, year }, set }) => { const dateMap = Meteostanica.getDateMap() const years = Object.keys(dateMap) const months = Object.keys(dateMap[years[years.length - 1]]) const days = dateMap[years[years.length - 1]][months[months.length - 1]] const lastYear = years[years.length - 1] const lastMonth = months[months.length - 1] const lastDay = days[days.length - 1] const data = Meteostanica.getDataPropertyDaily(property, `${year ?? lastYear}-${month ?? lastMonth}-${day ?? lastDay}`) if (!data) { set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/notFound`, { property }) } set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/property`, { lang, dateMap: { years, months, days }, type: `daily`, property, data }) }) .get(`/:property/daily`, ({ params: { property }, query: { day, month, year }, set }) => { const dateMap = Meteostanica.getDateMap() const years = Object.keys(dateMap) const months = Object.keys(dateMap[years[years.length - 1]]) const days = dateMap[years[years.length - 1]][months[months.length - 1]] const lastYear = years[years.length - 1] const lastMonth = months[months.length - 1] const lastDay = days[days.length - 1] const data = Meteostanica.getDataPropertyDaily(property, `${year ?? lastYear}-${month ?? lastMonth}-${day ?? lastDay}`) if (!data) { set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/notFound`, { property }) } set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/property`, { lang, dateMap: { years, months, days }, type: `daily`, property, data }) }) .get(`/:property/monthly`, ({ params: { property }, query: { month, year }, set }) => { const dateMap = Meteostanica.getDateMap() const years = Object.keys(dateMap) const months = Object.keys(dateMap[years[years.length - 1]]) const lastYear = years[years.length - 1] const lastMonth = months[months.length - 1] const data = Meteostanica.getDataPropertyMonthly(property, `${year ?? lastYear}-${month ?? lastMonth}`) if (!data) { set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/notFound`, { property }) } set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/property`, { lang, dateMap: { years, months }, type: `monthly`, property, data }) }) .get(`/:property/yearly`, ({ params: { property }, query: { year }, set }) => { const dateMap = Meteostanica.getDateMap() const years = Object.keys(dateMap) const lastYear = years[years.length - 1] const data = Meteostanica.getDataPropertyYearly(property, year ?? lastYear) if (!data) { set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/notFound`, { property }) } set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/property`, { lang, dateMap: { years }, type: `yearly`, property, data }) }) .get(`/:property/allTime`, ({ params: { property }, set }) => { const data = Meteostanica.getDataPropertyAllTime(property) const dateMap = Meteostanica.getDateMap() if (!data) { set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/notFound`, { property }) } set.headers['content-type'] = 'text/html; charset=utf8' return eta.render(`${langName}/history/property`, { lang, dateMap, type: `allTime`, property, data }) })