ctucx.git: oeffi-web

[nimlang] oeffisearch fork that works without javascript

commit 0ba11d3188b92a7521e3bce182925ef967ea1a03
parent 15b9bd9aebc09456b2d02906b29275a879a9dd84
Author: Leah Thein <leah@toaster.fritz.box>
Date: Fri, 4 Dec 2020 17:42:22 +0100

misc
4 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/src/endpoints/searchHandler.nim b/src/endpoints/searchHandler.nim
@@ -26,9 +26,9 @@ proc searchHandler* (request: Request, server: ServerRef) {.async.} =
     try: 
       var products = %* {}
       parseProduct(products, requestParams, "national")
-      parseProduct(products, requestParams, "national", "nationalExp")
+      parseProduct(products, requestParams, "nationalExp")
       parseProduct(products, requestParams, "regional")
-      parseProduct(products, requestParams, "regional", "regionalExp")
+      parseProduct(products, requestParams, "regionalExp")
       parseProduct(products, requestParams, "suburban")
       parseProduct(products, requestParams, "subway")
       parseProduct(products, requestParams, "tram")

@@ -71,6 +71,7 @@ proc searchHandler* (request: Request, server: ServerRef) {.async.} =
       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.")
diff --git a/src/endpoints/suggestionsHandler.nim b/src/endpoints/suggestionsHandler.nim
@@ -0,0 +1,24 @@
+import json, options, strutils
+import ../asyncWebServer
+import ../types, ../backend/hafas
+import ../formaters/formaters
+
+proc suggestionsHandler* (request: Request, server: ServerRef) {.async.} = 
+  let requestParams = parseQuery(request.url.query)
+  var results       = 10
+  var suggestions:    seq[string]
+
+  if requestParams.hasKey("results"):
+    results = parseInt(requestParams["results"])
+
+  if requestParams.hasKey("query"):
+    let points = (await suggestions(SuggestionsParams(
+        query: requestParams["query"],
+        results: some(results)
+      )))
+
+    for point in points:
+      suggestions.add(formatPoint(point))
+
+  var json = %* {"suggestions": suggestions}
+  await request.respondJson(Http200, "success", "", json["suggestions"])
diff --git a/src/formaters/renderJourney.nim b/src/formaters/renderJourney.nim
@@ -60,11 +60,11 @@ proc renderJourney*(data: CacheObject, id: string): JsonNode =
         })
 
     else:
+      var arrivalTime:   int64
+      var departureTime: int64
+      var duration:      int64
 
       if lastArrival.plannedTime != 0:
-        var arrivalTime:   int64
-        var departureTime: int64
-
         if not leg.departure.prognosedTime.isNone: 
           departureTime  = leg.departure.prognosedTime.get
         else:

@@ -73,15 +73,28 @@ proc renderJourney*(data: CacheObject, id: string): JsonNode =
         if not lastArrival.prognosedTime.isNone: 
           arrivalTime  = lastArrival.prognosedTime.get
         else:
-          arrivalTime = lastArrival.plannedTime
+          arrivalTime  = lastArrival.plannedTime
 
-        let duration = departureTime - arrivalTime
+        duration = departureTime - arrivalTime
 
         result["LEGS"].add(%* {
             "IS_CHANGE": true,
             "DURATION":  formatDuration(duration)
           })
 
+      if not leg.departure.prognosedTime.isNone: 
+        departureTime  = leg.departure.prognosedTime.get
+      else:
+        departureTime  = leg.departure.plannedTime
+
+      if not leg.arrival.prognosedTime.isNone: 
+        arrivalTime  = leg.arrival.prognosedTime.get
+      else:
+        arrivalTime  = leg.arrival.plannedTime
+
+      duration = departureTime - arrivalTime
+
+
       lastArrival = leg.arrival
       inc(changes)
 

@@ -194,6 +207,7 @@ proc renderJourney*(data: CacheObject, id: string): JsonNode =
           "IS_CANCELLED":    leg.cancelled,
           "LINE":            line,
           "DIRECTION":       leg.direction.get,
+          "LEG_DURATION":    %formatDuration(duration),
           "ARRIVAL_POINT":   formatPoint(leg.arrival.point),
           "DEPARTURE_POINT": formatPoint(leg.departure.point),
           "STOPOVERS":       stopovers,
diff --git a/src/translations.nim b/src/translations.nim
@@ -8,6 +8,8 @@ let translations* = %* {
         "LANG":      "en",
         "FROM":      "From",
         "TO":        "To",
+        "VIA":       "Via",
+        "OPTIONAL":  "optional",
         "PRODUCTS":  "Products",
         "DEPARTURE": "Departure",
         "ARRIVAL":   "Arrival",

@@ -19,6 +21,8 @@ let translations* = %* {
         "LANG":      "de",
         "FROM":      "Von",
         "TO":        "Nach",
+        "VIA":       "Via",
+        "OPTIONAL":  "optional",
         "PRODUCTS":  "Verkehrsmittel",
         "DEPARTURE": "Abfahrt",
         "ARRIVAL":   "Ankunft",