Allowing Users to Post New Articles
In this section, we'll add functionality that will allow users to post new articles.
The code for this functionality will be organized as follows:
models.article.go
will be updated to create a new article,handlers.article.go
will be updated with the new route handlers to show the article creating page and to process the article creation request,routes.go
will be updated with the new routes,templates/create-article.html
will display the article creation form,templates/submission-successful.html
will be displayed after a successful article submission, andtemplates/menu.html
will be updated with the newCreate Article
link.
Let's begin by creating a placeholder for the function to create a new article in models.article.go
, as follows:
// models.article.go
func createNewArticle(title, content string) (*article, error) {
return nil, nil
}
We need to update the handlers.article.go
with a showArticleCreationPage
handler to show the article creation page and a createArticle
handler to process the article creation request.
For now, we'll add empty handlers so that we can write tests for them. The updated handlers.article.go
file should contain the following code in addition to the previous one:
// handlers.article.go
func showArticleCreationPage(c *gin.Context) {}
func createArticle(c *gin.Context) {}