diff --git a/AutoJobs.fsproj b/AutoJobs.fsproj index 20f92aa..f6ee643 100644 --- a/AutoJobs.fsproj +++ b/AutoJobs.fsproj @@ -11,6 +11,7 @@ + diff --git a/src/Codec.fs b/src/Codec.fs new file mode 100644 index 0000000..18c11f4 --- /dev/null +++ b/src/Codec.fs @@ -0,0 +1,83 @@ +namespace TcFairplay + +open System +open System.Text.Json + +[] +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 = diff --git a/src/GotCourts.fs b/src/GotCourts.fs index eb4d7d3..97cf1ec 100644 --- a/src/GotCourts.fs +++ b/src/GotCourts.fs @@ -38,58 +38,10 @@ module GotCourts = 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 private toKeyValuePair (x, y) = KeyValuePair(x, y) let private clubId = 53223 - let private courtToId = function - | Court1 -> 8153 - | Court2 -> 8154 - | Court3 -> 8155 - - let private idToCourt = function - | 8153 -> Court1 - | 8154 -> Court2 - | 8155 -> Court3 - | id -> failwithf "Unknown court id '%d." id - - let private buildBlockingPairs (blocking: Blocking): (string * string) list = - let toCourtPair no = ("courts[]", no |> courtToId |> string) - let dateTimePairs = - let timePairs = - match blocking.StartEnd with - | Some (s, e) -> [ - "time[start]", Api.formatTime s - "time[end]", Api.formatTime e - ] - | None -> [] - - let date = Api.formatDate blocking.Date - [ - "date", date - "dateTo", date - "allDay[disabled]", (blocking.StartEnd |> Option.isSome |> string |> fun s -> s.ToLower()) - ] @ timePairs - - dateTimePairs @ - (blocking.Courts |> List.map toCourtPair) @ - [ - "autoremove", "true" - "type", "other" - "description", blocking.Description - "note", blocking.Note - ] - let processResponse (rawJson: string): Result = let doc = JsonDocument.Parse rawJson let success = (doc.RootElement.GetProperty "status").GetBoolean() @@ -102,7 +54,7 @@ module GotCourts = Error errorText let createBlocking (client: HttpClient) (blocking: Blocking): Result = - let pairs = buildBlockingPairs blocking + let pairs = Blocking.toKeyValueMap blocking use content = new FormUrlEncodedContent(pairs |> List.map toKeyValuePair) let respMsg = client.PostAsync (blockingUrl, content) |> await @@ -134,24 +86,8 @@ module GotCourts = let getBlockings (resp: JsonElement) = let blockings = resp.GetProperty "blockings" - let parseBlocking (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" |> idToCourt] - Date = date - StartEnd = Some (getTime "startTime", getTime "endTime") - Note = getString "note" - } - (guid, blocking) - blockings.EnumerateArray () - |> Seq.map parseBlocking + |> Seq.map Blocking.parse |> Seq.toList processResponse rawJson