import json, options, strutils, tables import nimhafas import ../asyncWebServer import ../utils, ../translations, ../types, ../cache_types, ../cache import ../formaters/renderJourney proc journeyHandler* (request: Request, server: ServerRef) {.async.} = let urlParts = request.url.path.split("/") let requestParams = parseQuery(request.url.query) var headers = newHttpHeaders() var lang = "de" var cacheObj: CacheObject try: languageHandler() if not cacheExists(urlParts[1]): raise newException(notFoundException, "REQUEST_NOT_FOUND") cacheObj = getCacheObject(urlParts[1]) if not cacheObj.journeys.hasKey(urlParts[2]): raise newException(notFoundException, "JOURNEY_NOT_FOUND") if requestParams.hasKey("refresh"): let journey = await refreshJourney(RefreshJourneyParams( refreshToken: cacheObj.journeys[urlParts[2]].refreshToken, stopovers: some(true) )) cacheObj = await updateJourney(urlParts[1], urlParts[2], journey) var templateContext = renderJourney(cacheObj, urlParts[2]) if not requestParams.hasKey("json"): await request.respTemplate(Http200, "journey", templateContext, lang, headers) else: await request.respondJson(Http200, "success", "", templateContext) except notFoundException: if not requestParams.hasKey("json"): await request.respError(Http404, "Nichts gefunden!", "Für diese Anfrage gibts nix.") else: await request.respondJson(Http404, "error", getCurrentExceptionMsg().split("\n")[0], newJObject())