Add repository method for deleting entity
This commit is contained in:
parent
2d43f62c1e
commit
2cfd175414
1 changed files with 24 additions and 0 deletions
|
|
@ -74,6 +74,30 @@ func (r *PgRepository) FindById(id string) (*Link, error) {
|
||||||
return entity, nil
|
return entity, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *PgRepository) DeleteById(id string) error {
|
||||||
|
ctx := context.Background()
|
||||||
|
tx, err := r.pool.Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := `
|
||||||
|
DELETE FROM links WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
_, err = tx.Exec(ctx, sql, id)
|
||||||
|
if err != nil {
|
||||||
|
_ = tx.Rollback(ctx)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = tx.Commit(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func mapRowToEntity(r pgx.Row) (*Link, error) {
|
func mapRowToEntity(r pgx.Row) (*Link, error) {
|
||||||
var entity Link
|
var entity Link
|
||||||
var urlStr string
|
var urlStr string
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue