-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_repository_postgres.go
147 lines (124 loc) · 3.09 KB
/
user_repository_postgres.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package repository
import (
"context"
"database/sql"
"github.com/lib/pq"
"github.com/tsmweb/auth-service/app/user"
"github.com/tsmweb/auth-service/infra/db"
"github.com/tsmweb/go-helper-api/cerror"
"time"
)
// userRepositoryPostgres implementation for user.Repository interface.
type userRepositoryPostgres struct {
dataBase db.Database
}
// NewUserRepositoryPostgres creates a new instance of user.Repository.
func NewUserRepositoryPostgres(db db.Database) user.Repository {
return &userRepositoryPostgres{dataBase: db}
}
// Get returns the user by id.
func (r *userRepositoryPostgres) Get(ctx context.Context, ID string) (*user.User, error) {
stmt, err := r.dataBase.DB().PrepareContext(ctx, `
SELECT u.id,
u.name,
u.lastname,
u.created_at,
COALESCE(u.updated_at, u.created_at, u.updated_at) AS updated_at
FROM "user" u WHERE u.id = $1`)
if err != nil {
return nil, err
}
defer stmt.Close()
var user user.User
err = stmt.QueryRowContext(ctx, ID).
Scan(&user.ID,
&user.Name,
&user.LastName,
&user.CreatedAt,
&user.UpdatedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, cerror.ErrNotFound
}
return nil, err
}
return &user, nil
}
// Create new user in the data base.
func (r *userRepositoryPostgres) Create(ctx context.Context, user *user.User) error {
txn, err := r.dataBase.DB().Begin()
if err != nil {
return err
}
stmt, err := txn.PrepareContext(ctx, `
INSERT INTO "user"(id, name, lastname, created_at)
VALUES($1, $2, $3, $4)`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.ExecContext(ctx,
user.ID, user.Name, user.LastName, user.CreatedAt)
if err != nil {
txn.Rollback()
//"23505": "unique_violation"
if err.(*pq.Error).Code == pq.ErrorCode("23505") {
return cerror.ErrRecordAlreadyRegistered
}
return err
}
err = r.addLogin(ctx, txn, user.ID, user.Password, user.CreatedAt)
if err != nil {
txn.Rollback()
return err
}
if err = txn.Commit(); err != nil {
txn.Rollback()
return err
}
return nil
}
// Update user data in the data base.
func (r *userRepositoryPostgres) Update(ctx context.Context, user *user.User) (bool, error) {
txn, err := r.dataBase.DB().Begin()
if err != nil {
return false, err
}
stmt, err := txn.PrepareContext(ctx, `
UPDATE "user"
SET name = $1, lastname = $2, updated_at = $3
WHERE id = $4`)
if err != nil {
return false, err
}
defer stmt.Close()
result, err := stmt.ExecContext(ctx,
user.Name, user.LastName, user.UpdatedAt, user.ID)
if err != nil {
txn.Rollback()
return false, err
}
ra, _ := result.RowsAffected()
if ra != 1 {
txn.Rollback()
return false, nil
}
if err = txn.Commit(); err != nil {
txn.Rollback()
return false, err
}
return true, nil
}
func (r *userRepositoryPostgres) addLogin(ctx context.Context,
txn *sql.Tx, userID string, password string, createdAt time.Time) error {
stmt, err := txn.PrepareContext(ctx, `
INSERT INTO login(user_id, password, created_at)
VALUES($1, $2, $3)`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.ExecContext(ctx,
userID, password, createdAt)
return err
}