ctucx.git: oeffisearch

[nimlang] fast and simple tripplanner

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 
import json, tables, options, asyncdispatch
import nimhafas
import ../types, ../cache

when not defined(release):
  import hotcodereloading

proc journeysEndpoint*(requestData: JsonNode): Future[JsonNode] {.async.} =
  when not defined(release):
    performCodeReload()

  var reqId     = requestData{"reqId"}.getStr()

  if reqId != "":
    if not cacheExists(reqId): raise newException(notFoundException, "REQUEST_NOT_FOUND")
    let cacheObj = getCacheObject(reqId)
    var response = %* {
          "reqId":       cacheObj.reqId,
          "lastUpdated": cacheObj.lastUpdated,
          "params":      cacheObj.params,
          "journeys":    {}
        }

    for key, journey in pairs(cacheObj.journeys):
      response["journeys"].add(key, %* journey)
      delete(response["journeys"][key], "refreshToken")
      delete(response["journeys"][key], "cycle")
      for legKey, leg in pairs(journey.legs):
        delete(response["journeys"][key]["legs"][legKey], "cycle")
        delete(response["journeys"][key]["legs"][legKey], "tripId")

    return response

  else:
    var params      = requestData["params"].to(JourneysParams)
    
    params.stopovers = some(true)
    params.polylines = some(false)
    params.remarks   = some(true)
    params.tickets   = some(true)

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

    var response = %* {
        "reqId":       cacheObj.reqId,
        "lastUpdated": cacheObj.lastUpdated,
        "params":      cacheObj.params,
        "journeys":    {}
      }

    for key, journey in pairs(cacheObj.journeys):
      response["journeys"].add(key, %* journey)
      delete(response["journeys"][key], "refreshToken")
      delete(response["journeys"][key], "cycle")
      for legKey, leg in pairs(journey.legs):
        delete(response["journeys"][key]["legs"][legKey], "cycle")
        delete(response["journeys"][key]["legs"][legKey], "tripId")

    return response