[Inicio] - Primeira parte que compila

This commit is contained in:
adnanioricce 2025-01-11 21:24:46 -03:00
parent d6df10b57d
commit 0c4b7e1743
2 changed files with 45 additions and 18 deletions

@ -44,36 +44,63 @@ let parseQuery (body: string) =
let fetchData (queryDsl: QueryDSL) = let fetchData (queryDsl: QueryDSL) =
// Mock database query (replace with actual DB logic) // Mock database query (replace with actual DB logic)
let mockData = [ let mockData = [
{| name = "Alice"; age = 30 |} Map.ofList [ "name", box "Alice"; "age", box 30 ]
{| name = "Bob"; age = 25 |} Map.ofList [ "name", box "Bob"; "age", box 25 ]
{| name = "Charlie"; age = 35 |} Map.ofList [ "name", box "Charlie"; "age", box 35 ]
] ]
// Filter and transform the data based on the DSL // Filter and transform the data based on the DSL
mockData mockData
|> List.filter (fun row -> |> List.filter (fun row ->
queryDsl.Filters |> List.forall (function queryDsl.Filters |> List.forall (function
| Equal(field, value) -> row.[field] = value | Equal(field, num) ->
| GreaterThan(field, num) -> row.[field] |> float > num Map.tryFind field row
| LessThan(field, num) -> row.[field] |> float < num)) |> 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 -> |> List.map (fun row ->
queryDsl.Select |> List.map (fun field -> field, row.[field]) |> dict) queryDsl.Select |> List.map (fun field -> field, row.[field]) |> dict)
|> fun result -> result |> fun result -> result
let queryHandler (next: HttpFunc) (ctx: HttpContext) = let queryHandler = fun (next: HttpFunc) (ctx: HttpContext) ->
task { task {
let! body = ctx.ReadBodyAsStringAsync() //let! body = ctx.ReadBodyAsStringAsync()
let queryDsl = parseQuery body let! body = ctx.ReadBodyBufferedFromRequestAsync()
return! parseQuery body
|> Result.map(fun queryDsl ->
let data = fetchData queryDsl let data = fetchData queryDsl
return! json data next ctx json data next ctx
)
|> Result.defaultWith (fun error -> json {|success = false;message = error|} next ctx)
} }
let webApp = let webApp () =
choose [ POST >=> route "query" >=> queryHandler
POST "/query" >=> queryHandler // [
RequestErrors.notFound "Not Found" //subRoute "/foo" [ GET [ route "/bar" (text "Aloha!") ] ]
] // POST
// route "query" queryHandler
// RequestErrors.notFound "Not Found"
// ]
let configureApp (app: IApplicationBuilder) = let configureApp (app: IApplicationBuilder) =
app.UseGiraffe webApp app.UseGiraffe queryHandler
let configureServices (services: IServiceCollection) = let configureServices (services: IServiceCollection) =
services.AddGiraffe() |> ignore services.AddGiraffe() |> ignore

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Giraffe" Version="6.*" /> <PackageReference Include="Giraffe" Version="6.*" />