mirror of
https://github.com/yanislav-igonin/micrach
synced 2025-04-20 03:40:33 +03:00
connect update thread controller
This commit is contained in:
parent
961e51ac9e
commit
c822b54579
@ -215,165 +215,154 @@ func CreateThread(c *fiber.Ctx) error {
|
|||||||
return c.Redirect("/"+strconv.Itoa(threadID), fiber.StatusFound)
|
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 *fiber.Ctx) error {
|
||||||
// threadIDString := c.Param("threadID")
|
threadID, err := c.ParamsInt("threadID")
|
||||||
// threadID, err := strconv.Atoi(threadIDString)
|
if err != nil {
|
||||||
// if err != nil {
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
||||||
// c.HTML(http.StatusNotFound, "500.html", nil)
|
}
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// isArchived, err := Repositories.Posts.GetIfThreadIsArchived(threadID)
|
isArchived, err := repositories.Posts.GetIfThreadIsArchived(threadID)
|
||||||
// if isArchived {
|
if isArchived {
|
||||||
// errorHtmlData := Repositories.BadRequestHtmlData{
|
errorHtmlData := repositories.BadRequestHtmlData{
|
||||||
// Message: Repositories.ThreadIsArchivedErrorMessage,
|
Message: repositories.ThreadIsArchivedErrorMessage,
|
||||||
// }
|
}
|
||||||
// c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
|
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
||||||
// return
|
}
|
||||||
// }
|
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
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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]
|
||||||
// filesInRequest := form.File["files"]
|
filesInRequest := form.File["files"]
|
||||||
// validationErrorMessage := Utils.ValidatePost("", text, filesInRequest)
|
validationErrorMessage := utils.ValidatePost("", 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
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// isSageField := form.Value["sage"]
|
isSageField := form.Value["sage"]
|
||||||
// var isSageString string
|
var isSageString string
|
||||||
// if len(isSageField) != 0 {
|
if len(isSageField) != 0 {
|
||||||
// isSageString = isSageField[0]
|
isSageString = isSageField[0]
|
||||||
// }
|
}
|
||||||
// isSage := isSageString == "on"
|
isSage := isSageString == "on"
|
||||||
|
|
||||||
// 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()
|
|
||||||
|
|
||||||
// tx, err := conn.Begin(context.TODO())
|
}
|
||||||
// if err != nil {
|
defer conn.Release()
|
||||||
// log.Println("error:", err)
|
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// defer tx.Rollback(context.TODO())
|
|
||||||
|
|
||||||
// if err != nil {
|
tx, err := conn.Begin(context.TODO())
|
||||||
// log.Println("error:", err)
|
if err != nil {
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
log.Println("error:", err)
|
||||||
// return
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// }
|
|
||||||
// post := Repositories.Post{
|
|
||||||
// IsParent: false,
|
|
||||||
// ParentID: &threadID,
|
|
||||||
// Title: "",
|
|
||||||
// Text: text,
|
|
||||||
// IsSage: isSage,
|
|
||||||
// }
|
|
||||||
// postID, err := Repositories.Posts.CreateInTx(tx, post)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Println("error:", err)
|
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// postsCountInThread, err := Repositories.Posts.GetThreadPostsCount(threadID)
|
}
|
||||||
// if err != nil {
|
defer tx.Rollback(context.TODO())
|
||||||
// log.Println("error:", err)
|
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// isBumpLimit := postsCountInThread >= Config.App.ThreadBumpLimit
|
|
||||||
// isThreadBumped := !isBumpLimit && !isSage && !post.IsParent
|
|
||||||
// if isThreadBumped {
|
|
||||||
// err = Repositories.Posts.BumpThreadInTx(tx, threadID)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Println("error:", err)
|
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for _, fileInRequest := range filesInRequest {
|
post := repositories.Post{
|
||||||
// file := Repositories.File{
|
IsParent: false,
|
||||||
// PostID: postID,
|
ParentID: &threadID,
|
||||||
// Name: fileInRequest.Filename,
|
Title: "",
|
||||||
// // image/jpeg -> jpeg
|
Text: text,
|
||||||
// Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1],
|
IsSage: isSage,
|
||||||
// Size: int(fileInRequest.Size),
|
}
|
||||||
// }
|
postID, err := repositories.Posts.CreateInTx(tx, post)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
|
|
||||||
// fileID, err := Repositories.Files.CreateInTx(tx, file)
|
}
|
||||||
// if err != nil {
|
|
||||||
// log.Println("error:", err)
|
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// path := filepath.Join(
|
postsCountInThread, err := repositories.Posts.GetThreadPostsCount(threadID)
|
||||||
// Utils.UPLOADS_DIR_PATH,
|
if err != nil {
|
||||||
// strconv.Itoa(threadID),
|
log.Println("error:", err)
|
||||||
// "o",
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
// strconv.Itoa(fileID)+"."+file.Ext,
|
|
||||||
// )
|
|
||||||
// err = c.SaveUploadedFile(fileInRequest, path)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Println("error:", err)
|
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// // creating thumbnail
|
|
||||||
// thumbImg, err := Utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Println("error:", err)
|
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// // saving thumbnail
|
|
||||||
// err = Utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Println("error:", err)
|
|
||||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// tx.Commit(context.TODO())
|
}
|
||||||
|
isBumpLimit := postsCountInThread >= config.App.ThreadBumpLimit
|
||||||
|
isThreadBumped := !isBumpLimit && !isSage && !post.IsParent
|
||||||
|
if isThreadBumped {
|
||||||
|
err = repositories.Posts.BumpThreadInTx(tx, threadID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
|
|
||||||
// c.Header("Refresh", "0")
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
for _, fileInRequest := range filesInRequest {
|
||||||
|
file := repositories.File{
|
||||||
|
PostID: postID,
|
||||||
|
Name: fileInRequest.Filename,
|
||||||
|
// image/jpeg -> jpeg
|
||||||
|
Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1],
|
||||||
|
Size: int(fileInRequest.Size),
|
||||||
|
}
|
||||||
|
|
||||||
|
fileID, err := repositories.Files.CreateInTx(tx, file)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
path := filepath.Join(
|
||||||
|
utils.UPLOADS_DIR_PATH,
|
||||||
|
strconv.Itoa(threadID),
|
||||||
|
"o",
|
||||||
|
strconv.Itoa(fileID)+"."+file.Ext,
|
||||||
|
)
|
||||||
|
err = c.SaveFile(fileInRequest, path)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
|
|
||||||
|
}
|
||||||
|
// creating thumbnail
|
||||||
|
thumbImg, err := utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
|
|
||||||
|
}
|
||||||
|
// saving thumbnail
|
||||||
|
err = utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit(context.TODO())
|
||||||
|
|
||||||
|
c.Append("Refresh", "0")
|
||||||
|
return c.SendStatus(fiber.StatusCreated)
|
||||||
|
}
|
||||||
|
4
main.go
4
main.go
@ -70,9 +70,7 @@ func main() {
|
|||||||
app.Get("/", controllers.GetThreads)
|
app.Get("/", controllers.GetThreads)
|
||||||
app.Post("/", controllers.CreateThread)
|
app.Post("/", controllers.CreateThread)
|
||||||
app.Get("/:threadID", controllers.GetThread)
|
app.Get("/:threadID", controllers.GetThread)
|
||||||
app.Post("/threadID", func(c *fiber.Ctx) error {
|
app.Post("/:threadID", controllers.UpdateThread)
|
||||||
return c.SendString("create post in thread")
|
|
||||||
})
|
|
||||||
app.Get("/captcha/:captchaID", controllers.GetCaptcha)
|
app.Get("/captcha/:captchaID", controllers.GetCaptcha)
|
||||||
|
|
||||||
log.Println("app - online, port -", strconv.Itoa(config.App.Port))
|
log.Println("app - online, port -", strconv.Itoa(config.App.Port))
|
||||||
|
Loading…
Reference in New Issue
Block a user