From d153a29b7e529fa7518f9240c046cfd297983451 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 7 Jan 2022 16:05:00 -0500 Subject: [PATCH] WIP on v3.1 changes - Merge recurrence and recur count - Separate type mappings --- src/MyPrayerJournal/Data.fs | 33 ++++++++++++++++++++++++++++++-- src/MyPrayerJournal/Domain.fs | 36 ++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/MyPrayerJournal/Data.fs b/src/MyPrayerJournal/Data.fs index 5f623db..f2a9a58 100644 --- a/src/MyPrayerJournal/Data.fs +++ b/src/MyPrayerJournal/Data.fs @@ -27,6 +27,31 @@ module Extensions = [] module Mapping = + /// Mapping for NodaTime's Instant type + module Instant = + let fromBson (value : BsonValue) = Instant.FromUnixTimeMilliseconds value.AsInt64 + let toBson (value : Instant) : BsonValue = value.ToUnixTimeMilliseconds () + + /// Mapping for option types + module Option = + let stringFromBson (value : BsonValue) = match value.AsString with "" -> None | x -> Some x + let stringToBson (value : string option) : BsonValue = match value with Some txt -> txt | None -> "" + + /// Mapping for RequestAction + module RequestAction = + let fromBson (value : BsonValue) = RequestAction.ofString value.AsString + let toBson (value : RequestAction) : BsonValue = RequestAction.toString value + + /// Mapping for RequestId + module RequestId = + let fromBson (value : BsonValue) = RequestId.ofString value.AsString + let toBson (value : RequestId) : BsonValue = RequestId.toString value + + /// Mapping for UserId + module UserId = + let fromBson (value : BsonValue) = UserId value.AsString + let toBson (value : UserId) : BsonValue = UserId.toString value + /// Map a history entry to BSON let historyToBson (hist : History) : BsonValue = let doc = BsonDocument () @@ -84,8 +109,12 @@ module Mapping = /// Set up the mapping let register () = - BsonMapper.Global.RegisterType( - Func requestToBson, Func requestFromBson) + BsonMapper.Global.RegisterType(requestToBson, requestFromBson) + BsonMapper.Global.RegisterType(Instant.toBson, Instant.fromBson) + BsonMapper.Global.RegisterType(RequestAction.toBson, RequestAction.fromBson) + BsonMapper.Global.RegisterType(RequestId.toBson, RequestId.fromBson) + BsonMapper.Global.RegisterType(Option.stringToBson, Option.stringFromBson) + BsonMapper.Global.RegisterType(UserId.toBson, UserId.fromBson) /// Code to be run at startup module Startup = diff --git a/src/MyPrayerJournal/Domain.fs b/src/MyPrayerJournal/Domain.fs index cd64800..eaed55a 100644 --- a/src/MyPrayerJournal/Domain.fs +++ b/src/MyPrayerJournal/Domain.fs @@ -4,6 +4,7 @@ module MyPrayerJournal.Domain // fsharplint:disable RecordFieldNames +open System open Cuid open NodaTime @@ -32,9 +33,9 @@ module UserId = /// How frequently a request should reappear after it is marked "Prayed" type Recurrence = | Immediate - | Hours - | Days - | Weeks + | Hours of int16 + | Days of int16 + | Weeks of int16 /// Functions to manipulate recurrences module Recurrence = @@ -42,26 +43,31 @@ module Recurrence = let toString = function | Immediate -> "Immediate" - | Hours -> "Hours" - | Days -> "Days" - | Weeks -> "Weeks" + | Hours h -> $"{h} Hours" + | Days d -> $"{d} Days" + | Weeks w -> $"{w} Weeks" /// Create a recurrence value from a string let ofString = function | "Immediate" -> Immediate - | "Hours" -> Hours - | "Days" -> Days - | "Weeks" -> Weeks - | it -> invalidOp $"{it} is not a valid recurrence" + | it when it.Contains " " -> + let parts = it.Split " " + let length = Convert.ToInt16 parts[0] + match parts[1] with + | "Hours" -> Hours length + | "Days" -> Days length + | "Weeks" -> Weeks length + | _ -> invalidOp $"{parts[1]} is not a valid recurrence" + | it -> invalidOp $"{it} is not a valid recurrence" /// An hour's worth of seconds let private oneHour = 3_600L /// The duration of the recurrence (in milliseconds) - let duration x = - (match x with + let duration = + function | Immediate -> 0L - | Hours -> oneHour - | Days -> oneHour * 24L - | Weeks -> oneHour * 24L * 7L) + | Hours h -> int64 h * oneHour + | Days d -> int64 d * oneHour * 24L + | Weeks w -> int64 w * oneHour * 24L * 7L /// The action taken on a request as part of a history entry