feat: add thread creation

This commit is contained in:
Yanislav Igonin 2021-09-10 19:16:53 +03:00
parent 545a131801
commit 007f5f4883
6 changed files with 130 additions and 27 deletions

View File

@ -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) {

View File

@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -20,17 +20,20 @@
<div class="col col-sm-6 col-md-4 col-lg-3">
<div class="card">
{{ $length := len $Post.Files }} {{ if gt $length 0 }}
{{ $length := len $Post.Files }}
{{ if gt $length 0 }}
{{ $FirstFile := index $Post.Files 0 }}
<img
src="{{$FirstFile.Name}}"
src="/uploads/{{$Post.ID}}/{{$FirstFile.Name}}"
class="card-img-top"
alt="Uploaded picture"
>
{{ end }}
<div class="card-body">
{{ if ne $Post.Title "" }}
<h5 class="card-title">{{$Post.Title}}</h5>
{{ end }}
<p class="card-text">{{$Post.Text}}</p>
<a href="/{{$Post.ID}}" class="btn btn-outline-primary">Open</a>
</div>

View File

@ -20,14 +20,16 @@
{{ $length := len $Post.Files }} {{ if gt $length 0 }}
{{$FirstFile := index $Post.Files 0}}
<img
src="{{$FirstFile.Name}}"
src="/uploads/{{$Post.ID}}/{{$FirstFile.Name}}"
class="card-img-top"
alt="Uploaded picture"
>
{{end}}
<div class="card-body">
{{ if ne $Post.Title "" }}
<h5 class="card-title">{{$Post.Title}}</h5>
{{ end }}
<p class="card-text">{{$Post.Text}}</p>
</div>
</div>