mirror of
https://github.com/yanislav-igonin/micrach
synced 2024-12-22 14:22:33 +03:00
feat: add get thread by id
This commit is contained in:
parent
01f25001cb
commit
ea8f3323ec
@ -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) {
|
||||
|
4
main.go
4
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")
|
||||
|
@ -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 {
|
||||
// }
|
||||
|
81
templates/thread.html
Normal file
81
templates/thread.html
Normal file
@ -0,0 +1,81 @@
|
||||
{{$FirstPost:= index . 0}}
|
||||
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>{{$FirstPost.Title}}</title>
|
||||
|
||||
<link
|
||||
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We"
|
||||
crossorigin="anonymous"
|
||||
>
|
||||
<script
|
||||
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj"
|
||||
crossorigin="anonymous">
|
||||
</script>
|
||||
<link
|
||||
href="/static/index.css"
|
||||
rel="stylesheet"
|
||||
>
|
||||
<link rel="icon" href="/static/favicon.ico"/>
|
||||
|
||||
<!-- Primary Meta Tags -->
|
||||
<meta name="title" content="{{$FirstPost.Title}}">
|
||||
<meta name="description" content="{{$FirstPost.Text}}">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
|
||||
<!-- Open Graph / Facebook -->
|
||||
<meta property="og:type" content="website">
|
||||
<meta property="og:url" content="https://micrach.igonin.dev/{{$FirstPost.ID}}">
|
||||
<meta property="og:title" content="{{$FirstPost.Title}}">
|
||||
<meta property="og:description" content="{{$FirstPost.Text}}">
|
||||
<meta property="og:image" content="https://memepedia.ru/wp-content/uploads/2018/03/ebanyy-rot-etogo-kazino.png">
|
||||
|
||||
<!-- Twitter -->
|
||||
<meta property="twitter:card" content="https://memepedia.ru/wp-content/uploads/2018/03/ebanyy-rot-etogo-kazino.png">
|
||||
<meta property="twitter:url" content="https://micrach.igonin.dev/{{$FirstPost.ID}}">
|
||||
<meta property="twitter:title" content="{{$FirstPost.Title}}">
|
||||
<meta property="twitter:description" content="{{$FirstPost.Text}}">
|
||||
<meta property="twitter:image" content="https://memepedia.ru/wp-content/uploads/2018/03/ebanyy-rot-etogo-kazino.png">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1 class="display-1 text-center">Welcome to Micrach</h1>
|
||||
|
||||
<div class="container">
|
||||
<form class="row row-cols-auto gy-4" action="/{{$FirstPost.ID}}" method="POST" enctype="multipart/form-data">
|
||||
<input class="form-control" id="postTitle" placeholder="Title" name="title">
|
||||
<textarea class="form-control" id="postText" rows="3" placeholder="Text" name="text"></textarea>
|
||||
<input class="form-control" type="file" id="formFileMultiple" multiple name="files">
|
||||
</form>
|
||||
|
||||
<div class="row row-cols-auto gy-4">
|
||||
{{range $Post := .}}
|
||||
<div class="col col-sm-6 col-md-4 col-lg-3">
|
||||
<div class="card">
|
||||
|
||||
{{ $length := len $Post.Files }} {{ if gt $length 0 }}
|
||||
{{$FirstFile := index $Post.Files 0}}
|
||||
<img
|
||||
src="{{$FirstFile.Name}}"
|
||||
class="card-img-top"
|
||||
alt="Uploaded picture"
|
||||
>
|
||||
{{end}}
|
||||
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">{{$Post.Title}}</h5>
|
||||
<p class="card-text">{{$Post.Text}}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user