Implemented offset.
h3rald h3rald@h3rald.com
Sun, 01 Feb 2015 11:08:11 +0100
4 files changed,
17 insertions(+),
3 deletions(-)
M
api_v1.nim
→
api_v1.nim
@@ -31,6 +31,11 @@ try:
options.limit = pair[1].parseInt except: raise newException(EInvalidRequest, "Invalid limit value: $1" % getCurrentExceptionMsg()) + of "offset": + try: + options.offset = pair[1].parseInt + except: + raise newException(EInvalidRequest, "Invalid offset value: $1" % getCurrentExceptionMsg()) of "sort": let orderby = pair[1].orderByClause() if orderby != "":@@ -141,11 +146,13 @@
proc getRawDocuments(LS: LiteStore, options: QueryOptions = newQueryOptions()): Response = var options = options let docs = LS.store.retrieveRawDocuments(options) - var orig_limit = options.limit + let orig_limit = options.limit + let orig_offset = options.offset options.limit = 0 + options.offset = 0 options.select = "COUNT(id)" let total = LS.store.retrieveRawDocuments(options)[0].num - if docs == %"[]": + if docs.len == 0: result = resError(Http404, "No documents found.") else: var content = newJObject()@@ -157,6 +164,10 @@ for tag in options.tags.decodeURL.split(","):
content["tags"].add(%tag) if orig_limit > 0: content["limit"] = %orig_limit + if orig_offset > 0: + content["offset"] = %orig_offset + if options.orderby != "": + content["sort"] = %options.orderby content["total"] = %total content["results"] = docs result.headers = ctJsonHeader()
M
types.nim
→
types.nim
@@ -14,6 +14,7 @@ QueryOptions* = object
select*: string single*:bool limit*: int + offset*: int orderby*: string tags*: string search*: string@@ -65,4 +66,4 @@ proc ctJsonHeader*(): StringTableRef =
return CT_JSON.newStringTable proc newQueryOptions*(): QueryOptions = - return QueryOptions(select: "*", single: false, limit: 0, orderby: "", tags: "", search: "") + return QueryOptions(select: "*", single: false, limit: 0, offset: 0, orderby: "", tags: "", search: "")
M
utils.nim
→
utils.nim
@@ -36,6 +36,8 @@ if options.orderby.len > 0:
result = result & "ORDER BY " & options.orderby & " " if options.limit > 0: result = result & "LIMIT " & $options.limit & " " + if options.offset > 0: + result = result & "OFFSET " & $options.offset & " " proc prepareSelectTagsQuery*(options: QueryOptions): string = result = "SELECT tag_id, COUNT(document_ID) "