From 1323d8303262aa39041d61da8ce84792c2402a34 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Wed, 1 Sep 2021 19:23:39 +0300 Subject: [PATCH] refac: move sql stmt --- repositories/posts_repository.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/repositories/posts_repository.go b/repositories/posts_repository.go index 4a04083..15f8fd2 100644 --- a/repositories/posts_repository.go +++ b/repositories/posts_repository.go @@ -16,6 +16,12 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) { return PostsDb, nil } + conn, err := Db.Pool.Acquire(context.TODO()) + if err != nil { + return nil, err + } + defer conn.Release() + sql := ` SELECT id, title, text, created_at FROM posts @@ -26,12 +32,6 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) { 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 @@ -55,18 +55,18 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, 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()) if err != nil { return 0, err } 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 if p.IsParent { row = conn.QueryRow(