diff --git a/src/quotesite.nim b/src/quotesite.nim index f3d54e8..d0887de 100644 --- a/src/quotesite.nim +++ b/src/quotesite.nim @@ -199,48 +199,50 @@ routes: fail() get "/@kind/@id": - var - title: string = "" - ctx: Context - rows: seq[Row] - qid: int = 0 - try: - qid = (@"id").parseInt - except: - qid = (@"id").decodeURLSimple() - redirect "/" & @"kind" & "/" & $qid + var + title: string = "" + ctx: Context + rows: seq[Row] + qid: int = 0 - case @"kind": - of "browse": - rows = db.getAllRows(sql"SELECT * FROM quotes where deleted=0 ORDER BY time desc LIMIT 20 OFFSET ?", (qid * 20)) + try: + qid = (@"id").parseInt + except: + qid = (@"id").decodeURLSimple() + redirect "/" & @"kind" & "/" & $qid - title = "page " & @"id" - of "quotes": - rows = db.getAllRows(sql"SELECT * FROM quotes WHERE id = ?", qid) + case @"kind": + of "browse": + rows = db.getAllRows(sql"SELECT * FROM quotes where deleted=0 ORDER BY time desc LIMIT 20 OFFSET ?", (qid * 20)) - title = "#" & $qid & " by " & rows[0][2] - else: - if (@"kind").startsWith "hashtag-": - let channel = (@"kind").replace("hashtag-", "#") + title = "page " & @"id" + of "quotes": + rows = db.getAllRows(sql"SELECT * FROM quotes WHERE id = ?", qid) - rows = db.getAllRows(sql"SELECT * FROM quotes WHERE channel=?, deleted=0 ORDER BY time desc LIMIT 20 OFFSET ?", channel, qid * 20) - - title = channel & " page " & @"id" + title = "#" & $qid & " by " & rows[0][2] else: - halt Http404, "not found" + if (@"kind").startsWith "hashtag-": + let channel = (@"kind").replace("hashtag-", "#") - ctx = newContext() - var quoteSeq = newSeq[Context]() + rows = db.getAllRows(sql"SELECT * FROM quotes WHERE channel=?, deleted=0 ORDER BY time desc LIMIT 20 OFFSET ?", channel, qid * 20) - for row in items(rows): - var c = newContext() - c.contextQuote row[0].parseInt(), row - quoteSeq.add c + title = channel & " page " & @"id" + else: + halt Http404, "not found" - ctx["quotes"] = quoteSeq - ctx.pagination(qid, @"kind") + ctx = newContext() + var quoteSeq = newSeq[Context]() - renderMustache(title, browseTemplate, ctx) + for row in items(rows): + var c = newContext() + c.contextQuote row[0].parseInt(), row + quoteSeq.add c + + ctx["quotes"] = quoteSeq + ctx.pagination(qid, @"kind") + + renderMustache(title, browseTemplate, ctx) + except: fail() runForever()