From cf933531b1defb8d2b7fa9945c39077c596bcf38 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 5 Feb 2016 18:07:11 -0800 Subject: [PATCH] show list of quotes --- .gitignore | 1 + data/.gitkeep | 0 src/quotesite.nim | 65 ++++++++++++++++++++++++++++++++++-- src/templates/error.mustache | 9 +++++ src/templates/index.mustache | 11 +++--- 5 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 data/.gitkeep create mode 100644 src/templates/error.mustache diff --git a/.gitignore b/.gitignore index 4b9b1f3..3ed5569 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ nimcache quotesite +*.db diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/quotesite.nim b/src/quotesite.nim index 96cbb9b..ca85410 100644 --- a/src/quotesite.nim +++ b/src/quotesite.nim @@ -1,11 +1,30 @@ -import asyncdispatch, jester, moustachu, os, strutils +import asyncdispatch, db_sqlite, jester, moustachu, os, + shorturl, strutils, tables, times const baseTemplate*: string = staticRead "./templates/layout.mustache" indexTemplate*: string = staticRead "./templates/index.mustache" + errorTemplate*: string = staticRead "./templates/error.mustache" testTemplate*: string = """

hi!

""" +let + db = open("data/quotes.db", nil, nil, nil) + +try: + db.exec(sql"""create table if not exists quotes ( + id INTEGER PRIMARY KEY, + channel TEXT, + adder TEXT, + nick TEXT, + message TEXT, + time REAL, + deleted INTEGER DEFAULT 0)""") + +except: + echo getCurrentExceptionMsg() + raise + template renderMustache*(title: string, templ: string, ctx: Context): expr = var layoutCtx = moustachu.newContext() @@ -15,12 +34,54 @@ template renderMustache*(title: string, templ: string, ctx: Context): expr = resp render(baseTemplate, layoutCtx) +template fail*(): expr = + var ctx = newContext() + ctx["exception"] = getCurrentExceptionMsg() + renderMustache("error", errorTemplate, ctx) + settings: port = 5000.Port bindAddr = "0.0.0.0" routes: - get "/": + get "/test": renderMustache("test", testTemplate, newContext()) + get "/": + let quotes = db.getAllRows(sql"SELECT * FROM quotes ORDER BY time desc LIMIT 20") + + var + ctx = newContext() + quoteSeq = newSeq[Context]() + + for quote in items(quotes): + var c = newContext() + + c["id"] = quote[0].parseInt().encodeURLSimple() + c["channel"] = quote[1] + c["channelsafe"] = quote[1].replace("#", "hashtag-") + c["nick"] = quote[2] + c["adder"] = quote[3] + c["message"] = quote[4] + c["time"] = parseInt(split(quote[5], '.')[0]) + + quoteSeq.add c + + ctx["quotes"] = quoteSeq + + renderMustache("most recent quotes", indexTemplate, ctx) + + get "/quotes/@id": + var qid: int = -1 + try: + qid = (@"id").decodeURLSimple() + except: + fail() + + let quote = db.getRow(sql"SELECT * FROM quotes WHERE id = ?", qid) + + echo quote + + resp $quote + runForever() diff --git a/src/templates/error.mustache b/src/templates/error.mustache new file mode 100644 index 0000000..d8f2023 --- /dev/null +++ b/src/templates/error.mustache @@ -0,0 +1,9 @@ +
+
+

could not find that page. are you sure you clicked on a valid link?

+
+ +
+{{ exception }}
+  
+
diff --git a/src/templates/index.mustache b/src/templates/index.mustache index 2d4f782..fe959ec 100644 --- a/src/templates/index.mustache +++ b/src/templates/index.mustache @@ -1,9 +1,12 @@ -

Latest Quotes

- {{# quotes }}
-

{{channel}} - {{time}}

+

{{channel}} - {{time}}

({{nick}}) {{message}}

- Added by {{adder}}. Permalink. + Added by {{adder}}. Permalink. +
{{/ quotes }} + +
+ +