[Inicio] - Primeira parte que compila
This commit is contained in:
parent
d6df10b57d
commit
0c4b7e1743
61
Program.fs
61
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
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Giraffe" Version="6.*" />
|
||||
|
Loading…
Reference in New Issue
Block a user