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

proc moreJourneysEndpoint*(requestData: JsonNode): Future[JsonNode] {.async.} =
  var reqId = requestData{"reqId"}.getStr()
  var mode  = later

  if reqId != "":
    if not cacheExists(reqId): raise newException(notFoundException, "REQUEST_NOT_FOUND")

    if requestData{"mode"}.getStr() != "":
      mode = parseEnum[moreJourneysMode](requestData{"mode"}.getStr())

    var cacheObj = getCacheObject(reqId)
    var params   = cacheObj.params

    if mode != later:
      params.earlierRef = some(cacheObj.earlierRef)
    else:
      params.laterRef   = some(cacheObj.laterRef)

    let journeys = await journeys(params)
    cacheObj = (await updateJourneys(reqId, mode, journeys))

    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:
    raise newException(errorException, "MISSING_VALUES")