diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 3ea083d..741d16d 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -1,7 +1,9 @@ package controlers import ( + "log" "net/http" + "strconv" "github.com/gin-gonic/gin" @@ -11,6 +13,8 @@ import ( func GetThreads(c *gin.Context) { threads, err := Repositories.Posts.Get(10, 10) if err != nil { + // TODO: наверное, тут 500 будет + log.Println("error:", err) c.JSON(http.StatusOK, gin.H{"error": true}) return } @@ -18,7 +22,22 @@ func GetThreads(c *gin.Context) { } func GetThread(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"route": "get thread"}) + threadIDString := c.Param("threadID") + threadID, err := strconv.Atoi(threadIDString) + if err != nil { + // TODO: тут 400 будет + log.Println("error:", err) + c.JSON(http.StatusOK, gin.H{"error": true}) + return + } + thread, err := Repositories.Posts.GetThreadByPostID(threadID) + if err != nil { + // TODO: наверное, тут 500 будет + log.Println("error:", err) + c.JSON(http.StatusOK, gin.H{"error": true}) + return + } + c.HTML(http.StatusOK, "thread.html", thread) } func CreateThread(c *gin.Context) { diff --git a/main.go b/main.go index 48ac460..ad98e1b 100644 --- a/main.go +++ b/main.go @@ -32,8 +32,8 @@ func main() { router.Static("/static", "./static") router.GET("/", Controllers.GetThreads) router.POST("/", Controllers.CreateThread) - router.GET("/:threadId", Controllers.GetThread) - router.POST("/:threadId", Controllers.UpdateThread) + router.GET("/:threadID", Controllers.GetThread) + router.POST("/:threadID", Controllers.UpdateThread) log.Println("port", Config.App.Port, "- online") log.Println("all systems nominal") diff --git a/repositories/posts_repository.go b/repositories/posts_repository.go index eb32a2c..e795969 100644 --- a/repositories/posts_repository.go +++ b/repositories/posts_repository.go @@ -101,5 +101,69 @@ func (r *PostsRepository) Create(p Post) (int, error) { return createdPost.ID, nil } +func (r *PostsRepository) GetThreadByPostID(ID int) ([]Post, error) { + conn, err := Db.Pool.Acquire(context.TODO()) + if err != nil { + return nil, err + } + defer conn.Release() + + sql := ` + SELECT + id, + title, + text, + is_sage, + created_at, + is_parent + FROM posts + WHERE + (id = $1 AND is_parent = true) OR parent_id = $1 + AND is_deleted = false + ORDER BY created_at ASC + ` + + rows, err := conn.Query(context.TODO(), sql, ID) + if err != nil { + return nil, err + } + if rows.Err() != nil { + return nil, err + } + + postsMap := make(map[int]Post) + var postIDs []int + for rows.Next() { + var post Post + err = rows.Scan( + &post.ID, + &post.Title, + &post.Text, + &post.IsSage, + &post.CreatedAt, + &post.IsParent, + ) + if err != nil { + return nil, err + } + + postsMap[post.ID] = post + postIDs = append(postIDs, post.ID) + } + + filesMap, err := Files.GetByPostIDs(postIDs) + if err != nil { + return nil, err + } + + var posts []Post + for _, postID := range postIDs { + post := postsMap[postID] + post.Files = filesMap[postID] + posts = append(posts, post) + } + + return posts, nil +} // func (r *PostsRepository) GetByID() int { // } diff --git a/templates/thread.html b/templates/thread.html new file mode 100644 index 0000000..8dd00a1 --- /dev/null +++ b/templates/thread.html @@ -0,0 +1,81 @@ +{{$FirstPost:= index . 0}} + + +
+ +{{$Post.Text}}
+