Commits

Kakadu committed 47b6f37

Getting questions

Comments (0)

Files changed (2)

   print_endline ans;
   Lwt.return true
 
+
+let interpret_of_json ej : Types.Option.t =
+  let id = id_from_node_json ej in
+  let data = List.Assoc.find_exn ej "data" |> YoUtil.drop_assoc in
+  let title = List.Assoc.(find_exn data "text") |> YoUtil.drop_string in
+  Types.Option.(id,title)
+
+let question_of_json options ej : Types.Question.t =
+  let id = id_from_node_json ej in
+  let data = List.Assoc.find_exn ej "data" |> YoUtil.drop_assoc in
+  let text = List.Assoc.(find_exn data "text") |> YoUtil.drop_string in
+  Types.Question.({id; options; text})
+
 let questions_with_options_of_nodeId nodeId =
   let cmd = sprintf "Start e=node(%Ld)
                      MATCH (a)<-[:HAS_ANSWER*0..]-q<-[:HAS_QUESTION]-e
                      RETURN q,collect(a)" nodeId
   in
   let ans = post_cypher cmd  in
-  print_endline ans;
+  (*print_endline ans;*)
   match ans |> to_json |> YoUtil.unwrap_res with
-  | _ ->
-  Lwt.return []
+  | `List xs -> begin
+      Lwt.return @@ List.map xs ~f:(fun x ->
+          (*print_json x;*)
+          match x with
+          | `List [`Assoc qj; `List ajs ] ->
+            let interprets = List.map ajs ~f:(function | `Assoc x -> interpret_of_json x
+                                                       | _ -> failwith "Wrong JSON") in
+            question_of_json interprets qj
+          | _ -> failwith "Wrong JSON format"
+        )
+    end
+  | _ -> failwith "Wrong JSON format in questions_with_options_of_nodeId"
 
 let http_get  = Http_client.Convenience.http_get
 let http_post = Http_client.Convenience.http_post
 let to_json = Yojson.Safe.from_string
+let print_json = Yojson.Safe.pretty_to_channel stdout
 
 module YoUtil = struct
   let drop_assoc = function `Assoc xs -> xs | _ -> failwith "Bad argument"
   | `List[`List[`Assoc xs]] -> `OK (new node_of_json xs)
   | _ -> `Error "JSON match failure"
 
+let id_from_node_json ej =
+  match List.Assoc.find_exn ej "self" with
+  | `String s -> Int64.of_string @@ String.rsplit s ~by:'/'
+  | _ -> failwith "Wrong json for function id_from_node_json"