From d4671c044d8ff40a2398447bff9491847c9f3241 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 9 Aug 2019 17:00:09 -0500 Subject: [PATCH] Use FunctionalCuid for CUIDs --- src/MyPrayerJournal.Api/Domain.fs | 8 +------- src/MyPrayerJournal.Api/Handlers.fs | 18 +++++++++++------- .../MyPrayerJournal.Api.fsproj | 4 ++-- src/MyPrayerJournal.Api/Program.fs | 1 - 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/MyPrayerJournal.Api/Domain.fs b/src/MyPrayerJournal.Api/Domain.fs index 042f07a..c13431a 100644 --- a/src/MyPrayerJournal.Api/Domain.fs +++ b/src/MyPrayerJournal.Api/Domain.fs @@ -2,13 +2,7 @@ /// The data model for myPrayerJournal module MyPrayerJournal.Domain -/// A Collision-resistant Unique IDentifier -type Cuid = - | Cuid of string -module Cuid = - /// The string value of the CUID - let toString x = match x with Cuid y -> y - +open Cuid /// Request ID is a CUID type RequestId = diff --git a/src/MyPrayerJournal.Api/Handlers.fs b/src/MyPrayerJournal.Api/Handlers.fs index 41db8c2..8041a7c 100644 --- a/src/MyPrayerJournal.Api/Handlers.fs +++ b/src/MyPrayerJournal.Api/Handlers.fs @@ -2,10 +2,7 @@ [] module MyPrayerJournal.Handlers -open FSharp.Control.Tasks.V2.ContextInsensitive open Giraffe -open MyPrayerJournal -open System /// Handler to return Vue files module Vue = @@ -13,6 +10,7 @@ module Vue = /// The application index page let app : HttpHandler = htmlFile "wwwroot/index.html" +open System /// Handlers for error conditions module Error = @@ -34,6 +32,7 @@ module Error = | 0 -> (setStatusCode 404 >=> json ([ "error", "not found" ] |> dict)) next ctx | _ -> Vue.app next ctx +open Cuid /// Handler helpers [] @@ -60,7 +59,12 @@ module private Helpers = ((user >> Option.get) ctx).Value |> UserId /// 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 let created next ctx = @@ -136,6 +140,8 @@ module Models = until : int64 } +open FSharp.Control.Tasks.V2.ContextInsensitive + /// /api/journal URLs module Journal = @@ -155,8 +161,6 @@ module Journal = /// /api/request URLs module Request = - open NCuid - /// POST /api/request let add : HttpHandler = authorize @@ -164,7 +168,7 @@ module Request = task { let! r = ctx.BindJsonAsync () use sess = session ctx - let reqId = (Cuid.Generate >> toReqId) () + let reqId = (Cuid.generate >> RequestId) () let usrId = userId ctx let now = jsNow () do! Data.addRequest diff --git a/src/MyPrayerJournal.Api/MyPrayerJournal.Api.fsproj b/src/MyPrayerJournal.Api/MyPrayerJournal.Api.fsproj index 2063f1b..9ab996a 100644 --- a/src/MyPrayerJournal.Api/MyPrayerJournal.Api.fsproj +++ b/src/MyPrayerJournal.Api/MyPrayerJournal.Api.fsproj @@ -13,18 +13,18 @@ + - - + diff --git a/src/MyPrayerJournal.Api/Program.fs b/src/MyPrayerJournal.Api/Program.fs index 5967783..7a6728c 100644 --- a/src/MyPrayerJournal.Api/Program.fs +++ b/src/MyPrayerJournal.Api/Program.fs @@ -40,7 +40,6 @@ module Configure = open Giraffe.TokenRouter open Microsoft.AspNetCore.Authentication.JwtBearer open Microsoft.Extensions.DependencyInjection - open MyPrayerJournal open MyPrayerJournal.Indexes open Newtonsoft.Json open Newtonsoft.Json.Serialization