From d5635ef37c20c39ff925e3000f99911bd003b8fe Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sun, 30 Apr 2017 21:16:45 -0500 Subject: [PATCH] Added request list by user Input for what will be the main journal view --- src/App.fs | 26 +++++++++++++++++++------- src/Data.fs | 21 +++++++++++++++++++++ src/Views.fs | 2 +- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/App.fs b/src/App.fs index 17adbd4..1e80967 100644 --- a/src/App.fs +++ b/src/App.fs @@ -19,7 +19,7 @@ open Suave.Successful let utf8 = System.Text.Encoding.UTF8 -type JsonNetCookieSerializer() = +type JsonNetCookieSerializer () = interface CookieSerialiser with member x.serialise m = utf8.GetBytes (JsonConvert.SerializeObject m) @@ -147,22 +147,34 @@ module Auth = /// Add the logged on user Id to the context if it exists let loggedOn = warbler (fun ctx -> - match ctx |> HttpContext.state with + match HttpContext.state ctx with | Some state -> Writers.setUserData "user" (state.get "auth-token" |> getIdFromToken) | _ -> Writers.setUserData "user" None) /// Create a user context for the currently assigned user let userCtx ctx = { Id = ctx.userState.["user"] :?> string option } + +/// Read an item from the user state, downcast to the expected type +let read ctx key : 'value = + ctx.userState |> Map.tryFind key |> Option.map (fun x -> x :?> 'value) |> Option.get /// Create a new data context let dataCtx () = new DataContext (((DbContextOptionsBuilder()).UseNpgsql cfg.Conn).Options) +/// Return an HTML page +let html ctx content = + Views.page (Auth.userCtx ctx) content + /// Home page -let viewHome = warbler (fun ctx -> OK (Views.page (Auth.userCtx ctx) Views.home)) +let viewHome = warbler (fun ctx -> OK (Views.home |> html ctx)) /// Journal page -let viewJournal = warbler (fun ctx -> OK (Views.page (Auth.userCtx ctx) Views.journal)) +let viewJournal = + context (fun ctx -> + use dataCtx = dataCtx () + let reqs = Data.Requests.allForUser (defaultArg (read ctx "user") "") dataCtx + OK (Views.journal reqs |> html ctx)) /// Suave application let app = @@ -180,16 +192,16 @@ let app = /// Ensure the EF context is created in the right format let ensureDatabase () = async { - use data = dataCtx() + use data = dataCtx () do! data.Database.MigrateAsync () - } + } |> Async.RunSynchronously let suaveCfg = { defaultConfig with homeFolder = Some (Path.GetFullPath "./wwwroot/") serverKey = Text.Encoding.UTF8.GetBytes("12345678901234567890123456789012") - cookieSerialiser = new JsonNetCookieSerializer() + cookieSerialiser = JsonNetCookieSerializer () } [] diff --git a/src/Data.fs b/src/Data.fs index 96281ac..ce9e79c 100644 --- a/src/Data.fs +++ b/src/Data.fs @@ -1,6 +1,7 @@ namespace MyPrayerJournal open Microsoft.EntityFrameworkCore +open System.Linq open System.Runtime.CompilerServices /// Data context for myPrayerJournal @@ -34,3 +35,23 @@ type DataContext = |> Request.ConfigureEF |> History.ConfigureEF |> ignore + +/// Data access +module Data = + + /// Data access for prayer requests + module Requests = + + /// Get all prayer requests for a user + let allForUser userId (ctx : DataContext) = + query { + for req in ctx.Requests do + where (req.UserId = userId) + select req + } + |> Seq.sortBy + (fun req -> + match req.History |> Seq.sortBy (fun hist -> hist.AsOf) |> Seq.tryLast with + | Some hist -> hist.AsOf + | _ -> 0L) + |> List.ofSeq diff --git a/src/Views.fs b/src/Views.fs index c91a43a..84c5eea 100644 --- a/src/Views.fs +++ b/src/Views.fs @@ -107,7 +107,7 @@ let home = p [ text "This site is currently in very limited alpha, as it is being developed with a core group of test users. If this is something you are interested in using, check back around mid-February 2017 to check on the development progress." ] ] -let journal = +let journal (reqs : Request list) = fullRow [ p [ text "journal goes here" ] ]