This layout requires each page to define the title, links, and content template. So, let's edit web/src/pages/index.html and define those template blocks:
{{ define "title" }}Hacker News{{ end }}{{ define "links" }}<a href="/submit">submit</a>{{ end }}{{ define "content" }}{{ end }}
The Submit Page
Hacker News requires users to make submissions. Let's create a new Submit page with a simple form that can accept these submissions.
Notice that the markup includes an HTML form that makes a POST request to the /submit endpoint with title and urlas the form values.
Open http://localhost:5901/submit in your browser and play around with the page. For now, hitting submit won't work because we don't have a route handler for submissions.
Save Posts
Let's set up a storage layer that can save user submissions into our database. We'll keep this logic in a new package called posts:
The above commands create the pkg/posts package as well as a pkg/posts/queries.go that we can use to implement our SQL queries.
Before we make any SQL queries, let's create the Post model and its database migration:
Open up pkg/posts/models.go and define the Post model:
typePoststruct { ID string Title string URL string Poster string}
Then, open up migrations/0001_initial.sql and define its database schema:
-- +migrate Upcreatetableifnotexists posts( id textprimary key, title text,urltext, poster text);-- +migrate Downdroptable posts;
In your terminal, run copper migrate to create the table.
Now that we have a Post model and a posts table in our database, we can write queries in the pkg/posts/queries.go file. Add the following SavePost method that can be used to insert a new entry into the posts table:
Now, your submit page should work! Click submit will make a new entry into the posts table.
The Home Page
On the home page, we want to show a list of all posts that exist in the database. Let's start with creating a new SQL query and work our way up from there. In pkg/posts/queries.go, add the following method to list all posts: