-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
108 lines (89 loc) · 2.56 KB
/
search.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
package search
import (
"context"
"github.com/olympsis/models"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Service struct {
AuthCol *mongo.Collection
UserCol *mongo.Collection
Log *logrus.Logger
}
func NewSearchService(l *logrus.Logger, a *mongo.Collection, u *mongo.Collection) *Service {
return &Service{Log: l, AuthCol: a, UserCol: u}
}
func (s *Service) SearchUserByUUID(uuid string) (models.UserData, error) {
// context/filter
ctx := context.Background()
filter := bson.M{"uuid": uuid}
opts := options.FindOneOptions{}
// find and decode auth user data
var auth models.AuthUser
err := s.AuthCol.FindOne(ctx, filter).Decode(&auth)
if err != nil {
return models.UserData{}, err
}
// find and decode user metadata
var user models.User
err = s.UserCol.FindOne(ctx, filter, &opts).Decode(&user)
if err != nil {
return models.UserData{}, err
}
// create user data object
userData := models.UserData{
UUID: *auth.UUID,
Username: user.UserName,
FirstName: *auth.FirstName,
LastName: *auth.LastName,
ImageURL: user.ImageURL,
Visibility: user.Visibility,
DeviceTokens: user.DeviceTokens,
}
// if user visibility is public display this data if not then don't
if user.Visibility == "public" {
userData.Bio = user.Bio
userData.Clubs = user.Clubs
userData.Sports = user.Sports
}
return userData, nil
}
func (s *Service) SearchUserByUsername(name string) (models.UserData, error) {
// context/filter
ctx := context.Background()
filter := bson.M{"username": name}
opts := options.FindOneOptions{}
// find and decode user metadata
var user models.User
err := s.UserCol.FindOne(ctx, filter, &opts).Decode(&user)
if err != nil {
return models.UserData{}, err
}
filter = bson.M{"uuid": user.UUID}
// return only uuid, first name and last name
// find and decode auth user data
var auth models.AuthUser
err = s.AuthCol.FindOne(ctx, filter).Decode(&auth)
if err != nil {
return models.UserData{}, err
}
// create user data object
userData := models.UserData{
UUID: *auth.UUID,
Username: user.UserName,
FirstName: *auth.FirstName,
LastName: *auth.LastName,
ImageURL: user.ImageURL,
Visibility: user.Visibility,
DeviceTokens: user.DeviceTokens,
}
// if user visibility is public display this data if not then don't
if user.Visibility == "public" {
userData.Bio = user.Bio
userData.Clubs = user.Clubs
userData.Sports = user.Sports
}
return userData, nil
}