mirror of
https://github.com/yanislav-igonin/micrach
synced 2024-12-22 14:22:33 +03:00
feat: add get posts in repo
This commit is contained in:
parent
b4a77d1b55
commit
8d050d2521
@ -12,9 +12,48 @@ type PostsRepository struct{}
|
||||
var Posts PostsRepository
|
||||
|
||||
func (r *PostsRepository) Get(limit, offset int) ([]Post, error) {
|
||||
if Db.Pool == nil {
|
||||
return PostsDb, nil
|
||||
}
|
||||
|
||||
sql := `
|
||||
SELECT id, title, text, created_at
|
||||
FROM posts
|
||||
WHERE
|
||||
is_parent = true
|
||||
AND is_deleted = false
|
||||
ORDER BY updated_at DESC
|
||||
LIMIT $1
|
||||
`
|
||||
|
||||
conn, err := Db.Pool.Acquire(context.TODO())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
rows, err := conn.Query(context.TODO(), sql, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var posts []Post
|
||||
for rows.Next() {
|
||||
var post Post
|
||||
err = rows.Scan(&post.ID, &post.Title, &post.Text, &post.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posts = append(posts, post)
|
||||
}
|
||||
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
func (r *PostsRepository) Create(p Post) (int, error) {
|
||||
sql := `
|
||||
INSERT INTO posts (is_parent, parent_id, title, text, is_sage)
|
||||
|
Loading…
Reference in New Issue
Block a user