This commit is contained in:
Yanislav Igonin 2022-01-26 19:37:14 +02:00
parent 25d91e5742
commit d74fb8a246
8 changed files with 54 additions and 20 deletions

View File

@ -64,6 +64,53 @@ func GetThreads(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", htmlData) 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) { func GetThread(c *gin.Context) {
threadIDString := c.Param("threadID") threadIDString := c.Param("threadID")
threadID, err := strconv.Atoi(threadIDString) threadID, err := strconv.Atoi(threadIDString)

View File

@ -69,6 +69,7 @@ func main() {
router.Static("/static", "./static") router.Static("/static", "./static")
router.GET("/", Controllers.GetThreads) router.GET("/", Controllers.GetThreads)
router.POST("/", Controllers.CreateThread) router.POST("/", Controllers.CreateThread)
router.GET("/api", Controllers.GetThreadsApi)
router.GET("/:threadID", Controllers.GetThread) router.GET("/:threadID", Controllers.GetThread)
router.POST("/:threadID", Controllers.UpdateThread) router.POST("/:threadID", Controllers.UpdateThread)
router.GET("/captcha/:captchaID", Controllers.GetCaptcha) router.GET("/captcha/:captchaID", Controllers.GetCaptcha)

View File

@ -46,9 +46,9 @@ type GetThreadHtmlData struct {
// index.html // index.html
type GetThreadsHtmlData struct { type GetThreadsHtmlData struct {
Threads []Post Threads []Post `json:"threads"`
PagesCount int PagesCount int `json:"pagesCount"`
Page int Page int `json:"page"`
FormData HtmlFormData FormData HtmlFormData
} }

View File

@ -2,11 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
{{ template "static" }} {{ template "static" }}
<link <link href="/static/styles/error-image.css" rel="stylesheet">
href="/static/styles/error-image.css"
rel="stylesheet"
>
{{ template "meta-tags-static" }} {{ template "meta-tags-static" }}
</head> </head>

View File

@ -2,11 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
{{ template "static" }} {{ template "static" }}
<link <link href="/static/styles/error-image.css" rel="stylesheet">
href="/static/styles/error-image.css"
rel="stylesheet"
>
{{ template "meta-tags-static" }} {{ template "meta-tags-static" }}
</head> </head>

View File

@ -2,11 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
{{ template "static" }} {{ template "static" }}
<link <link href="/static/styles/error-image.css" rel="stylesheet">
href="/static/styles/error-image.css"
rel="stylesheet"
>
{{ template "meta-tags-static" }} {{ template "meta-tags-static" }}
</head> </head>

View File

@ -4,7 +4,6 @@
<head> <head>
{{ template "static" }} {{ template "static" }}
<link href="/static/styles/index.css" rel="stylesheet"> <link href="/static/styles/index.css" rel="stylesheet">
{{ template "meta-tags-static" }} {{ template "meta-tags-static" }}
</head> </head>

View File

@ -13,7 +13,6 @@
<body> <body>
<a href="/" class="thread-title-link"> <a href="/" class="thread-title-link">
<h1 class="display-1 text-center thread-title"> <h1 class="display-1 text-center thread-title">
{{ if ne $FirstPost.Title "" }} {{ if ne $FirstPost.Title "" }}
{{$FirstPost.Title}} {{$FirstPost.Title}}
{{ else }} {{ else }}