diff --git a/src/quotesite.nim b/src/quotesite.nim index f43371c..7daa94b 100644 --- a/src/quotesite.nim +++ b/src/quotesite.nim @@ -21,7 +21,17 @@ try: nick TEXT, message TEXT, time REAL, - deleted INTEGER DEFAULT 0)""") + deleted INTEGER DEFAULT 0);""") + + db.exec(sql"""create table if not exists votes ( + id integer primary key, + quoteid integer, + ip TEXT UNIQUE);""") + + try: + db.exec(sql"""ALTER TABLE quotes + ADD votes INTEGER DEFAULT 0 NOT NULL""") + except: discard except: echo getCurrentExceptionMsg() @@ -69,6 +79,7 @@ template contextQuote(ctx: Context, qid: int, quote: Row): expr = ctx["adder"] = quote[3] ctx["message"] = quote[4] ctx["time"] = parseInt(split(quote[5], '.')[0]) + ctx["score"] = quote[7] template pagination(ctx: Context, qid: int, kind: string): expr = ctx["paging"] = true @@ -133,6 +144,23 @@ routes: except: fail() + get "/quotes/@id/vote": + try: + let + row = db.getRow(sql"SELECT * FROM quotes WHERE id=?", @"id") + + if row[0] == "": + raise newException(ValueError, "no such quote") + + let + qid = row[0].parseInt() + + db.exec(sql"insert into votes values(null, ?, ?);", qid, request.headers.getOrDefault "X-Forwarded-For") + db.exec(sql"update quotes set votes = votes + 1 where id = ?", qid) + + redirect "/quotes/" & row[0] + except: fail() + get "/@kind/@id": var title: string = "" @@ -165,8 +193,6 @@ routes: else: halt Http404, "not found" - echo request.headers.getOrDefault "X-Forwarded-For" - ctx = newContext() var quoteSeq = newSeq[Context]() diff --git a/src/templates/browse.mustache b/src/templates/browse.mustache index 144724a..1649a09 100644 --- a/src/templates/browse.mustache +++ b/src/templates/browse.mustache @@ -2,7 +2,7 @@

{{channel}} - #{{listid}} - {{time}}

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

- Added by {{adder}}. permalink + added by {{adder}}. permalink upvote score: {{ score }}
{{/ quotes }}