diff --git a/Program.fs b/Program.fs
index a79323e..774dd9f 100644
--- a/Program.fs
+++ b/Program.fs
@@ -44,36 +44,63 @@ let parseQuery (body: string) =
let fetchData (queryDsl: QueryDSL) =
// Mock database query (replace with actual DB logic)
let mockData = [
- {| name = "Alice"; age = 30 |}
- {| name = "Bob"; age = 25 |}
- {| name = "Charlie"; age = 35 |}
+ Map.ofList [ "name", box "Alice"; "age", box 30 ]
+ Map.ofList [ "name", box "Bob"; "age", box 25 ]
+ Map.ofList [ "name", box "Charlie"; "age", box 35 ]
]
// Filter and transform the data based on the DSL
mockData
|> List.filter (fun row ->
queryDsl.Filters |> List.forall (function
- | Equal(field, value) -> row.[field] = value
- | GreaterThan(field, num) -> row.[field] |> float > num
- | LessThan(field, num) -> row.[field] |> float < num))
+ | Equal(field, num) ->
+ Map.tryFind field row
+ |> Option.map(fun value -> (string value) = num)
+ |> Option.defaultValue false
+ //row.GetType().GetProperty(field).GetValue(row) = value
+ //| GreaterThan(field, num) -> row.[field] |> float > num
+ | GreaterThan(field, num) ->
+ Map.tryFind field row
+ |> Option.bind (fun value ->
+ match value with
+ | :? float as v -> Some (v > num)
+ | :? int as v -> Some (float v > num)
+ | _ -> None)
+ |> Option.defaultValue false
+ | LessThan(field, num) ->
+ Map.tryFind field row
+ |> Option.bind (fun value ->
+ match value with
+ | :? float as v -> Some (v < num)
+ | :? int as v -> Some (float v < num)
+ | _ -> None)
+ |> Option.defaultValue false))
|> List.map (fun row ->
queryDsl.Select |> List.map (fun field -> field, row.[field]) |> dict)
|> fun result -> result
-let queryHandler (next: HttpFunc) (ctx: HttpContext) =
+let queryHandler = fun (next: HttpFunc) (ctx: HttpContext) ->
task {
- let! body = ctx.ReadBodyAsStringAsync()
- let queryDsl = parseQuery body
- let data = fetchData queryDsl
- return! json data next ctx
+ //let! body = ctx.ReadBodyAsStringAsync()
+ let! body = ctx.ReadBodyBufferedFromRequestAsync()
+ return! parseQuery body
+ |> Result.map(fun queryDsl ->
+ let data = fetchData queryDsl
+ json data next ctx
+ )
+ |> Result.defaultWith (fun error -> json {|success = false;message = error|} next ctx)
}
-let webApp =
- choose [
- POST "/query" >=> queryHandler
- RequestErrors.notFound "Not Found"
- ]
+let webApp () =
+ POST >=> route "query" >=> queryHandler
+ // [
+ //subRoute "/foo" [ GET [ route "/bar" (text "Aloha!") ] ]
+ // POST
+ // route "query" queryHandler
+ // RequestErrors.notFound "Not Found"
+
+ // ]
let configureApp (app: IApplicationBuilder) =
- app.UseGiraffe webApp
+ app.UseGiraffe queryHandler
let configureServices (services: IServiceCollection) =
services.AddGiraffe() |> ignore
diff --git a/ReportApi.fsproj b/ReportApi.fsproj
index 1dcb2f6..912f842 100644
--- a/ReportApi.fsproj
+++ b/ReportApi.fsproj
@@ -2,7 +2,7 @@
Exe
- net8.0
+ net9.0