-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Move low-level codecs to dedicated module.
- Loading branch information
Showing
3 changed files
with
86 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
namespace TcFairplay | ||
|
||
open System | ||
open System.Text.Json | ||
|
||
[<AutoOpen>] | ||
module Codec = | ||
module private Api = | ||
let private dateFormat = "yyyy-MM-dd" | ||
|
||
let formatDate (date: DateOnly): string = | ||
date.ToString(dateFormat) | ||
|
||
let parseDate (s: string): DateOnly = | ||
DateOnly.ParseExact(s, dateFormat) | ||
|
||
let formatTime (time: TimeOnly): string = | ||
(time.Hour * 60 + time.Minute) * 60 | ||
|> string | ||
|
||
let calcTime (secondsSinceMidnight: int): TimeOnly = | ||
TimeOnly(int64 secondsSinceMidnight * 10_000_000L) | ||
|
||
let courtToId = function | ||
| Court1 -> 8153 | ||
| Court2 -> 8154 | ||
| Court3 -> 8155 | ||
|
||
let idToCourt = function | ||
| 8153 -> Court1 | ||
| 8154 -> Court2 | ||
| 8155 -> Court3 | ||
| id -> failwithf "Unknown court id '%d." id | ||
|
||
module Blocking = | ||
// FIXME! | ||
let date = DateOnly(2023, 1, 1) | ||
|
||
let parse (el: JsonElement): (Guid * Blocking) = | ||
let get (name: string) = el.GetProperty name | ||
let getString name = (get name).GetString() | ||
let getInt name = (get name).GetInt32() | ||
let getTime = getInt >> Api.calcTime | ||
|
||
let guid = Guid.Parse (getString "id") | ||
let blocking = { | ||
Description = (getString "shortDesc") | ||
Courts = [getInt "courtId" |> Api.idToCourt] | ||
Date = date // FIXME! | ||
StartEnd = Some (getTime "startTime", getTime "endTime") | ||
Note = getString "note" | ||
} | ||
(guid, blocking) | ||
|
||
let toKeyValueMap (b: Blocking): (string * string) list = | ||
let toCourtPair no = ("courts[]", no |> Api.courtToId |> string) | ||
let dateTimePairs = | ||
let timePairs = | ||
match b.StartEnd with | ||
| Some (s, e) -> [ | ||
"time[start]", Api.formatTime s | ||
"time[end]", Api.formatTime e | ||
] | ||
| None -> [] | ||
|
||
let date = Api.formatDate b.Date | ||
[ | ||
"date", date | ||
"dateTo", date | ||
"allDay[disabled]", (b.StartEnd |> Option.isSome |> string |> fun s -> s.ToLower()) | ||
] @ timePairs | ||
|
||
dateTimePairs @ | ||
(b.Courts |> List.map toCourtPair) @ | ||
[ | ||
"autoremove", "true" | ||
"type", "other" | ||
"description", b.Description | ||
"note", b.Note | ||
] | ||
|
||
//module Reservation = | ||
//module Player = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters