Change the measure model: less pre-determined, more configurable.

This commit is contained in:
2019-03-10 15:59:02 -05:00
parent b629ec1a14
commit 4965d162ad
8 changed files with 43 additions and 44 deletions

View File

@ -251,6 +251,25 @@ proc start*(ctx: PMApiContext): void =
resp(Http200, $(%session.user), JSON)
put "/user":
checkAuth()
try:
let jsonBody = parseJson(request.body)
var updatedUser = session.user
# if jsonBody.hasKey("email") # TODO: add verification?
if jsonBody.hasKey("displayName"):
updatedUser.displayName = jsonBody["displayName"].getStr()
jsonResp(Http200, $(%ctx.db.updateUser(updatedUser)))
except JsonParsingError: jsonResp(Http400, getCurrentExceptionMsg())
except BadRequestError: jsonResp(Http400, getCurrentExceptionMsg())
except:
error "Could not update user information:\n\t" & getCurrentExceptionMsg()
jsonResp(Http500)
get "/users":
checkAuth(true)
@ -381,19 +400,7 @@ proc start*(ctx: PMApiContext): void =
slug: slug,
name: name,
description: jsonBody.getIfExists("description").getStr(""),
domainSource:
if jsonBody.hasKey("domainSource"): some(jsonBody["domainSource"].getStr)
else: none[string](),
domainUnits: jsonBody.getIfExists("domainUnits").getStr(""),
rangeSource:
if jsonBody.hasKey("rangeSource"): some(jsonBody["rangeSource"].getStr)
else: none[string](),
rangeUnits: jsonBody.getIfExists("rangeUnits").getStr(""),
analysis: @[])
if jsonBody.hasKey("analysis"):
for a in jsonBody["analysis"].getElems:
newMeasure.analysis.add(a.getStr)
config: jsonBody.getOrFail("config"))
resp($(%ctx.db.createMeasure(newMeasure)), JSON)

View File

@ -23,11 +23,7 @@ type
slug*: string
name*: string
description*: string
domainSource*: Option[string]
domainUnits*: string
rangeSource*: Option[string]
rangeUnits*: string
analysis*: seq[string]
config*: JsonNode
Measurement* = object
id*: UUID
@ -81,18 +77,3 @@ proc `%`*(tok: ApiToken): JsonNode =
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)