api: initial implementation with support for creating EventProposals.
This commit is contained in:
77
api/src/hff_entry_forms_apipkg/models.nim
Normal file
77
api/src/hff_entry_forms_apipkg/models.nim
Normal file
@ -0,0 +1,77 @@
|
||||
import json, times, timeutils
|
||||
|
||||
type
|
||||
EventProposal* = object
|
||||
name*: string
|
||||
description*: string
|
||||
purpose*: string
|
||||
department*: string
|
||||
owner*: string
|
||||
date*: DateTime
|
||||
budgetInDollars*: int
|
||||
|
||||
proc getOrFail(n: JsonNode, key: string): JsonNode =
|
||||
## convenience method to get a key from a JObject or raise an exception
|
||||
if not n.hasKey(key):
|
||||
raise newException(ValueError, "missing key '" & key & "'")
|
||||
|
||||
return n[key]
|
||||
|
||||
proc parseIso8601(n: JsonNode, key: string): DateTime =
|
||||
return parseIso8601(n.getOrFail(key).getStr)
|
||||
|
||||
proc textProp(value: string): JsonNode =
|
||||
return %*[
|
||||
{
|
||||
"type": "text",
|
||||
"text": { "content": value }
|
||||
}
|
||||
]
|
||||
|
||||
proc parseEventProposal*(n: JsonNode): EventProposal {.raises: [JsonParsingError].} =
|
||||
|
||||
try:
|
||||
result = EventProposal(
|
||||
name: n.getOrFail("name").getStr,
|
||||
description: n.getOrFail("description").getStr,
|
||||
purpose: n.getOrFail("purpose").getStr,
|
||||
department: n.getOrFail("department").getStr,
|
||||
owner: n.getOrFail("owner").getStr,
|
||||
date: n.parseIso8601("date"),
|
||||
budgetInDollars: n.getOrFail("budgetInDollars").getInt)
|
||||
except:
|
||||
raise newException(JsonParsingError, "Invalid EventProposal: " & getCurrentExceptionMsg())
|
||||
|
||||
proc asNotionPage*(ep: EventProposal): JsonNode =
|
||||
result = %*{
|
||||
"properties": {
|
||||
"Event": { "title": textProp(ep.name) },
|
||||
"Date": { "date": { "start": formatIso8601(ep.date) } },
|
||||
"Department": { "multi_select": [ { "name": ep.department } ] },
|
||||
"Location": { "rich_text": textProp("") },
|
||||
"Owner": { "rich_text": textProp(ep.owner) },
|
||||
"State": { "select": { "name": "Proposed" } }
|
||||
},
|
||||
"children": [
|
||||
{
|
||||
"object": "block",
|
||||
"type": "heading_2",
|
||||
"heading_2": { "text": textProp("Purpose") }
|
||||
},
|
||||
{
|
||||
"object": "block",
|
||||
"type": "paragraph",
|
||||
"paragraph": { "text": textProp(ep.purpose) }
|
||||
},
|
||||
{
|
||||
"object": "block",
|
||||
"type": "heading_2",
|
||||
"heading_2": { "text": textProp("Description") }
|
||||
},
|
||||
{
|
||||
"object": "block",
|
||||
"type": "paragraph",
|
||||
"paragraph": { "text": textProp(ep.description) }
|
||||
}
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user