90 lines
2.1 KiB
Nim
90 lines
2.1 KiB
Nim
import json, options, times, timeutils, uuids
|
|
|
|
type
|
|
User* = object
|
|
id*: UUID
|
|
displayName*: string
|
|
email*: string
|
|
hashedPwd*: string
|
|
|
|
ApiToken* = object
|
|
id*: UUID
|
|
userId*: UUID
|
|
name*:string
|
|
expires*: Option[DateTime]
|
|
hashedToken*: string
|
|
|
|
Measure* = object
|
|
id*: UUID
|
|
userId*: UUID
|
|
slug*: string
|
|
name*: string
|
|
description*: string
|
|
domainSource*: Option[string]
|
|
domainUnits*: string
|
|
rangeSource*: Option[string]
|
|
rangeUnits*: string
|
|
analysis*: seq[string]
|
|
|
|
Value* = object
|
|
id*: UUID
|
|
measureId*: UUID
|
|
value*: int
|
|
timestamp*: DateTime
|
|
extData*: string
|
|
|
|
proc `$`*(u: User): string =
|
|
return "User " & ($u.id)[0..6] & " - " & u.displayName & " <" & u.email & ">"
|
|
|
|
proc `$`*(tok: ApiToken): string =
|
|
result = "ApiToken " & ($tok.id)[0..6] & " - " & tok.name
|
|
if tok.expires.isSome: result &= " (expires " & tok.expires.get.formatIso8601 & ")"
|
|
else: result &= " (no expiry)"
|
|
|
|
proc `$`*(m: Measure): string =
|
|
return "Measure " & ($m.id)[0..6] & " - " & m.slug
|
|
|
|
proc `$`*(v: Value): string =
|
|
return "Value " & ($v.id)[0..6] & " - " & ($v.measureId)[0..6] & " = " & $v.value
|
|
|
|
proc `%`*(u: User): JsonNode =
|
|
result = %*{
|
|
"id": $u.id,
|
|
"email": u.email,
|
|
"displayName": u.displayName
|
|
}
|
|
|
|
proc `%`*(tok: ApiToken): JsonNode =
|
|
result = %*{
|
|
"id": $tok.id,
|
|
"userId": $tok.userId,
|
|
"name": tok.name
|
|
}
|
|
|
|
if tok.expires.isSome:
|
|
result["expires"] = %(tok.expires.get.formatIso8601)
|
|
|
|
proc `%`*(m: Measure): JsonNode =
|
|
result = %*{
|
|
"id": $m.id,
|
|
"userId": $m.userId,
|
|
"slug": m.slug,
|
|
"name": m.name,
|
|
"description": m.description,
|
|
"domainUnits": m.domainUnits,
|
|
"rangeUnits": m.rangeUnits,
|
|
"analysis": m.analysis
|
|
}
|
|
|
|
if m.domainSource.isSome: result["domainSource"] = %(m.domainSource.get)
|
|
if m.rangeSource.isSome: result["rangeSource"] = %(m.rangeSource.get)
|
|
|
|
proc `%`*(v: Value): JsonNode =
|
|
result = %*{
|
|
"id": $v.id,
|
|
"measureId": $v.measureId,
|
|
"value": v.value,
|
|
"timestampe": v.timestamp.formatIso8601,
|
|
"extData": v.extData
|
|
}
|