-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
168 lines (143 loc) · 3.62 KB
/
user.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package dal
import (
"encoding/json"
"fmt"
"time"
"github.com/VivaLaPanda/isle-api/api/models"
"github.com/dgraph-io/dgo"
)
// NewUser creates a new User based on the provided struct
func NewUser(db *dgo.Dgraph, user models.User, inviteCode string) (uid string, err error) {
// Handle validation, etc
// Making sure creation time is current
now := time.Now()
user.Joined = &now
if inviteCode == "" {
// TODO: Validate that user is an admin
} else {
invite, err := GetInvite(db, inviteCode)
if err != nil {
// Something is wrong with the invite, don't create the user
return "", err
}
user.InvitedBy = []*models.User{invite.CreatedBy}
}
// TODO: Delete the invite when it's used
return Mutator(db, user)
}
// NewRole creates a new User based on the provided struct
func NewRole(db *dgo.Dgraph, role models.Role) (uid string, err error) {
// Handle validation, etc
return Mutator(db, role)
}
// NewInvite creates a new invite owned by the current user
func NewInvite(db *dgo.Dgraph, invite models.Invite, currentUser models.User) (uid string, err error) {
// Handle validation, etc
invite.CreatedBy = &models.User{UID: currentUser.UID}
return Mutator(db, invite)
}
// GetInvite will return the struct representing a particular invite matching the given code
func GetInvite(db *dgo.Dgraph, inviteCode string) (resp models.Invite, err error) {
// Construct the query
const q = `
query GetInvite($match: string) {
invite(func: eq(code, $match)) @filter(type(Invite)) {
uid
code
createdBy {
uid
}
dgraph.type
}
}
`
jsonResp, err := SimpleQuery(db, q, inviteCode)
// Decode the response
var decode struct {
Invite []models.Invite
}
if err := json.Unmarshal(jsonResp, &decode); err != nil {
return resp, err
}
// if len(decode.Invite) != 1 {
// return resp, fmt.Errorf("Found %d invites, should only be 1", len(decode.Invite))
// }
// We got the expected 1 result, just return that
return decode.Invite[0], nil
}
// GetRole returns a struct with a summary of the information for a role
func GetRole(db *dgo.Dgraph, uid string) (resp models.Role, err error) {
// Construct the query
const q = `
query GetRole($match: string) {
role(func: uid($match)) @filter(type(User)) {
text
uid
dgraph.type
}
}
`
jsonResp, err := SimpleQuery(db, q, uid)
// Decode the response
var decode struct {
Role []models.Role
}
if err := json.Unmarshal(jsonResp, &decode); err != nil {
return resp, err
}
if len(decode.Role) != 1 {
return resp, fmt.Errorf("Query returned %d results, not 1 as expected. Did you query the wrong endpoint?", len(decode.Role))
}
// There's only ever one node
return decode.Role[0], nil
}
// GetUser returns a struct with a summary of the information for a user
func GetUser(db *dgo.Dgraph, uid string) (resp models.User, err error) {
// Construct the query
const q = `
query GetUser($match: string) {
user(func: uid($match)) @filter(type(User)) {
uid
name
email
joined
reputation
spent
aviImgUri
dgraph.type
role {
text
uid
}
invitedBy {
uid
name
aviImgUri
}
~author(orderasc: edited, first: 10) {
uid
title
body
created
imageUri
sentiment
score
tags
}
}
}
`
jsonResp, err := SimpleQuery(db, q, uid)
// Decode the response
var decode struct {
User []models.User
}
if err := json.Unmarshal(jsonResp, &decode); err != nil {
return resp, err
}
if len(decode.User) != 1 {
return resp, fmt.Errorf("Query returned %d results, not 1 as expected. Did you query the wrong endpoint?", len(decode.User))
}
// There's only ever one node
return decode.User[0], nil
}