feat: add get posts in repo

This commit is contained in:
Yanislav Igonin 2021-08-31 22:51:10 +03:00
parent b4a77d1b55
commit 8d050d2521

View File

@ -12,7 +12,46 @@ 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) {