-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
identities.go
92 lines (72 loc) · 2.28 KB
/
identities.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
package idp
import (
"fmt"
"strings"
"github.com/neo4j/neo4j-go-driver/neo4j"
)
// You should never make these, please specialize with another label, see client.go or human.go
// func CreateIdentities(driver neo4j.Driver, identities []Identity) ([]Identity, error)
func FetchIdentities(tx neo4j.Transaction, iIdentities []Identity) (identities []Identity, err error) {
var result neo4j.Result
var cypher string
var params = make(map[string]interface{})
cypFilterIdentities := ""
if len(iIdentities) > 0 {
var ids []string
for _, identity := range iIdentities {
ids = append(ids, identity.Id)
}
cypFilterIdentities = ` AND i.id in split($ids, ",") `
params["ids"] = strings.Join(ids, ",")
}
cypher = fmt.Sprintf(`
MATCH (i:Identity) WHERE 1=1 %s RETURN i
`, cypFilterIdentities)
logCypher(cypher, params)
if result, err = tx.Run(cypher, params); err != nil {
return nil, err
}
for result.Next() {
record := result.Record()
identityNode := record.GetByIndex(0)
if identityNode != nil {
i := marshalNodeToIdentity(identityNode.(neo4j.Node))
identities = append(identities, i)
}
}
// Check if we encountered any error during record streaming
if err = result.Err(); err != nil {
return nil, err
}
return identities, nil
}
func SearchIdentities(tx neo4j.Transaction, iSearch string) (identities []Identity, err error) {
var result neo4j.Result
var cypher string
var params = make(map[string]interface{})
cypFilterIdentities := ""
if iSearch != "" {
cypFilterIdentities = ` AND ( i.name =~ $search or i.email =~ $search)`
params["search"] = fmt.Sprintf(`(?i).*%s.*`, iSearch)
}
cypher = fmt.Sprintf(`
MATCH (i:Identity) WHERE 1=1 %s RETURN i
`, cypFilterIdentities)
if result, err = tx.Run(cypher, params); err != nil {
return nil, err
}
logCypher(cypher, params)
for result.Next() {
record := result.Record()
identityNode := record.GetByIndex(0)
if identityNode != nil {
i := marshalNodeToIdentity(identityNode.(neo4j.Node))
identities = append(identities, i)
}
}
// Check if we encountered any error during record streaming
if err = result.Err(); err != nil {
return nil, err
}
return identities, nil
}