Use FunctionalCuid for CUIDs

This commit is contained in:
Daniel J. Summers 2019-08-09 17:00:09 -05:00
parent bf489cc4a8
commit d4671c044d
4 changed files with 14 additions and 17 deletions

View File

@ -2,13 +2,7 @@
/// The data model for myPrayerJournal /// The data model for myPrayerJournal
module MyPrayerJournal.Domain module MyPrayerJournal.Domain
/// A Collision-resistant Unique IDentifier open Cuid
type Cuid =
| Cuid of string
module Cuid =
/// The string value of the CUID
let toString x = match x with Cuid y -> y
/// Request ID is a CUID /// Request ID is a CUID
type RequestId = type RequestId =

View File

@ -2,10 +2,7 @@
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
module MyPrayerJournal.Handlers module MyPrayerJournal.Handlers
open FSharp.Control.Tasks.V2.ContextInsensitive
open Giraffe open Giraffe
open MyPrayerJournal
open System
/// Handler to return Vue files /// Handler to return Vue files
module Vue = module Vue =
@ -13,6 +10,7 @@ module Vue =
/// The application index page /// The application index page
let app : HttpHandler = htmlFile "wwwroot/index.html" let app : HttpHandler = htmlFile "wwwroot/index.html"
open System
/// Handlers for error conditions /// Handlers for error conditions
module Error = module Error =
@ -34,6 +32,7 @@ module Error =
| 0 -> (setStatusCode 404 >=> json ([ "error", "not found" ] |> dict)) next ctx | 0 -> (setStatusCode 404 >=> json ([ "error", "not found" ] |> dict)) next ctx
| _ -> Vue.app next ctx | _ -> Vue.app next ctx
open Cuid
/// Handler helpers /// Handler helpers
[<AutoOpen>] [<AutoOpen>]
@ -60,7 +59,12 @@ module private Helpers =
((user >> Option.get) ctx).Value |> UserId ((user >> Option.get) ctx).Value |> UserId
/// Create a request ID from a string /// Create a request ID from a string
let toReqId = Cuid >> RequestId let toReqId x =
let reqId =
match Cuid.ofString x with
| Ok cuid -> cuid
| Error msg -> invalidOp msg
RequestId reqId
/// Return a 201 CREATED response /// Return a 201 CREATED response
let created next ctx = let created next ctx =
@ -136,6 +140,8 @@ module Models =
until : int64 until : int64
} }
open FSharp.Control.Tasks.V2.ContextInsensitive
/// /api/journal URLs /// /api/journal URLs
module Journal = module Journal =
@ -155,8 +161,6 @@ module Journal =
/// /api/request URLs /// /api/request URLs
module Request = module Request =
open NCuid
/// POST /api/request /// POST /api/request
let add : HttpHandler = let add : HttpHandler =
authorize authorize
@ -164,7 +168,7 @@ module Request =
task { task {
let! r = ctx.BindJsonAsync<Models.Request> () let! r = ctx.BindJsonAsync<Models.Request> ()
use sess = session ctx use sess = session ctx
let reqId = (Cuid.Generate >> toReqId) () let reqId = (Cuid.generate >> RequestId) ()
let usrId = userId ctx let usrId = userId ctx
let now = jsNow () let now = jsNow ()
do! Data.addRequest do! Data.addRequest

View File

@ -13,18 +13,18 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FunctionalCuid" Version="1.0.0" />
<PackageReference Include="Giraffe" Version="3.6.0" /> <PackageReference Include="Giraffe" Version="3.6.0" />
<PackageReference Include="Giraffe.TokenRouter" Version="1.0.0" /> <PackageReference Include="Giraffe.TokenRouter" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.FSharpLu" Version="0.10.29" /> <PackageReference Include="Microsoft.FSharpLu" Version="0.10.29" />
<PackageReference Include="Microsoft.FSharpLu.Json" Version="0.10.29" /> <PackageReference Include="Microsoft.FSharpLu.Json" Version="0.10.29" />
<PackageReference Include="NCuid.NetCore" Version="1.0.1" />
<PackageReference Include="RavenDb.Client" Version="4.2.1" /> <PackageReference Include="RavenDb.Client" Version="4.2.1" />
<PackageReference Include="TaskBuilder.fs" Version="2.1.0" /> <PackageReference Include="TaskBuilder.fs" Version="2.1.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.6.2" /> <PackageReference Update="FSharp.Core" Version="4.7.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -40,7 +40,6 @@ module Configure =
open Giraffe.TokenRouter open Giraffe.TokenRouter
open Microsoft.AspNetCore.Authentication.JwtBearer open Microsoft.AspNetCore.Authentication.JwtBearer
open Microsoft.Extensions.DependencyInjection open Microsoft.Extensions.DependencyInjection
open MyPrayerJournal
open MyPrayerJournal.Indexes open MyPrayerJournal.Indexes
open Newtonsoft.Json open Newtonsoft.Json
open Newtonsoft.Json.Serialization open Newtonsoft.Json.Serialization