-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathuser.go
More file actions
48 lines (35 loc) · 844 Bytes
/
user.go
File metadata and controls
48 lines (35 loc) · 844 Bytes
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
package postgresql
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/MarioCarrion/videos/2023/transaction-in-context/internal"
)
type User struct {
conn *pgx.Conn
}
func NewUser(conn *pgx.Conn) *User {
return &User{
conn: conn,
}
}
func (u *User) Insert(ctx context.Context, name string) (internal.User, error) {
uq := userQueries{conn: u.conn}
return uq.Insert(ctx, name)
}
type userQueries struct {
conn DBTX
}
func (u *userQueries) Insert(ctx context.Context, name string) (internal.User, error) {
const sql = `INSERT INTO users(name) VALUES ($1) RETURNING id`
row := u.conn.QueryRow(ctx, sql, &name)
var id uuid.UUID
if err := row.Scan(&id); err != nil {
return internal.User{}, fmt.Errorf("Insert %w", err)
}
return internal.User{
ID: id,
Name: name,
}, nil
}