Last active
August 29, 2015 13:59
-
-
Save GeertVL-zz/10472354 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The SqlEntityConnection (Entity Data Model) TypeProvider allows you to write code that uses | |
// a live connection to a database that is represented by the Entity Data Model. For more information, | |
// please go to | |
// http://go.microsoft.com/fwlink/?LinkId=229210 | |
module GeertVLConnection | |
#if INTERACTIVE | |
#r "System.Data" | |
#r "System.Data.Entity" | |
#r "FSharp.Data.TypeProviders" | |
#endif | |
open System.Data | |
open System.Data.Entity | |
open Microsoft.FSharp.Data.TypeProviders | |
open System.ComponentModel.DataAnnotations | |
// You can use Server Explorer to build your ConnectionString. | |
[<Literal>] | |
let conn = """Data Source=MC-FPBJSY1\SQLEXPRESS;Initial Catalog=geertvl;Integrated Security=True;""" | |
type internal SqlConnection = | |
Microsoft.FSharp.Data.TypeProviders.SqlEntityConnection<ConnectionString = conn> | |
let internal db = SqlConnection.GetDataContext() | |
type Course() = | |
[<Key>] member val Id = 0 with get, set | |
[<Required>] member val Name = "" with get, set | |
let GetAllCourses = | |
use context = SqlConnection.GetDataContext() | |
query { | |
for g in context.Courses do | |
select (Course(Id = g.CourseID, Name = g.CourseName)) | |
} |> Seq.toList | |
let GetCourse id = | |
use context = SqlConnection.GetDataContext() | |
query { | |
for g in context.Courses do | |
where (g.CourseID = id) | |
select (Course(Id = g.CourseID, Name = g.CourseName)) | |
} |> Seq.toList | |
type CourseRepository() = | |
member x.GetAll() = | |
use context = SqlConnection.GetDataContext() | |
query { | |
for g in context.Courses do | |
select (Course(Id = g.CourseID, Name = g.CourseName)) | |
} |> Seq.toList | |
member x.GetById id = | |
use context = SqlConnection.GetDataContext() | |
query { | |
for g in context.Courses do | |
where (g.CourseID = id) | |
select (Course(Id = g.CourseID, Name = g.CourseName)) | |
} |> Seq.toList | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open Nancy | |
open Nancy.Hosting.Self | |
open GeertVLConnection | |
open Newtonsoft.Json | |
let GetNancyParam prms key = ((prms :> DynamicDictionary).[key]).ToString() | |
type HelloModule() = | |
inherit NancyModule() | |
let changeName v = | |
let repo = CourseRepository() | |
let res = repo.GetAll() | |
let json = JsonConvert.SerializeObject(res) | |
json |> sprintf "Dit is een naam %A" | |
let jsonify v = | |
let response = v |> JsonConvert.SerializeObject |> Response.op_Implicit | |
response.ContentType <- "application/json; charset=utf-8" | |
response :> obj | |
let getName (v:Course list) = | |
(List.head v).Name |> sprintf "Dit is de naam %A" | |
do base.Get.["/"] <- | |
fun _ -> "Hello" :> obj | |
do base.Get.["/test"] <- | |
fun _ -> "Testing" :> obj | |
do base.Get.["/test/{name}"] <- | |
fun prms -> GetNancyParam (unbox prms) "name" |> changeName :> obj | |
do base.Get.["/courses/{id:int}"] <- | |
fun prms -> int (GetNancyParam (unbox prms) "id") |> GetCourse |> jsonify | |
do base.Get.["/courses"] <- | |
fun prms -> GetAllCourses |> jsonify | |
do base.Get.["/courses/{id:int}/name"] <- | |
fun prms -> int (GetNancyParam (unbox prms) "id") |> GetCourse |> getName :> obj | |
[<EntryPoint>] | |
let main argv = | |
let nancyHost = new NancyHost(new Uri("http://localhost:8888/nancy/"), | |
new Uri("http://127.0.0.1:8888/nancy/")) | |
nancyHost.Start() | |
printfn "ready..." | |
Console.ReadKey() |> ignore | |
nancyHost.Stop() | |
0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment