show list of quotes

This commit is contained in:
Christine Dodrill 2016-02-05 18:07:11 -08:00
parent 89201245d7
commit cf933531b1
5 changed files with 80 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
nimcache nimcache
quotesite quotesite
*.db

0
data/.gitkeep Normal file
View File

View File

@ -1,11 +1,30 @@
import asyncdispatch, jester, moustachu, os, strutils import asyncdispatch, db_sqlite, jester, moustachu, os,
shorturl, strutils, tables, times
const const
baseTemplate*: string = staticRead "./templates/layout.mustache" baseTemplate*: string = staticRead "./templates/layout.mustache"
indexTemplate*: string = staticRead "./templates/index.mustache" indexTemplate*: string = staticRead "./templates/index.mustache"
errorTemplate*: string = staticRead "./templates/error.mustache"
testTemplate*: string = """<p>hi!</p>""" testTemplate*: string = """<p>hi!</p>"""
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 = template renderMustache*(title: string, templ: string, ctx: Context): expr =
var var
layoutCtx = moustachu.newContext() layoutCtx = moustachu.newContext()
@ -15,12 +34,54 @@ template renderMustache*(title: string, templ: string, ctx: Context): expr =
resp render(baseTemplate, layoutCtx) resp render(baseTemplate, layoutCtx)
template fail*(): expr =
var ctx = newContext()
ctx["exception"] = getCurrentExceptionMsg()
renderMustache("error", errorTemplate, ctx)
settings: settings:
port = 5000.Port port = 5000.Port
bindAddr = "0.0.0.0" bindAddr = "0.0.0.0"
routes: routes:
get "/": get "/test":
renderMustache("test", testTemplate, newContext()) 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() runForever()

View File

@ -0,0 +1,9 @@
<div class="error">
<center>
<p>could not find that page. are you sure you clicked on a valid link?</p>
</center>
<pre>
{{ exception }}
</pre>
</div>

View File

@ -1,9 +1,12 @@
<h1>Latest Quotes</h1>
{{# quotes }} {{# quotes }}
<div class="entries"> <div class="entries">
<h3>{{channel}} - {{time}}</h3> <h3><a href="/channel/{{ channelsafe }}">{{channel}}</a> - {{time}}</h3>
<p>({{nick}}) {{message}}</p> <p>({{nick}}) {{message}}</p>
<small><i>Added by {{adder}}. <a href="/quotes/{{id}}">Permalink.</i></small> <small><i>Added by {{adder}}. <a href="/quotes/{{id}}">Permalink.</a></i></small>
<br />
</div> </div>
{{/ quotes }} {{/ quotes }}
<br />
<center><a href="/browse/2">→</a></center>