diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 38d3fba..95169fa 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -1,14 +1,18 @@ package controlers import ( + "context" "log" "math" "net/http" + "path/filepath" "strconv" "github.com/gin-gonic/gin" + Db "micrach/db" Repositories "micrach/repositories" + Utils "micrach/utils" ) func GetThreads(c *gin.Context) { @@ -40,7 +44,7 @@ func GetThreads(c *gin.Context) { } pagesCount := int(math.Ceil(float64(count) / 10)) - if page > pagesCount { + if page > pagesCount && len(threads) != 0 { c.HTML(http.StatusNotFound, "404.html", nil) return } @@ -75,25 +79,84 @@ func GetThread(c *gin.Context) { func CreateThread(c *gin.Context) { form, err := c.MultipartForm() if err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "error": "Problem uploading file!", - }) + log.Println("error:", err) + c.HTML(http.StatusInternalServerError, "500.html", nil) return } // TODO: dat shit crashes if no fields in request - // text := form.Value["text"][0] - // title := form.Value["title"][0] - // isSageString := form.Value["isSage"][0] - // isSage, err := strconv.ParseBool(isSageString) - // if err != nil { - // // TODO: validation error - // response := Dto.GetInternalServerErrorResponse() - // c.JSON(http.StatusInternalServerError, response) - // return - // } + text := form.Value["text"][0] + title := form.Value["title"][0] + filesInRequest := form.File["files"] - c.JSON(http.StatusOK, gin.H{"route": form}) + 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() + + 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()) + + post := Repositories.Post{ + IsParent: true, + Title: title, + Text: text, + IsSage: false, + } + postID, err := Repositories.Posts.CreateInTx(tx, post) + if err != nil { + log.Println("error:", err) + c.HTML(http.StatusInternalServerError, "500.html", nil) + return + } + + err = Utils.CreateThreadFolder(postID) + if err != nil { + log.Println("error:", err) + c.HTML(http.StatusInternalServerError, "500.html", nil) + return + } + + for _, fileInRequest := range filesInRequest { + path := filepath.Join( + Utils.UPLOADS_DIR_PATH, + strconv.Itoa(postID), + fileInRequest.Filename, + ) + log.Println(path) + file := Repositories.File{ + PostID: postID, + Name: fileInRequest.Filename, + Ext: fileInRequest.Header["Content-Type"][0], + Size: int(fileInRequest.Size), + } + + err := Repositories.Files.CreateInTx(tx, file) + if err != nil { + log.Println("error:", err) + c.HTML(http.StatusInternalServerError, "500.html", nil) + return + } + + err = c.SaveUploadedFile(fileInRequest, path) + if err != nil { + log.Println("error:", err) + c.HTML(http.StatusInternalServerError, "500.html", nil) + return + } + } + + tx.Commit(context.TODO()) + + c.Redirect(http.StatusFound, "/"+strconv.Itoa(postID)) } func UpdateThread(c *gin.Context) { diff --git a/db/db.go b/db/db.go index 48dbe3f..e3edb7e 100644 --- a/db/db.go +++ b/db/db.go @@ -13,7 +13,7 @@ var Pool *pgxpool.Pool func Init() { var err error - Pool, err = pgxpool.Connect(context.Background(), Config.Db.Url) + Pool, err = pgxpool.Connect(context.TODO(), Config.Db.Url) if err != nil { log.Println("database - offline") log.Panicln(err) diff --git a/repositories/files_repository.go b/repositories/files_repository.go index a218cba..3169408 100644 --- a/repositories/files_repository.go +++ b/repositories/files_repository.go @@ -69,3 +69,21 @@ func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) { return filesMapByPostId, nil } + +func (r *FilesRepository) CreateInTx(tx pgx.Tx, f File) error { + sql := ` + INSERT INTO files (post_id, name, ext, size) + VALUES ($1, $2, $3, $4) + ` + + row := tx.QueryRow( + context.TODO(), sql, f.PostID, f.Name, f.Ext, f.Size, + ) + + err := row.Scan() + if err != nil && err != pgx.ErrNoRows { + return err + } + + return nil +} diff --git a/repositories/posts_repository.go b/repositories/posts_repository.go index 254c4d9..fa3d4d9 100644 --- a/repositories/posts_repository.go +++ b/repositories/posts_repository.go @@ -163,12 +163,29 @@ func (r *PostsRepository) GetThreadByPostID(ID int) ([]Post, error) { return posts, nil } -// func (r *PostsRepository) IsThreadExists(ID int) ([]Post, error) { -// conn, err := Db.Pool.Acquire(context.TODO()) -// if err != nil { -// return nil, err -// } -// defer conn.Release() +func (r *PostsRepository) CreateInTx(tx pgx.Tx, p Post) (int, error) { + sql := ` + INSERT INTO posts (is_parent, parent_id, title, text, is_sage) + VALUES ($1, $2, $3, $4, $5) + RETURNING id + ` -// sql -// } + var row pgx.Row + if p.IsParent { + row = tx.QueryRow( + context.TODO(), sql, p.IsParent, nil, p.Title, p.Text, p.IsSage, + ) + } else { + row = tx.QueryRow( + context.TODO(), sql, p.IsParent, p.ParentID, p.Title, p.Text, p.IsSage, + ) + } + + createdPost := new(Post) + err := row.Scan(&createdPost.ID) + if err != nil { + return 0, err + } + + return createdPost.ID, nil +} diff --git a/templates/index.html b/templates/index.html index 1e7bc6b..9d2124d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -20,17 +20,20 @@
- {{ $length := len $Post.Files }} {{ if gt $length 0 }} + {{ $length := len $Post.Files }} + {{ if gt $length 0 }} {{ $FirstFile := index $Post.Files 0 }} Uploaded picture {{ end }}
+ {{ if ne $Post.Title "" }}
{{$Post.Title}}
+ {{ end }}

{{$Post.Text}}

Open
diff --git a/templates/thread.html b/templates/thread.html index f143b74..c752175 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -20,14 +20,16 @@ {{ $length := len $Post.Files }} {{ if gt $length 0 }} {{$FirstFile := index $Post.Files 0}} Uploaded picture {{end}}
+ {{ if ne $Post.Title "" }}
{{$Post.Title}}
+ {{ end }}

{{$Post.Text}}