From 074ed9a7f0e4deacb399eb41f4da0f5eafd84bb4 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Wed, 13 Apr 2022 12:23:16 +0300 Subject: [PATCH] wip --- controllers/threads_controller.go | 60 ++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index d1aa8bb..194f4ee 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -99,12 +99,62 @@ func CreateThread(c *fiber.Ctx) error { text := form.Value["text"][0] title := form.Value["title"][0] filesInRequest := form.File["files"] - validationErrorMessage := utils.ValidatePost(title, text, filesInRequest) - if validationErrorMessage != "" { - errorHtmlData := repositories.BadRequestHtmlData{ - Message: validationErrorMessage, + // validationErrorMessage := utils.ValidatePost(title, text, filesInRequest) + // if validationErrorMessage != "" { + // errorHtmlData := repositories.BadRequestHtmlData{ + // Message: validationErrorMessage, + // } + // return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData) + // } + + validationErrors := utils.ValidatePost2("", text, filesInRequest) + if validationErrors != nil { + + pageString := c.Query("page", "1") + page, err := strconv.Atoi(pageString) + if err != nil { + return c.Status(fiber.StatusNotFound).Render("pages/404", nil) } - return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData) + + if page <= 0 { + return c.Status(fiber.StatusNotFound).Render("pages/404", nil) + } + + limit := 10 + offset := limit * (page - 1) + threads, err := repositories.Posts.Get(limit, offset) + if err != nil { + log.Println("error:", err) + return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil) + } + count, err := repositories.Posts.GetThreadsCount() + if err != nil { + log.Println("error:", err) + return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil) + } + + pagesCount := int(math.Ceil(float64(count) / 10)) + if page > pagesCount && count != 0 { + return c.Status(fiber.StatusNotFound).Render("pages/404", nil) + } + + htmlData := repositories.GetThreadsHtmlData{ + Threads: threads, + Pagination: repositories.HtmlPaginationData{ + PagesCount: pagesCount, + Page: page, + }, + FormData: repositories.HtmlFormData{ + CaptchaID: form.Value["captchaId"][0], + IsCaptchaActive: config.App.IsCaptchaActive, + Errors: *validationErrors, + Inputs: repositories.Inputs{ + Text: text, + // Files: filesInRequest, + }, + }, + } + return c.Render("pages/index", htmlData) } if config.App.IsCaptchaActive {