diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index e0f7b5f..92ca8ff 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -64,6 +64,53 @@ func GetThreads(c *gin.Context) { c.HTML(http.StatusOK, "index.html", htmlData) } +func GetThreadsApi(c *gin.Context) { + pageString := c.DefaultQuery("page", "1") + page, err := strconv.Atoi(pageString) + if err != 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.StatusInternalServerError, "500.html", nil) + return + } + count, err := Repositories.Posts.GetCount() + if err != nil { + log.Println("error:", err) + c.HTML(http.StatusInternalServerError, "500.html", nil) + return + } + + pagesCount := int(math.Ceil(float64(count) / 10)) + if page > pagesCount && count != 0 { + c.HTML(http.StatusNotFound, "404.html", nil) + return + } + + captchaID := captcha.New() + htmlData := Repositories.GetThreadsHtmlData{ + Threads: threads, + PagesCount: pagesCount, + Page: page, + FormData: Repositories.HtmlFormData{ + CaptchaID: captchaID, + }, + } + // c.HTML(http.StatusOK, "index.html", htmlData) + c.JSON(http.StatusOK, htmlData) +} + func GetThread(c *gin.Context) { threadIDString := c.Param("threadID") threadID, err := strconv.Atoi(threadIDString) diff --git a/main.go b/main.go index 4e67f2f..7e09e0b 100644 --- a/main.go +++ b/main.go @@ -69,6 +69,7 @@ func main() { router.Static("/static", "./static") router.GET("/", Controllers.GetThreads) router.POST("/", Controllers.CreateThread) + router.GET("/api", Controllers.GetThreadsApi) router.GET("/:threadID", Controllers.GetThread) router.POST("/:threadID", Controllers.UpdateThread) router.GET("/captcha/:captchaID", Controllers.GetCaptcha) diff --git a/repositories/structs.go b/repositories/structs.go index 51c963c..66fee40 100644 --- a/repositories/structs.go +++ b/repositories/structs.go @@ -46,9 +46,9 @@ type GetThreadHtmlData struct { // index.html type GetThreadsHtmlData struct { - Threads []Post - PagesCount int - Page int + Threads []Post `json:"threads"` + PagesCount int `json:"pagesCount"` + Page int `json:"page"` FormData HtmlFormData } diff --git a/templates/400.html b/templates/400.html index 2d52413..0057cda 100644 --- a/templates/400.html +++ b/templates/400.html @@ -2,11 +2,7 @@ {{ template "static" }} - - + {{ template "meta-tags-static" }} diff --git a/templates/404.html b/templates/404.html index cb21419..614a3d4 100644 --- a/templates/404.html +++ b/templates/404.html @@ -2,11 +2,7 @@ {{ template "static" }} - - + {{ template "meta-tags-static" }} diff --git a/templates/500.html b/templates/500.html index 63e5871..3215fa5 100644 --- a/templates/500.html +++ b/templates/500.html @@ -2,11 +2,7 @@ {{ template "static" }} - - + {{ template "meta-tags-static" }} diff --git a/templates/index.html b/templates/index.html index 0887520..11eb087 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,7 +4,6 @@ {{ template "static" }} - {{ template "meta-tags-static" }} diff --git a/templates/thread.html b/templates/thread.html index e16a246..153e65e 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -13,7 +13,6 @@

- ⤶ {{ if ne $FirstPost.Title "" }} {{$FirstPost.Title}} {{ else }}