-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathuser.go
More file actions
38 lines (28 loc) · 650 Bytes
/
Copy pathuser.go
File metadata and controls
38 lines (28 loc) · 650 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
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) {
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
}