ctucx.git: oeffi-web

[nimlang] oeffisearch fork that works without javascript

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
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())