-
Notifications
You must be signed in to change notification settings - Fork 0
/
owner.go
51 lines (43 loc) · 1.15 KB
/
owner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package postgres
import (
"context"
"errors"
"fmt"
"sync"
"github.com/4aykovski/effective_mobile_test_task/internal/model"
"github.com/4aykovski/effective_mobile_test_task/internal/repository"
"github.com/4aykovski/effective_mobile_test_task/pkg/database/postgres"
"github.com/lib/pq"
)
type OwnerRepository struct {
postgres *postgres.Postgres
}
func NewOwnerRepository(postgres *postgres.Postgres) *OwnerRepository {
return &OwnerRepository{
postgres: postgres,
}
}
func (r *OwnerRepository) InsertOwner(ctx context.Context, owner model.Owner) error {
stmt, err := r.postgres.Prepare(
`INSERT INTO owners (name, surname, patronymic)
VALUES ($1, $2, $3)`,
)
if err != nil {
return fmt.Errorf("failed to prepare add new owner statement: %w", err)
}
defer stmt.Close()
var mu sync.Mutex
mu.Lock()
_, err = stmt.ExecContext(ctx, owner.Name, owner.Surname, owner.Patronymic)
if err != nil {
var pqErr *pq.Error
if errors.As(err, &pqErr) {
if pqErr.Code.Name() == "unique_violation" {
return repository.ErrOwnerExists
}
}
return fmt.Errorf("failed to execute add new owner statement: %w", err)
}
mu.Unlock()
return nil
}