This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
/
tCreateUser.go
139 lines (127 loc) · 2.81 KB
/
tCreateUser.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
package dgraph
import (
"context"
"encoding/json"
"time"
"github.com/dgraph-io/dgo/protos/api"
"github.com/pkg/errors"
"github.com/romshark/dgraph_graphql_go/store"
strerr "github.com/romshark/dgraph_graphql_go/store/errors"
)
// CreateUser creates a new user account and adds it to the global index
func (str *impl) CreateUser(
ctx context.Context,
creationTime time.Time,
email string,
displayName string,
passwordHash string,
) (
result store.User,
err error,
) {
result.Creation = creationTime
result.Email = email
result.DisplayName = displayName
result.Password = passwordHash
// Prepare
result.ID = store.NewID()
// Begin transaction
txn, close := str.txn(&err)
if err != nil {
return
}
defer close()
// Ensure no users with a similar email already exist
var qr struct {
ByID []struct {
UID string `json:"uid"`
} `json:"byId"`
ByEmail []struct {
UID string `json:"uid"`
} `json:"byEmail"`
ByDisplayName []struct {
UID string `json:"uid"`
} `json:"byDisplayName"`
}
err = txn.QueryVars(
ctx,
`query User(
$id: string,
$email: string,
$displayName: string
) {
byId(func: eq(User.id, $id)) { uid }
byEmail(func: eq(User.email, $email)) { uid }
byDisplayName(func: eq(User.displayName, $displayName)) { uid }
}`,
map[string]string{
"$email": email,
"$displayName": displayName,
},
&qr,
)
if err != nil {
return
}
if len(qr.ByID) > 0 {
err = errors.Errorf("duplicate User.id: %s", result.ID)
return
}
if len(qr.ByEmail) > 0 {
err = strerr.Newf(
strerr.ErrInvalidInput,
"%d users with a similar email already exist",
len(qr.ByEmail),
)
return
}
if len(qr.ByDisplayName) > 0 {
err = strerr.Newf(
strerr.ErrInvalidInput,
"%d users with a similar displayName already exist",
len(qr.ByDisplayName),
)
return
}
// Create user account
var newUserJSON []byte
newUserJSON, err = json.Marshal(struct {
ID string `json:"User.id"`
Email string `json:"User.email"`
DisplayName string `json:"User.displayName"`
Creation time.Time `json:"User.creation"`
Password string `json:"User.password"`
}{
ID: string(result.ID),
Email: email,
DisplayName: displayName,
Creation: creationTime,
Password: string(passwordHash),
})
if err != nil {
return
}
var userCreationMut map[string]string
userCreationMut, err = txn.Mutation(ctx, &api.Mutation{
SetJson: newUserJSON,
})
if err != nil {
return
}
result.UID = userCreationMut["blank-0"]
// Add the new account to the global Index
var newUsersIndexJSON []byte
newUsersIndexJSON, err = json.Marshal(struct {
UID UID `json:"users"`
}{
UID: UID{NodeID: result.UID},
})
if err != nil {
return
}
_, err = txn.Mutation(ctx, &api.Mutation{
SetJson: newUsersIndexJSON,
Set: nil,
})
return
}