-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathuser_role.go
More file actions
119 lines (97 loc) · 2.23 KB
/
Copy pathuser_role.go
File metadata and controls
119 lines (97 loc) · 2.23 KB
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
package postgresql
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/MarioCarrion/videos/2023/transaction-in-context/internal"
)
type UserRole struct {
conn *pgx.Conn
}
func NewUserRole(conn *pgx.Conn) *UserRole {
return &UserRole{
conn: conn,
}
}
func (u *UserRole) Insert(ctx context.Context, id uuid.UUID, roleIDs ...uuid.UUID) error {
tx, err := u.conn.Begin(ctx)
if err != nil {
return fmt.Errorf("Begin %w", err)
}
const sql = `INSERT INTO users_roles(user_id, role_id) VALUES ($1, $2)`
err = transaction(ctx, tx, func() error {
for _, roleID := range roleIDs {
_, err := tx.Exec(ctx, sql, &id, &roleID)
if err != nil {
return fmt.Errorf("Exec %w", err)
}
}
return nil
})
if err != nil {
return fmt.Errorf("transaction %w", err)
}
return nil
}
func (u *UserRole) Select(ctx context.Context, id uuid.UUID) (internal.User, error) {
const sql = `
SELECT
U.id AS user_id,
U.name AS user_name,
R.id AS role_id,
R.name AS role_name,
P.id AS permission_name,
P.type AS permission_type
FROM
users U
LEFT JOIN users_roles UR ON U.id = UR.user_id
LEFT JOIN roles R ON R.id = UR.role_id
LEFT JOIN permissions P ON R.id = P.role_id
WHERE
U.id = $1
`
rows, err := u.conn.Query(ctx, sql, id)
if err != nil {
return internal.User{}, fmt.Errorf("Select %w", err)
}
roles := make(map[uuid.UUID]internal.Role)
var (
userId uuid.UUID
userName *string
roleId uuid.UUID
roleName *string
permissionId uuid.UUID
permissionType string
)
_, err = pgx.ForEachRow(rows,
[]any{&userId, &userName, &roleId, &roleName, &permissionId, &permissionType},
func() error {
role, ok := roles[roleId]
if !ok {
role = internal.Role{
ID: roleId,
Name: *roleName,
}
}
role.Permissions = append(role.Permissions,
internal.Permission{
ID: permissionId,
RoleID: role.ID,
Type: internal.PermissionType(permissionType),
})
roles[roleId] = role
return nil
})
if userName == nil {
return internal.User{}, fmt.Errorf("user not found")
}
user := internal.User{
ID: userId,
Name: *userName,
}
for _, p := range roles {
user.Roles = append(user.Roles, p)
}
return user, nil
}