-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
executable file
·65 lines (54 loc) · 1.54 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
package user
import (
firebase "firebase.google.com/go"
"google.golang.org/api/option"
"golang.org/x/net/context"
"cloud.google.com/go/firestore"
"errors"
//"google.golang.org/api/iterator"
)
type UserModel struct { // username as the path
Fullname string `firestore:"fullname"`
Email string `firestore:"email"`
Password string `firestore:"password"`
}
type UserLib struct {
Ctx context.Context
Users *firestore.CollectionRef
}
func InitUser(sdkLocation string, database string) (*UserLib, error) {
var ul UserLib
ul.Ctx = context.Background()
sa := option.WithCredentialsFile("/home/habibie/sdk.json") // This should be parameterized
app, err := firebase.NewApp(ul.Ctx, nil, sa)
if err != nil {
return nil, err
}
client, err := app.Firestore(ul.Ctx)
if err != nil {
return nil, err
}
ul.Users = client.Collection(database) // This should be parameterized
return &ul, nil
}
func (ul *UserLib) InsertOrUpdateData(username string, um UserModel) (error) {
u := ul.Users.Doc("user/accounts/"+username)
_, err := u.Set(ul.Ctx, um)
return err
}
func (ul *UserLib) GetData(username string) (*UserModel, error) {
u, err := ul.Users.Doc("user/accounts/"+username).Get(ul.Ctx)
if (u.Exists()) {
if (err != nil){
return nil, err
}
var um UserModel
d := u.Data()
um.Fullname, _ = d["fullname"].(string)
um.Email, _ = d["email"].(string)
um.Password, _ = d["password"].(string)
return &um, nil
} else {
return nil, errors.New("Not found")
}
}