refac: move sql stmt

This commit is contained in:
Yanislav Igonin 2021-09-01 19:23:39 +03:00
parent 1d93c5621d
commit 1323d83032

View File

@ -16,6 +16,12 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) {
return PostsDb, nil return PostsDb, nil
} }
conn, err := Db.Pool.Acquire(context.TODO())
if err != nil {
return nil, err
}
defer conn.Release()
sql := ` sql := `
SELECT id, title, text, created_at SELECT id, title, text, created_at
FROM posts FROM posts
@ -26,12 +32,6 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) {
LIMIT $1 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) rows, err := conn.Query(context.TODO(), sql, limit)
if err != nil { if err != nil {
return nil, err return nil, err
@ -55,18 +55,18 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) {
} }
func (r *PostsRepository) Create(p Post) (int, error) { func (r *PostsRepository) Create(p Post) (int, error) {
sql := `
INSERT INTO posts (is_parent, parent_id, title, text, is_sage)
VALUES ($1, $2, $3, $4, $5)
RETURNING id
`
conn, err := Db.Pool.Acquire(context.TODO()) conn, err := Db.Pool.Acquire(context.TODO())
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer conn.Release() defer conn.Release()
sql := `
INSERT INTO posts (is_parent, parent_id, title, text, is_sage)
VALUES ($1, $2, $3, $4, $5)
RETURNING id
`
var row pgx.Row var row pgx.Row
if p.IsParent { if p.IsParent {
row = conn.QueryRow( row = conn.QueryRow(