mirror of
https://github.com/yanislav-igonin/micrach
synced 2025-04-18 19:00: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)
|
||||
}
|
||||
|
||||
// // Add new post in thread
|
||||
// func UpdateThread(c *gin.Context) {
|
||||
// threadIDString := c.Param("threadID")
|
||||
// threadID, err := strconv.Atoi(threadIDString)
|
||||
// if err != nil {
|
||||
// c.HTML(http.StatusNotFound, "500.html", nil)
|
||||
// return
|
||||
// }
|
||||
// Add new post in thread
|
||||
func UpdateThread(c *fiber.Ctx) error {
|
||||
threadID, err := c.ParamsInt("threadID")
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
||||
}
|
||||
|
||||
// isArchived, err := Repositories.Posts.GetIfThreadIsArchived(threadID)
|
||||
// if isArchived {
|
||||
// errorHtmlData := Repositories.BadRequestHtmlData{
|
||||
// Message: Repositories.ThreadIsArchivedErrorMessage,
|
||||
// }
|
||||
// c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
|
||||
// return
|
||||
// }
|
||||
// if err != nil {
|
||||
// log.Println("error:", err)
|
||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
||||
// return
|
||||
// }
|
||||
isArchived, err := repositories.Posts.GetIfThreadIsArchived(threadID)
|
||||
if isArchived {
|
||||
errorHtmlData := repositories.BadRequestHtmlData{
|
||||
Message: repositories.ThreadIsArchivedErrorMessage,
|
||||
}
|
||||
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
||||
}
|
||||
if err != nil {
|
||||
log.Println("error:", err)
|
||||
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||
}
|
||||
|
||||
// form, err := c.MultipartForm()
|
||||
// if err != nil {
|
||||
// log.Println("error:", err)
|
||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
||||
// return
|
||||
// }
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
log.Println("error:", err)
|
||||
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||
}
|
||||
|
||||
// // TODO: dat shit crashes if no fields in request
|
||||
// text := form.Value["text"][0]
|
||||
// filesInRequest := form.File["files"]
|
||||
// validationErrorMessage := Utils.ValidatePost("", text, filesInRequest)
|
||||
// if validationErrorMessage != "" {
|
||||
// errorHtmlData := Repositories.BadRequestHtmlData{
|
||||
// Message: validationErrorMessage,
|
||||
// }
|
||||
// c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
|
||||
// return
|
||||
// }
|
||||
// TODO: dat shit crashes if no fields in request
|
||||
text := form.Value["text"][0]
|
||||
filesInRequest := form.File["files"]
|
||||
validationErrorMessage := utils.ValidatePost("", text, filesInRequest)
|
||||
if validationErrorMessage != "" {
|
||||
errorHtmlData := repositories.BadRequestHtmlData{
|
||||
Message: validationErrorMessage,
|
||||
}
|
||||
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
||||
}
|
||||
|
||||
// if Config.App.IsCaptchaActive {
|
||||
// captchaID := form.Value["captchaId"][0]
|
||||
// captchaString := form.Value["captcha"][0]
|
||||
// isCaptchaValid := captcha.VerifyString(captchaID, captchaString)
|
||||
// if !isCaptchaValid {
|
||||
// errorHtmlData := Repositories.BadRequestHtmlData{
|
||||
// Message: Repositories.InvalidCaptchaErrorMessage,
|
||||
// }
|
||||
// c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
if config.App.IsCaptchaActive {
|
||||
captchaID := form.Value["captchaId"][0]
|
||||
captchaString := form.Value["captcha"][0]
|
||||
isCaptchaValid := captcha.VerifyString(captchaID, captchaString)
|
||||
if !isCaptchaValid {
|
||||
errorHtmlData := repositories.BadRequestHtmlData{
|
||||
Message: repositories.InvalidCaptchaErrorMessage,
|
||||
}
|
||||
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
||||
}
|
||||
}
|
||||
|
||||
// isSageField := form.Value["sage"]
|
||||
// var isSageString string
|
||||
// if len(isSageField) != 0 {
|
||||
// isSageString = isSageField[0]
|
||||
// }
|
||||
// isSage := isSageString == "on"
|
||||
isSageField := form.Value["sage"]
|
||||
var isSageString string
|
||||
if len(isSageField) != 0 {
|
||||
isSageString = isSageField[0]
|
||||
}
|
||||
isSage := isSageString == "on"
|
||||
|
||||
// conn, err := Db.Pool.Acquire(context.TODO())
|
||||
// if err != nil {
|
||||
// log.Println("error:", err)
|
||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
||||
// return
|
||||
// }
|
||||
// defer conn.Release()
|
||||
conn, err := db.Pool.Acquire(context.TODO())
|
||||
if err != nil {
|
||||
log.Println("error:", err)
|
||||
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||
|
||||
// tx, err := conn.Begin(context.TODO())
|
||||
// if err != nil {
|
||||
// log.Println("error:", err)
|
||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
||||
// return
|
||||
// }
|
||||
// defer tx.Rollback(context.TODO())
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
// if err != nil {
|
||||
// log.Println("error:", err)
|
||||
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
||||
// return
|
||||
// }
|
||||
// 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
|
||||
// }
|
||||
tx, err := conn.Begin(context.TODO())
|
||||
if err != nil {
|
||||
log.Println("error:", err)
|
||||
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||
|
||||
// postsCountInThread, err := Repositories.Posts.GetThreadPostsCount(threadID)
|
||||
// if err != nil {
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
}
|
||||
defer tx.Rollback(context.TODO())
|
||||
|
||||
// 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),
|
||||
// }
|
||||
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)
|
||||
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(
|
||||
// Utils.UPLOADS_DIR_PATH,
|
||||
// strconv.Itoa(threadID),
|
||||
// "o",
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
postsCountInThread, err := repositories.Posts.GetThreadPostsCount(threadID)
|
||||
if err != nil {
|
||||
log.Println("error:", err)
|
||||
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
||||
|
||||
// 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.Post("/", controllers.CreateThread)
|
||||
app.Get("/:threadID", controllers.GetThread)
|
||||
app.Post("/threadID", func(c *fiber.Ctx) error {
|
||||
return c.SendString("create post in thread")
|
||||
})
|
||||
app.Post("/:threadID", controllers.UpdateThread)
|
||||
app.Get("/captcha/:captchaID", controllers.GetCaptcha)
|
||||
|
||||
log.Println("app - online, port -", strconv.Itoa(config.App.Port))
|
||||
|
Loading…
Reference in New Issue
Block a user