Add endpoint for updating link
This commit is contained in:
parent
9ae733e618
commit
8a23425826
3 changed files with 70 additions and 5 deletions
|
|
@ -16,6 +16,7 @@ type Repository interface {
|
|||
Save(link *Link) error
|
||||
FindById(id string) (*Link, error)
|
||||
FindAll(limit int, offset int) (Links, error)
|
||||
Update(link *Link) error
|
||||
DeleteById(id string) error
|
||||
}
|
||||
|
||||
|
|
@ -135,6 +136,38 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
|
|||
return links, nil
|
||||
}
|
||||
|
||||
func (r *PgRepository) Update(link *Link) error {
|
||||
if link.Id == "" {
|
||||
return errors.New("link ID must not be empty")
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *PgRepository) DeleteById(id string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
defer cancel()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue