From 733f3cae5ae2ce7a96dc2311a596532dd963d1ab Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Thu, 7 Apr 2022 09:44:10 +0300 Subject: [PATCH] connect get thread by id controller --- controllers/threads_controller.go | 56 ++++++++++++++----------------- main.go | 4 +-- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 6f80d08..71ce415 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -53,39 +53,35 @@ func GetThreads(c *fiber.Ctx) error { IsCaptchaActive: config.App.IsCaptchaActive, }, } - return c.Status(fiber.StatusOK).Render("pages/index", htmlData) + return c.Render("pages/index", htmlData) } -// func GetThread(c *gin.Context) { -// threadIDString := c.Param("threadID") -// threadID, err := strconv.Atoi(threadIDString) -// if err != 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.StatusInternalServerError, "500.html", nil) -// return -// } -// if thread == nil { -// c.HTML(http.StatusNotFound, "404.html", nil) -// return -// } +func GetThread(c *fiber.Ctx) error { + threadID, err := c.ParamsInt("threadID") + if err != nil { + return c.Status(fiber.StatusNotFound).Render("pages/404", nil) + } + thread, err := repositories.Posts.GetThreadByPostID(threadID) + if err != nil { + log.Println("error:", err) + return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil) + } + if thread == nil { + return c.Status(fiber.StatusNotFound).Render("pages/404", nil) + } -// firstPost := thread[0] -// captchaID := captcha.New() -// htmlData := Repositories.GetThreadHtmlData{ -// Thread: thread, -// FormData: Repositories.HtmlFormData{ -// FirstPostID: firstPost.ID, -// CaptchaID: captchaID, -// IsCaptchaActive: Config.App.IsCaptchaActive, -// }, -// } -// c.HTML(http.StatusOK, "thread.html", htmlData) -// } + firstPost := thread[0] + captchaID := captcha.New() + htmlData := repositories.GetThreadHtmlData{ + Thread: thread, + FormData: repositories.HtmlFormData{ + FirstPostID: firstPost.ID, + CaptchaID: captchaID, + IsCaptchaActive: config.App.IsCaptchaActive, + }, + } + return c.Render("pages/thread", htmlData) +} // func CreateThread(c *gin.Context) { // form, err := c.MultipartForm() diff --git a/main.go b/main.go index 3ff437a..b5569a6 100644 --- a/main.go +++ b/main.go @@ -71,9 +71,7 @@ func main() { app.Post("/", func(c *fiber.Ctx) error { return c.SendString("create thread") }) - app.Get("/:threadID", func(c *fiber.Ctx) error { - return c.SendString("get thread by id") - }) + app.Get("/:threadID", controllers.GetThread) app.Post("/threadID", func(c *fiber.Ctx) error { return c.SendString("create post in thread") })