Version 3.1 #71

Merged
danieljsummers merged 9 commits from v3.1 into main 2022-07-30 21:02:58 +00:00
2 changed files with 52 additions and 17 deletions
Showing only changes of commit d153a29b7e - Show all commits

View File

@ -27,6 +27,31 @@ module Extensions =
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
module Mapping = 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 /// Map a history entry to BSON
let historyToBson (hist : History) : BsonValue = let historyToBson (hist : History) : BsonValue =
let doc = BsonDocument () let doc = BsonDocument ()
@ -84,8 +109,12 @@ module Mapping =
/// Set up the mapping /// Set up the mapping
let register () = let register () =
BsonMapper.Global.RegisterType<Request>( BsonMapper.Global.RegisterType<Request>(requestToBson, requestFromBson)
Func<Request, BsonValue> requestToBson, Func<BsonValue, Request> requestFromBson) BsonMapper.Global.RegisterType<Instant>(Instant.toBson, Instant.fromBson)
BsonMapper.Global.RegisterType<RequestAction>(RequestAction.toBson, RequestAction.fromBson)
BsonMapper.Global.RegisterType<RequestId>(RequestId.toBson, RequestId.fromBson)
BsonMapper.Global.RegisterType<string option>(Option.stringToBson, Option.stringFromBson)
BsonMapper.Global.RegisterType<UserId>(UserId.toBson, UserId.fromBson)
/// Code to be run at startup /// Code to be run at startup
module Startup = module Startup =

View File

@ -4,6 +4,7 @@ module MyPrayerJournal.Domain
// fsharplint:disable RecordFieldNames // fsharplint:disable RecordFieldNames
open System
open Cuid open Cuid
open NodaTime open NodaTime
@ -32,9 +33,9 @@ module UserId =
/// How frequently a request should reappear after it is marked "Prayed" /// How frequently a request should reappear after it is marked "Prayed"
type Recurrence = type Recurrence =
| Immediate | Immediate
| Hours | Hours of int16
| Days | Days of int16
| Weeks | Weeks of int16
/// Functions to manipulate recurrences /// Functions to manipulate recurrences
module Recurrence = module Recurrence =
@ -42,26 +43,31 @@ module Recurrence =
let toString = let toString =
function function
| Immediate -> "Immediate" | Immediate -> "Immediate"
| Hours -> "Hours" | Hours h -> $"{h} Hours"
| Days -> "Days" | Days d -> $"{d} Days"
| Weeks -> "Weeks" | Weeks w -> $"{w} Weeks"
/// Create a recurrence value from a string /// Create a recurrence value from a string
let ofString = let ofString =
function function
| "Immediate" -> Immediate | "Immediate" -> Immediate
| "Hours" -> Hours | it when it.Contains " " ->
| "Days" -> Days let parts = it.Split " "
| "Weeks" -> Weeks 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" | it -> invalidOp $"{it} is not a valid recurrence"
/// An hour's worth of seconds /// An hour's worth of seconds
let private oneHour = 3_600L let private oneHour = 3_600L
/// The duration of the recurrence (in milliseconds) /// The duration of the recurrence (in milliseconds)
let duration x = let duration =
(match x with function
| Immediate -> 0L | Immediate -> 0L
| Hours -> oneHour | Hours h -> int64 h * oneHour
| Days -> oneHour * 24L | Days d -> int64 d * oneHour * 24L
| Weeks -> oneHour * 24L * 7L) | Weeks w -> int64 w * oneHour * 24L * 7L
/// The action taken on a request as part of a history entry /// The action taken on a request as part of a history entry