feat: add some checks to render 404

This commit is contained in:
Yanislav Igonin 2021-09-10 17:09:15 +03:00
parent 4123a4a04a
commit 2a21b5b4b0

View File

@ -15,29 +15,38 @@ func GetThreads(c *gin.Context) {
pageString := c.DefaultQuery("page", "1") pageString := c.DefaultQuery("page", "1")
page, err := strconv.Atoi(pageString) page, err := strconv.Atoi(pageString)
if err != nil { if err != nil {
log.Println("error:", err) c.HTML(http.StatusNotFound, "404.html", nil)
c.HTML(http.StatusOK, "500.html", nil)
return return
} }
if page <= 0 {
c.HTML(http.StatusNotFound, "404.html", nil)
return
}
limit := 10 limit := 10
offset := limit * (page - 1) offset := limit * (page - 1)
threads, err := Repositories.Posts.Get(limit, offset) threads, err := Repositories.Posts.Get(limit, offset)
if err != nil { if err != nil {
log.Println("error:", err) log.Println("error:", err)
c.HTML(http.StatusOK, "500.html", nil) c.HTML(http.StatusInternalServerError, "500.html", nil)
return return
} }
count, err := Repositories.Posts.GetCount() count, err := Repositories.Posts.GetCount()
if err != nil { if err != nil {
log.Println("error:", err) log.Println("error:", err)
c.HTML(http.StatusOK, "500.html", nil) c.HTML(http.StatusInternalServerError, "500.html", nil)
return 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{ data := Repositories.IndexPageData{
Threads: threads, Threads: threads,
PagesCount: int(math.Ceil(float64(count) / 10)), PagesCount: pagesCount,
Page: page, Page: page,
} }
c.HTML(http.StatusOK, "index.html", data) c.HTML(http.StatusOK, "index.html", data)
@ -47,17 +56,17 @@ func GetThread(c *gin.Context) {
threadIDString := c.Param("threadID") threadIDString := c.Param("threadID")
threadID, err := strconv.Atoi(threadIDString) threadID, err := strconv.Atoi(threadIDString)
if err != nil { if err != nil {
c.HTML(http.StatusOK, "404.html", nil) c.HTML(http.StatusNotFound, "404.html", nil)
return return
} }
thread, err := Repositories.Posts.GetThreadByPostID(threadID) thread, err := Repositories.Posts.GetThreadByPostID(threadID)
if err != nil { if err != nil {
log.Println("error:", err) log.Println("error:", err)
c.HTML(http.StatusOK, "500.html", nil) c.HTML(http.StatusInternalServerError, "500.html", nil)
return return
} }
if thread == nil { if thread == nil {
c.HTML(http.StatusOK, "404.html", nil) c.HTML(http.StatusNotFound, "404.html", nil)
return return
} }
c.HTML(http.StatusOK, "thread.html", thread) c.HTML(http.StatusOK, "thread.html", thread)