Implemented query for time ranges on created and updated; added check for invalid options.
h3rald h3rald@h3rald.com
Sat, 15 Sep 2018 14:22:20 +0200
3 files changed,
36 insertions(+),
2 deletions(-)
M
lib/api_v4.nim
→
lib/api_v4.nim
@@ -159,6 +159,26 @@ of "search":
options.search = pair[1] of "tags": options.tags = pair[1] + of "created-after": + try: + options.createdAfter = pair[1].parseInt.fromUnix.format("yyyy-MM-dd'T'HH:mm:ss'Z'") + except: + raise newException(EInvalidRequest, "Invalid created-after value: $1" % getCurrentExceptionMsg()) + of "created-before": + try: + options.createdBefore = pair[1].parseInt.fromUnix.format("yyyy-MM-dd'T'HH:mm:ss'Z'") + except: + raise newException(EInvalidRequest, "Invalid created-before value: $1" % getCurrentExceptionMsg()) + of "modified-after": + try: + options.modifiedAfter = pair[1].parseInt.fromUnix.format("yyyy-MM-dd'T'HH:mm:ss'Z'") + except: + raise newException(EInvalidRequest, "Invalid modified.after value: $1" % getCurrentExceptionMsg()) + of "modified-before": + try: + options.modifiedBefore = pair[1].parseInt.fromUnix.format("yyyy-MM-dd'T'HH:mm:ss'Z'") + except: + raise newException(EInvalidRequest, "Invalid modified-before value: $1" % getCurrentExceptionMsg()) of "limit": try: options.limit = pair[1].parseInt@@ -176,7 +196,7 @@ options.orderby = orderby
else: raise newException(EInvalidRequest, "Invalid sort value: $1" % pair[1]) else: - return + raise newException(EInvalidRequest, "Invalid option: $1" % pair[0]) proc parseQueryOptions*(querystring: string, options: var QueryOptions) = var fragments = querystring.split('&')
M
lib/types.nim
→
lib/types.nim
@@ -37,6 +37,10 @@ offset*: int
orderby*: string tags*: string like*: string + createdAfter*: string + createdBefore*: string + modifiedAfter*: string + modifiedBefore*: string folder*: string search*: string TagExpression* = object@@ -110,4 +114,6 @@ "Server": LS.appname & "/" & LS.appversion
} proc newQueryOptions*(): QueryOptions = - return QueryOptions(select: @["documents.id AS id", "documents.data AS data", "content_type", "binary", "searchable", "created", "modified"], single: false, limit: 0, offset: 0, orderby: "", tags: "", search: "", folder: "", like: "", jsonFilter: "", jsonSelect: newSeq[tuple[path: string, alias: string]](), tables: newSeq[string]()) + return QueryOptions(select: @["documents.id AS id", "documents.data AS data", "content_type", "binary", "searchable", "created", "modified"], + single: false, limit: 0, offset: 0, orderby: "", tags: "", search: "", folder: "", like: "", + createdAfter: "", createdBefore: "", modifiedAfter: "", modifiedBefore: "", jsonFilter: "", jsonSelect: newSeq[tuple[path: string, alias: string]](), tables: newSeq[string]())
M
lib/utils.nim
→
lib/utils.nim
@@ -80,6 +80,14 @@ if options.jsonFilter.len > 0 or (options.search.len > 0 and options.select[0] != "COUNT(docid)"):
doc_id_col = "documents.id" else: doc_id_col = "id" + if options.createdAfter != "": + result = result & "AND created > \"" & $options.createdAfter & "\" " + if options.createdBefore != "": + result = result & "AND created < \"" & $options.createdBefore & "\" " + if options.modifiedAfter != "": + result = result & "AND modified > \"" & $options.modifiedAfter & "\" " + if options.modifiedBefore != "": + result = result & "AND modified < \"" & $options.modifiedBefore & "\" " if options.folder.len > 0: result = result & "AND " & doc_id_col & " LIKE ? " if options.tags.len > 0: