From adddef3188377610de16d949c802aceb0c70a111 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sun, 16 Feb 2020 23:20:02 -0600 Subject: [PATCH] api: Support for decimal values in measures. --- api/personal_measure_api.nimble | 4 ++-- api/src/main/nim/personal_measure_apipkg/api.nim | 9 +++++---- api/src/main/nim/personal_measure_apipkg/models.nim | 2 +- .../20200216230431-measure-value-is-numeric-down.sql | 2 ++ .../20200216230431-measure-value-is-numeric-up.sql | 2 ++ 5 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 api/src/main/sql/migrations/20200216230431-measure-value-is-numeric-down.sql create mode 100644 api/src/main/sql/migrations/20200216230431-measure-value-is-numeric-up.sql diff --git a/api/personal_measure_api.nimble b/api/personal_measure_api.nimble index d0fc10c..a8a3813 100644 --- a/api/personal_measure_api.nimble +++ b/api/personal_measure_api.nimble @@ -17,5 +17,5 @@ requires @["nim >= 0.19.4", "bcrypt", "docopt >= 0.6.8", "isaac >= 0.1.3", "jester >= 0.4.3", "jwt", "tempfile", "uuids >= 0.1.10" ] requires "https://git.jdb-labs.com/jdb/nim-cli-utils.git >= 0.6.3" -requires "https://git.jdb-labs.com/jdb/nim-time-utils.git >= 0.5.0" -requires "https://git.jdb-labs.com/jdb-labs/fiber-orm-nim.git >= 0.2.0" +requires "https://git.jdb-labs.com/jdb/nim-time-utils.git >= 0.5.2" +requires "https://git.jdb-labs.com/jdb-labs/fiber-orm-nim.git >= 0.3.0" diff --git a/api/src/main/nim/personal_measure_apipkg/api.nim b/api/src/main/nim/personal_measure_apipkg/api.nim index c6703c4..3aae6b9 100644 --- a/api/src/main/nim/personal_measure_apipkg/api.nim +++ b/api/src/main/nim/personal_measure_apipkg/api.nim @@ -1,7 +1,8 @@ import asyncdispatch, base64, jester, json, jwt, logging, options, sequtils, - strutils, times, uuids + times, uuids from unicode import capitalize -import timeutils except `<` +import strutils except capitalize +import timeutils import ./db, ./configuration, ./models, ./service, ./version @@ -476,7 +477,7 @@ proc start*(ctx: PMApiContext): void = let newMeasurement = Measurement( measureId: measure.id, - value: jsonBody.getOrFail("value").getInt, + value: jsonBody.getOrFail("value").getFloat, timestamp: if jsonBody.hasKey("timestamp"): jsonBody["timestamp"].getStr.parseIso8601.utc else: getTime().utc, @@ -515,7 +516,7 @@ proc start*(ctx: PMApiContext): void = let measure = ctx.getMeasureForSlug(session.user.id, @"slug") var measurement = ctx.getMeasurementForMeasure(measure.id, parseUUID(@"id")) let jsonBody = parseJson(request.body) - if jsonBody.hasKey("value"): measurement.value = jsonBody["value"].getInt + if jsonBody.hasKey("value"): measurement.value = jsonBody["value"].getFloat if jsonBody.hasKey("timestamp"): measurement.timestamp = jsonBody["timestamp"].getStr.parseIso8601 if jsonBody.hasKey("extData"): measurement.extData = jsonBody["extData"] jsonResp($(%ctx.db.updateMeasurement(measurement))) diff --git a/api/src/main/nim/personal_measure_apipkg/models.nim b/api/src/main/nim/personal_measure_apipkg/models.nim index c97c4e8..38336b2 100644 --- a/api/src/main/nim/personal_measure_apipkg/models.nim +++ b/api/src/main/nim/personal_measure_apipkg/models.nim @@ -28,7 +28,7 @@ type Measurement* = object id*: UUID measureId*: UUID - value*: int + value*: float timestamp*: DateTime extData*: JsonNode diff --git a/api/src/main/sql/migrations/20200216230431-measure-value-is-numeric-down.sql b/api/src/main/sql/migrations/20200216230431-measure-value-is-numeric-down.sql new file mode 100644 index 0000000..21938e3 --- /dev/null +++ b/api/src/main/sql/migrations/20200216230431-measure-value-is-numeric-down.sql @@ -0,0 +1,2 @@ +-- DOWN script for measure-value-is-numeric (20200216230431) +alter table "measurements" alter column "value" type integer; diff --git a/api/src/main/sql/migrations/20200216230431-measure-value-is-numeric-up.sql b/api/src/main/sql/migrations/20200216230431-measure-value-is-numeric-up.sql new file mode 100644 index 0000000..964bfed --- /dev/null +++ b/api/src/main/sql/migrations/20200216230431-measure-value-is-numeric-up.sql @@ -0,0 +1,2 @@ +-- UP script for measure-value-is-numeric (20200216230431) +alter table "measurements" alter column "value" type numeric;