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 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
import json, times, options, strutils
import nimhafas
import ../asyncWebServer
import ../utils, ../translations, ../types, ../cache_types, ../cache
import ../formaters/formaters, ../formaters/renderJourneys

proc searchHandler* (request: Request, server: ServerRef) {.async.} = 
  if request.reqMethod != HttpPost:
    let requestParams = parseQuery(request.url.query)
    var headers       = newHttpHeaders()
    var lang          = "de"

    languageHandler()

    var templateContext = %* {
        "DATE": now().format("yyyy-MM-dd"),
        "TIME": now().format("HH:mm"),
        "FROM": if requestParams.contains("from"): requestParams["from"] else: "",
        "TO":   if requestParams.contains("to"): requestParams["to"] else: ""
      }

    await request.respTemplate(Http200, "search", templateContext, lang, headers)

  else:
    let requestParams = parseQuery(request.body)
    var cacheObj:       CacheObject
    var params:         JourneysParams

    try: 
      var products = %* {}
      parseProduct(products, requestParams, "national")
      parseProduct(products, requestParams, "nationalExp")
      parseProduct(products, requestParams, "regional")
      parseProduct(products, requestParams, "regionalExp")
      parseProduct(products, requestParams, "suburban")
      parseProduct(products, requestParams, "subway")
      parseProduct(products, requestParams, "tram")
      parseProduct(products, requestParams, "bus")
      parseProduct(products, requestParams, "ferry")
      parseProduct(products, requestParams, "taxi")

      params  = JourneysParams(
          fromPoint: (await getPoint(requestParams["from"])),
          toPoint:   (await getPoint(requestParams["to"])),
        )

      params.stopovers     = some(true)
      params.polylines     = some(false)
      params.remarks       = some(true)
      params.tickets       = some(true)
      params.accessibility = some(parseEnum[Accessibility](requestParams["accessibility"]))
      params.products      = some(products.to(Products))

      if requestParams.hasKey("isarr"):
        params.arrival   = some(parseTime(requestParams["date"] & " " & requestParams["time"], "yyyy-MM-dd HH:mm", local()).toUnix)
      else:
        params.departure = some(parseTime(requestParams["date"] & " " & requestParams["time"], "yyyy-MM-dd HH:mm", local()).toUnix)

      if formatPoint(params.fromPoint) == formatPoint(params.toPoint):
        raise newException(errorException, "TOO_CLOSE")

      cacheObj = (await saveJourneys(params, await journeys(params)))

      if cacheObj.reqId != "":
        if not requestParams.hasKey("json"):
          await request.redirect("/" & cacheObj.reqId)
        else:
          await request.respondJson(Http200, "success", "", renderJourneys(cacheObj))
      else:
        raise newException(errorException, "NO_REQID")

    except:
      echo getCurrentExceptionMsg()
      let exceptionMsg = getCurrentExceptionMsg().split("\n")[0]

      if not requestParams.hasKey("json"):
        echo exceptionMsg
        case exceptionMsg:
          of "TOO_CLOSE":
            await request.respError(Http500, "Fehler!", "Abfahrts- und Ankunftsbahnhof dürfen nicht gleich sein.")
          else:
            await request.respError(Http500, "Unbekannter Fehler!", "Das hätte nicht passieren dürfen, versuche es doch bitte erneut.")
      else:
        case exceptionMsg:
          of "TOO_CLOSE":
            await request.respondJson(Http500, "error", "TOO_CLOSE", newJObject())
          else:
            await request.respondJson(Http500, "error", "UNKNOWN_ERROR", newJObject())