Implementing the Route handlers

The showArticleCreationPage handler is similar to the the previously created handlers that display static pages.

The createArticle is similar to the register handler created in a previous section.

The updated handlers.article.go file should contain the following code:

// handlers.article.go

func showArticleCreationPage(c *gin.Context) {
    render(c, gin.H{
        "title": "Create New Article"}, "create-article.html")
}

func createArticle(c *gin.Context) {
    title := c.PostForm("title")
    content := c.PostForm("content")

    if a, err := createNewArticle(title, content); err == nil {
        render(c, gin.H{
            "title":   "Submission Successful",
            "payload": a}, "submission-successful.html")
    } else {
        c.AbortWithStatus(http.StatusBadRequest)
    }
}

After implementing these handlers, the tests should execute successfully and show the following result:

=== RUN   TestShowIndexPageUnauthenticated
[GIN] 2016/09/04 - 10:41:55 | 200 |     183.623µs |  |   GET     /
--- PASS: TestShowIndexPageUnauthenticated (0.00s)
=== RUN   TestArticleUnauthenticated
[GIN] 2016/09/04 - 10:41:55 | 200 |      99.933µs |  |   GET     /article/view/1
--- PASS: TestArticleUnauthenticated (0.00s)
=== RUN   TestArticleListJSON
[GIN] 2016/09/04 - 10:41:55 | 200 |      32.855µs |  |   GET     /
--- PASS: TestArticleListJSON (0.00s)
=== RUN   TestArticleXML
[GIN] 2016/09/04 - 10:41:55 | 200 |      34.698µs |  |   GET     /article/view/1
--- PASS: TestArticleXML (0.00s)
=== RUN   TestArticleCreationAuthenticated
[GIN] 2016/09/04 - 10:41:55 | 200 |     155.667µs |  |   POST    /article/create
--- PASS: TestArticleCreationAuthenticated (0.00s)
=== RUN   TestShowRegistrationPageUnauthenticated
[GIN] 2016/09/04 - 10:41:55 | 200 |     108.247µs |  |   GET     /u/register
--- PASS: TestShowRegistrationPageUnauthenticated (0.00s)
=== RUN   TestRegisterUnauthenticated
[GIN] 2016/09/04 - 10:41:55 | 200 |     115.109µs |  |   POST    /u/register
--- PASS: TestRegisterUnauthenticated (0.00s)
=== RUN   TestRegisterUnauthenticatedUnavailableUsername
[GIN] 2016/09/04 - 10:41:55 | 400 |     121.615µs |  |   POST    /u/register
--- PASS: TestRegisterUnauthenticatedUnavailableUsername (0.00s)
=== RUN   TestShowLoginPageUnauthenticated
[GIN] 2016/09/04 - 10:41:55 | 200 |     139.911µs |  |   GET     /u/login
--- PASS: TestShowLoginPageUnauthenticated (0.00s)
=== RUN   TestLoginUnauthenticated
[GIN] 2016/09/04 - 10:41:55 | 200 |     105.532µs |  |   POST    /u/login
--- PASS: TestLoginUnauthenticated (0.00s)
=== RUN   TestLoginUnauthenticatedIncorrectCredentials
[GIN] 2016/09/04 - 10:41:55 | 400 |     138.814µs |  |   POST    /u/login
--- PASS: TestLoginUnauthenticatedIncorrectCredentials (0.00s)
=== RUN   TestGetAllArticles
--- PASS: TestGetAllArticles (0.00s)
=== RUN   TestGetArticleByID
--- PASS: TestGetArticleByID (0.00s)
=== RUN   TestCreateNewArticle
--- PASS: TestCreateNewArticle (0.00s)
=== RUN   TestValidUserRegistration
--- PASS: TestValidUserRegistration (0.00s)
=== RUN   TestInvalidUserRegistration
--- PASS: TestInvalidUserRegistration (0.00s)
=== RUN   TestUsernameAvailability
--- PASS: TestUsernameAvailability (0.00s)
=== RUN   TestUserValidity
--- PASS: TestUserValidity (0.00s)
PASS
ok      github.com/demo-apps/go-gin-app 0.011s

Now that we've implemented quite a bit of functionality in the application, let's add some authorization checks to some of the functionality.

results matching ""

    No results matching ""