Browse Source

Add templates and more routes for navigation

master
Wesley Kerfoot 4 years ago
parent
commit
6c2d508d9a
  1. 36
      src/twit2blogpkg/server.nim
  2. 8
      templates/authors.html
  3. 10
      templates/layout.html
  4. 3
      templates/nonexistent.html
  5. 9
      templates/thread.html
  6. 8
      templates/threads.html

36
src/twit2blogpkg/server.nim

@ -1,4 +1,4 @@
import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite
import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite, strformat
import twitter
import xander
@ -43,6 +43,14 @@ proc threadExists(threadID : string, author : string) : Option[TwitterThread] =
tweets: row[3])
)
iterator allAuthors() : string =
for author in db.getAllRows(sql"SELECT DISTINCT author FROM threads"):
yield author[0]
iterator threadIDs(author : string) : string =
for threadID in db.getAllRows(sql"SELECT tid from threads WHERE author=?", author):
yield threadID[0]
proc insertThread(thread : TwitterThread) =
db.exec(sql"INSERT INTO threads (tid, author, tweets) VALUES (?, ?, ?)",
thread.tweetID,
@ -52,17 +60,29 @@ proc insertThread(thread : TwitterThread) =
get "/thread/:author/status/:tweetID":
let tweetID = data{"tweetID"}.getStr()
let author = data{"author"}.getStr()
let thread = threadExists(tweetID, author)
if thread.isSome:
respond thread.get.tweets
data["title"] = fmt"Thread by {author}"
data["tweets"] = thread.get.tweets.split("\n")
respond tmplt("thread", data)
else:
chan.send(
ThreadRequest(tweetID: data{"tweetID"}.getStr(),
author: data{"author"}.getStr())
)
respond "Hang on, we're grabbing your thread :) Come back to this page later."
chan.send(ThreadRequest(tweetID: tweetID, author: author))
data["title"] = "Check back later"
respond tmplt("nonexistent", data)
get "/":
# lists all authors
data["authors"] = allAuthors.toSeq
data["title"] = "Authors"
respond tmplt("authors", data)
get "/author/:author/threads":
let author = data{"author"}.getStr()
data["author"] = author
data["title"] = fmt"Threads for {author}"
data["threads"] = toSeq(threadIDs(author))
respond tmplt("threads", data)
proc startServer* =
createTweetTable()

8
templates/authors.html

@ -0,0 +1,8 @@
<h4>
{[ title ]}
</h4>
<ul>
{[ for author in authors ]}
<li><p><a href="/author/{[ author ]}/threads">{[ author ]}</a></p></li>
{[ end ]}
</ul>

10
templates/layout.html

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>{[ title ]}</title>
</head>
<body>
<h3><a href="/">Main Page</a></h3>
{[ content ]}
</body>
</html>

3
templates/nonexistent.html

@ -0,0 +1,3 @@
<h4>
Check back later
</h4>

9
templates/thread.html

@ -0,0 +1,9 @@
<a href="/author/{[ author ]}/threads">See all of {[ author ]}'s threads</a>
<h4>
{[ title ]}
</h4>
<ul>
{[ for tweet in tweets ]}
<li><p>{[ tweet ]}</p></li>
{[ end ]}
</ul>

8
templates/threads.html

@ -0,0 +1,8 @@
<h4>
{[ title ]}
</h4>
<ul>
{[ for thread in threads ]}
<li><a href="/thread/{[ author ]}/status/{[ thread ]}">{[ thread ]}</a></li>
{[ end ]}
</ul>
Loading…
Cancel
Save