mirror of
https://github.com/yanislav-igonin/micrach
synced 2025-04-20 03:40:33 +03:00
connect create thread controller
This commit is contained in:
parent
733f3cae5a
commit
7703154fd1
@ -1,15 +1,20 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/dchest/captcha"
|
"github.com/dchest/captcha"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
|
||||||
"micrach/config"
|
"micrach/config"
|
||||||
|
"micrach/db"
|
||||||
"micrach/repositories"
|
"micrach/repositories"
|
||||||
|
"micrach/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetThreads(c *fiber.Ctx) error {
|
func GetThreads(c *fiber.Ctx) error {
|
||||||
@ -83,146 +88,132 @@ func GetThread(c *fiber.Ctx) error {
|
|||||||
return c.Render("pages/thread", htmlData)
|
return c.Render("pages/thread", htmlData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func CreateThread(c *gin.Context) {
|
func CreateThread(c *fiber.Ctx) error {
|
||||||
// form, err := c.MultipartForm()
|
form, err := c.MultipartForm()
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// // TODO: dat shit crashes if no fields in request
|
// TODO: dat shit crashes if no fields in request
|
||||||
// text := form.Value["text"][0]
|
text := form.Value["text"][0]
|
||||||
// title := form.Value["title"][0]
|
title := form.Value["title"][0]
|
||||||
// filesInRequest := form.File["files"]
|
filesInRequest := form.File["files"]
|
||||||
// validationErrorMessage := Utils.ValidatePost(title, text, filesInRequest)
|
validationErrorMessage := utils.ValidatePost(title, text, filesInRequest)
|
||||||
// if validationErrorMessage != "" {
|
if validationErrorMessage != "" {
|
||||||
// errorHtmlData := Repositories.BadRequestHtmlData{
|
errorHtmlData := repositories.BadRequestHtmlData{
|
||||||
// Message: validationErrorMessage,
|
Message: validationErrorMessage,
|
||||||
// }
|
}
|
||||||
// c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
|
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// if Config.App.IsCaptchaActive {
|
if config.App.IsCaptchaActive {
|
||||||
// captchaID := form.Value["captchaId"][0]
|
captchaID := form.Value["captchaId"][0]
|
||||||
// captchaString := form.Value["captcha"][0]
|
captchaString := form.Value["captcha"][0]
|
||||||
// isCaptchaValid := captcha.VerifyString(captchaID, captchaString)
|
isCaptchaValid := captcha.VerifyString(captchaID, captchaString)
|
||||||
// if !isCaptchaValid {
|
if !isCaptchaValid {
|
||||||
// errorHtmlData := Repositories.BadRequestHtmlData{
|
errorHtmlData := repositories.BadRequestHtmlData{
|
||||||
// Message: Repositories.InvalidCaptchaErrorMessage,
|
Message: repositories.InvalidCaptchaErrorMessage,
|
||||||
// }
|
}
|
||||||
// c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
|
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
||||||
// return
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// conn, err := Db.Pool.Acquire(context.TODO())
|
conn, err := db.Pool.Acquire(context.TODO())
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// return
|
}
|
||||||
// }
|
defer conn.Release()
|
||||||
// defer conn.Release()
|
|
||||||
|
|
||||||
// threadsCount, err := Repositories.Posts.GetThreadsCount()
|
threadsCount, err := repositories.Posts.GetThreadsCount()
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// if threadsCount >= Config.App.ThreadsMaxCount {
|
if threadsCount >= config.App.ThreadsMaxCount {
|
||||||
// oldestThreadUpdatedAt, err := Repositories.Posts.GetOldestThreadUpdatedAt()
|
oldestThreadUpdatedAt, err := repositories.Posts.GetOldestThreadUpdatedAt()
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// return
|
}
|
||||||
// }
|
err = repositories.Posts.ArchiveThreadsFrom(oldestThreadUpdatedAt)
|
||||||
// err = Repositories.Posts.ArchiveThreadsFrom(oldestThreadUpdatedAt)
|
if err != nil {
|
||||||
// if err != nil {
|
log.Println("error:", err)
|
||||||
// log.Println("error:", err)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
}
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// tx, err := conn.Begin(context.TODO())
|
tx, err := conn.Begin(context.TODO())
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// return
|
}
|
||||||
// }
|
defer tx.Rollback(context.TODO())
|
||||||
// defer tx.Rollback(context.TODO())
|
|
||||||
|
|
||||||
// post := Repositories.Post{
|
post := repositories.Post{
|
||||||
// IsParent: true,
|
IsParent: true,
|
||||||
// Title: title,
|
Title: title,
|
||||||
// Text: text,
|
Text: text,
|
||||||
// IsSage: false,
|
IsSage: false,
|
||||||
// }
|
}
|
||||||
// threadID, err := Repositories.Posts.CreateInTx(tx, post)
|
threadID, err := repositories.Posts.CreateInTx(tx, post)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// err = Utils.CreateThreadFolder(threadID)
|
err = utils.CreateThreadFolder(threadID)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// for _, fileInRequest := range filesInRequest {
|
for _, fileInRequest := range filesInRequest {
|
||||||
// file := Repositories.File{
|
file := repositories.File{
|
||||||
// PostID: threadID,
|
PostID: threadID,
|
||||||
// Name: fileInRequest.Filename,
|
Name: fileInRequest.Filename,
|
||||||
// // image/jpeg -> jpeg
|
// image/jpeg -> jpeg
|
||||||
// Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1],
|
Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1],
|
||||||
// Size: int(fileInRequest.Size),
|
Size: int(fileInRequest.Size),
|
||||||
// }
|
}
|
||||||
|
|
||||||
// fileID, err := Repositories.Files.CreateInTx(tx, file)
|
fileID, err := repositories.Files.CreateInTx(tx, file)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// path := filepath.Join(
|
path := filepath.Join(
|
||||||
// Utils.UPLOADS_DIR_PATH,
|
utils.UPLOADS_DIR_PATH,
|
||||||
// strconv.Itoa(threadID),
|
strconv.Itoa(threadID),
|
||||||
// "o",
|
"o",
|
||||||
// strconv.Itoa(fileID)+"."+file.Ext,
|
strconv.Itoa(fileID)+"."+file.Ext,
|
||||||
// )
|
)
|
||||||
// err = c.SaveUploadedFile(fileInRequest, path)
|
err = c.SaveFile(fileInRequest, path)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// return
|
}
|
||||||
// }
|
// creating thumbnail
|
||||||
// // creating thumbnail
|
thumbImg, err := utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
||||||
// thumbImg, err := Utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
if err != nil {
|
||||||
// if err != nil {
|
log.Println("error:", err)
|
||||||
// log.Println("error:", err)
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
}
|
||||||
// return
|
// saving thumbnail
|
||||||
// }
|
err = utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
||||||
// // saving thumbnail
|
if err != nil {
|
||||||
// err = Utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
log.Println("error:", err)
|
||||||
// if err != nil {
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// log.Println("error:", err)
|
}
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
}
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// tx.Commit(context.TODO())
|
tx.Commit(context.TODO())
|
||||||
|
|
||||||
// c.Redirect(http.StatusFound, "/"+strconv.Itoa(threadID))
|
return c.Redirect("/"+strconv.Itoa(threadID), fiber.StatusFound)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // Add new post in thread
|
// // Add new post in thread
|
||||||
// func UpdateThread(c *gin.Context) {
|
// func UpdateThread(c *gin.Context) {
|
||||||
|
4
main.go
4
main.go
@ -68,9 +68,7 @@ func main() {
|
|||||||
app.Static("/static", "./static")
|
app.Static("/static", "./static")
|
||||||
|
|
||||||
app.Get("/", controllers.GetThreads)
|
app.Get("/", controllers.GetThreads)
|
||||||
app.Post("/", func(c *fiber.Ctx) error {
|
app.Post("/", controllers.CreateThread)
|
||||||
return c.SendString("create thread")
|
|
||||||
})
|
|
||||||
app.Get("/:threadID", controllers.GetThread)
|
app.Get("/:threadID", controllers.GetThread)
|
||||||
app.Post("/threadID", func(c *fiber.Ctx) error {
|
app.Post("/threadID", func(c *fiber.Ctx) error {
|
||||||
return c.SendString("create post in thread")
|
return c.SendString("create post in thread")
|
||||||
|
Loading…
Reference in New Issue
Block a user