From 2a21b5b4b07536b9860bb7fcd7399258f8f4b7c1 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Fri, 10 Sep 2021 17:09:15 +0300 Subject: [PATCH] feat: add some checks to render 404 --- controllers/threads_controller.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 20fca75..38d3fba 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -15,29 +15,38 @@ func GetThreads(c *gin.Context) { pageString := c.DefaultQuery("page", "1") page, err := strconv.Atoi(pageString) if err != nil { - log.Println("error:", err) - c.HTML(http.StatusOK, "500.html", nil) + c.HTML(http.StatusNotFound, "404.html", nil) return } + + if page <= 0 { + c.HTML(http.StatusNotFound, "404.html", nil) + return + } + limit := 10 offset := limit * (page - 1) threads, err := Repositories.Posts.Get(limit, offset) if err != nil { log.Println("error:", err) - c.HTML(http.StatusOK, "500.html", nil) + c.HTML(http.StatusInternalServerError, "500.html", nil) return } count, err := Repositories.Posts.GetCount() if err != nil { log.Println("error:", err) - c.HTML(http.StatusOK, "500.html", nil) + c.HTML(http.StatusInternalServerError, "500.html", nil) return } - // TODO: if page > pagescount render 404 + pagesCount := int(math.Ceil(float64(count) / 10)) + if page > pagesCount { + c.HTML(http.StatusNotFound, "404.html", nil) + return + } data := Repositories.IndexPageData{ Threads: threads, - PagesCount: int(math.Ceil(float64(count) / 10)), + PagesCount: pagesCount, Page: page, } c.HTML(http.StatusOK, "index.html", data) @@ -47,17 +56,17 @@ func GetThread(c *gin.Context) { threadIDString := c.Param("threadID") threadID, err := strconv.Atoi(threadIDString) if err != nil { - c.HTML(http.StatusOK, "404.html", nil) + c.HTML(http.StatusNotFound, "404.html", nil) return } thread, err := Repositories.Posts.GetThreadByPostID(threadID) if err != nil { log.Println("error:", err) - c.HTML(http.StatusOK, "500.html", nil) + c.HTML(http.StatusInternalServerError, "500.html", nil) return } if thread == nil { - c.HTML(http.StatusOK, "404.html", nil) + c.HTML(http.StatusNotFound, "404.html", nil) return } c.HTML(http.StatusOK, "thread.html", thread)