Update link repository to use DB pool directly
This commit is contained in:
parent
8a23425826
commit
ed027a03ad
1 changed files with 5 additions and 57 deletions
|
|
@ -28,23 +28,13 @@ func (r *PgRepository) Save(link *Link) error {
|
|||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
defer cancel()
|
||||
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sql := `
|
||||
INSERT INTO links (id, name, redirect_url, creation_time)
|
||||
VALUES ($1, $2, $3, $4::timestamp)
|
||||
`
|
||||
|
||||
_, err = tx.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.CreationTime.Format("2006-01-02 15:04:05"))
|
||||
_, err := r.pool.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.CreationTime.Format("2006-01-02 15:04:05"))
|
||||
if err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -55,31 +45,21 @@ func (r *PgRepository) FindById(id string) (*Link, error) {
|
|||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
defer cancel()
|
||||
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sql := `
|
||||
SELECT id, name, redirect_url, creation_time
|
||||
FROM links
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
entity, err := mapRowToEntity(tx.QueryRow(ctx, sql, id))
|
||||
entity, err := mapRowToEntity(r.pool.QueryRow(ctx, sql, id))
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
} else {
|
||||
_ = tx.Rollback(ctx)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entity, nil
|
||||
}
|
||||
|
||||
|
|
@ -94,11 +74,6 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
|
|||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
defer cancel()
|
||||
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sql := `
|
||||
SELECT id, name, redirect_url, creation_time
|
||||
FROM links
|
||||
|
|
@ -106,9 +81,8 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
|
|||
OFFSET $2
|
||||
`
|
||||
|
||||
rows, err := tx.Query(ctx, sql, limit, offset)
|
||||
rows, err := r.pool.Query(ctx, sql, limit, offset)
|
||||
if err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
|
@ -117,7 +91,6 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
|
|||
for rows.Next() {
|
||||
link, err := mapRowToEntity(rows)
|
||||
if err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -125,11 +98,6 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
|
|||
}
|
||||
|
||||
if err = rows.Err(); err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -144,24 +112,14 @@ func (r *PgRepository) Update(link *Link) error {
|
|||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
defer cancel()
|
||||
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sql := `
|
||||
UPDATE links
|
||||
SET name = $1, redirect_url = $2
|
||||
WHERE id = $3
|
||||
`
|
||||
|
||||
_, err = tx.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Id)
|
||||
_, err := r.pool.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Id)
|
||||
if err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -172,22 +130,12 @@ func (r *PgRepository) DeleteById(id string) error {
|
|||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
defer cancel()
|
||||
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sql := `
|
||||
DELETE FROM links WHERE id = $1
|
||||
`
|
||||
|
||||
_, err = tx.Exec(ctx, sql, id)
|
||||
_, err := r.pool.Exec(ctx, sql, id)
|
||||
if err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue