api: Refactor measurements URLs, add Measure update endpoint.
This commit is contained in:
		| @@ -392,6 +392,8 @@ proc start*(ctx: PMApiContext): void = | ||||
|       except NotFoundError: statusResp(Http404, getCurrentExceptionMsg()) | ||||
|       except: statusResp(Http500) | ||||
|  | ||||
|     # Measure | ||||
|  | ||||
|     get "/measures": | ||||
|       checkAuth() | ||||
|  | ||||
| @@ -445,6 +447,37 @@ proc start*(ctx: PMApiContext): void = | ||||
|         error "unable to look up a measure by id:\n\t" & getCurrentExceptionMsg() | ||||
|         statusResp(Http500) | ||||
|  | ||||
|     post "/measures/@slug": | ||||
|       checkAuth() | ||||
|  | ||||
|       try: | ||||
|         let jsonBody = parseJson(request.body) | ||||
|         var existingMeasure = ctx.getMeasureForSlug(session.user.id, @"slug") | ||||
|  | ||||
|         if not (jsonBody.hasKey("slug") or jsonBody.hasKey("name")): | ||||
|           raiseEx BadRequestError, "body must contain either the 'slug' field (short name), or the 'name' field, or both" | ||||
|  | ||||
|         existingMeasure.slug = | ||||
|           if jsonBody.hasKey("slug"): jsonBody["slug"].getStr.nameToSlug | ||||
|           else: jsonBody["name"].getStr.nameToSlug | ||||
|  | ||||
|         existingMeasure.name = | ||||
|           if jsonBody.hasKey("name"): jsonBody["name"].getStr | ||||
|           else: jsonBody["slug"].getStr.capitalize | ||||
|  | ||||
|  | ||||
|         if jsonBody.hasKey("config"): existingMeasure.config = jsonBody["config"] | ||||
|  | ||||
|         if jsonBody.hasKey("description"): existingMeasure.description = jsonBody["description"].getStr | ||||
|  | ||||
|         jsonResp($(%ctx.db.updateMeasure(existingMeasure))) | ||||
|  | ||||
|       except JsonParsingError: statusResp(Http400, getCurrentExceptionMsg()) | ||||
|       except BadRequestError: statusResp(Http400, getCurrentExceptionMsg()) | ||||
|       except: | ||||
|         error "unable to update measure:\n\t" & getCurrentExceptionMsg() | ||||
|         statusResp(Http500) | ||||
|  | ||||
|     delete "/measures/@slug": | ||||
|       checkAuth() | ||||
|  | ||||
| @@ -457,7 +490,8 @@ proc start*(ctx: PMApiContext): void = | ||||
|           error "unable to delete a measure:\n\t" & getCurrentExceptionMsg() | ||||
|           statusResp(Http500) | ||||
|  | ||||
|     get "/measure/@slug": | ||||
|     # Measurements | ||||
|     get "/measurements/@slug": | ||||
|       checkAuth() | ||||
|  | ||||
|       try: | ||||
| @@ -468,7 +502,7 @@ proc start*(ctx: PMApiContext): void = | ||||
|           error "unable to list measurements:\n\t" & getCurrentExceptionMsg() | ||||
|           statusResp(Http500) | ||||
|  | ||||
|     post "/measure/@slug": | ||||
|     post "/measurements/@slug": | ||||
|       checkAuth() | ||||
|  | ||||
|       try: | ||||
| @@ -494,7 +528,7 @@ proc start*(ctx: PMApiContext): void = | ||||
|         error "unable to add measurement:\n\t" & getCurrentExceptionMsg() | ||||
|         statusResp(Http500) | ||||
|  | ||||
|     get "/measure/@slug/@id": | ||||
|     get "/measurements/@slug/@id": | ||||
|       checkAuth() | ||||
|  | ||||
|       try: | ||||
| @@ -509,7 +543,7 @@ proc start*(ctx: PMApiContext): void = | ||||
|         error "unable to retrieve measurement:\n\t" & getCurrentExceptionMsg() | ||||
|         statusResp(Http500) | ||||
|  | ||||
|     put "/measure/@slug/@id": | ||||
|     put "/measurements/@slug/@id": | ||||
|       checkAuth() | ||||
|  | ||||
|       try: | ||||
| @@ -529,7 +563,7 @@ proc start*(ctx: PMApiContext): void = | ||||
|         error "unable to retrieve measurement:\n\t" & getCurrentExceptionMsg() | ||||
|         statusResp(Http500) | ||||
|  | ||||
|     delete "/measure/@slug/@id": | ||||
|     delete "/measurements/@slug/@id": | ||||
|       checkAuth() | ||||
|  | ||||
|       try: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user