2020-01-02 12:24:00 +01:00
module Db
open FSharp.Data.Sql
[<Literal>]
let ConnectionString = "Server=127.0.0.1; Database=todo_api_db; User Id=suave; Password=1234;"
type Sql = SqlDataProvider<ConnectionString=ConnectionString, DatabaseVendor=Common.DatabaseProviderTypes.POSTGRESQL, CaseSensitivityChange=Common.CaseSensitivityChange.ORIGINAL>
2020-01-02 12:48:35 +01:00
module Context =
type DbContext = Sql.dataContext
2020-01-02 12:24:00 +01:00
2020-01-02 12:48:35 +01:00
let getContext() = Sql.GetDataContext()
module Types =
open Context
2020-01-02 13:24:11 +01:00
open System.Runtime.Serialization
type DbTodo = DbContext.``public.todosEntity``
[<DataContract>]
type Todo =
{ [<field:DataMember(Name = "id")>]
id: int
[<field:DataMember(Name = "description")>]
description: string
[<field:DataMember(Name = "name")>]
name: string }
2020-01-02 14:37:47 +01:00
[<DataContract>]
type TodoDetails =
{ [<field:DataMember(Name = "description")>]
description: string
[<field:DataMember(Name = "name")>]
name: string }
2020-01-02 12:48:35 +01:00
module Queries =
open Context
open Types
2020-01-02 14:37:47 +01:00
let getTodosById id (ctx: DbContext): DbTodo option =
2020-01-02 12:48:35 +01:00
query {
for todo in ctx.Public.Todos do
where (todo.Id = id)
select todo
}
|> Seq.tryHead
2020-01-02 14:37:47 +01:00
let updateTodosById (todo: DbTodo) (details: TodoDetails) (ctx: DbContext) =
todo.Name <- details.name
todo.Description <- details.description
ctx.SubmitUpdatesAsync()
2020-01-02 12:48:35 +01:00