-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathlinkedin_service.go
152 lines (128 loc) · 3.53 KB
/
linkedin_service.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
package linkedin
import (
"fmt"
"strings"
"sync"
"time"
)
type LinkedInService struct {
users map[string]*User
jobPostings map[string]*JobPosting
notifications map[string][]*Notification
mu sync.RWMutex
}
var (
instance *LinkedInService
once sync.Once
)
func GetLinkedInService() *LinkedInService {
once.Do(func() {
instance = &LinkedInService{
users: make(map[string]*User),
jobPostings: make(map[string]*JobPosting),
notifications: make(map[string][]*Notification),
}
})
return instance
}
func (s *LinkedInService) RegisterUser(user *User) {
s.mu.Lock()
defer s.mu.Unlock()
s.users[user.ID] = user
}
func (s *LinkedInService) LoginUser(email, password string) (*User, error) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, user := range s.users {
if user.Email == email && user.Password == password {
return user, nil
}
}
return nil, fmt.Errorf("invalid email or password")
}
func (s *LinkedInService) UpdateUserProfile(user *User) {
s.mu.Lock()
defer s.mu.Unlock()
s.users[user.ID] = user
}
func (s *LinkedInService) SendConnectionRequest(sender, receiver *User) {
connection := NewConnection(sender)
receiver.AddConnection(connection)
notification := NewNotification(
fmt.Sprintf("NOTIF-%d", time.Now().UnixNano()),
receiver,
NotificationTypeConnectionRequest,
fmt.Sprintf("New connection request from %s", sender.Name),
)
s.addNotification(receiver.ID, notification)
}
func (s *LinkedInService) AcceptConnectionRequest(user, connectionUser *User) {
user.AddConnection(NewConnection(connectionUser))
}
func (s *LinkedInService) SearchUsers(keyword string) []*User {
s.mu.RLock()
defer s.mu.RUnlock()
var results []*User
keyword = strings.ToLower(keyword)
for _, user := range s.users {
if strings.Contains(strings.ToLower(user.Name), keyword) {
results = append(results, user)
}
}
return results
}
func (s *LinkedInService) PostJobListing(jobPosting *JobPosting) {
s.mu.Lock()
s.jobPostings[jobPosting.ID] = jobPosting
s.mu.Unlock()
// Notify all users about new job posting
for _, user := range s.users {
notification := NewNotification(
fmt.Sprintf("NOTIF-%d", time.Now().UnixNano()),
user,
NotificationTypeJobPosting,
fmt.Sprintf("New job posting: %s", jobPosting.Title),
)
s.addNotification(user.ID, notification)
}
}
func (s *LinkedInService) SearchJobPostings(keyword string) []*JobPosting {
s.mu.RLock()
defer s.mu.RUnlock()
var results []*JobPosting
keyword = strings.ToLower(keyword)
for _, posting := range s.jobPostings {
if strings.Contains(strings.ToLower(posting.Title), keyword) ||
strings.Contains(strings.ToLower(posting.Description), keyword) {
results = append(results, posting)
}
}
return results
}
func (s *LinkedInService) SendMessage(sender, receiver *User, content string) {
message := NewMessage(
fmt.Sprintf("MSG-%d", time.Now().UnixNano()),
sender,
receiver,
content,
)
receiver.AddMessage(message, false)
sender.AddMessage(message, true)
notification := NewNotification(
fmt.Sprintf("NOTIF-%d", time.Now().UnixNano()),
receiver,
NotificationTypeMessage,
fmt.Sprintf("New message from %s", sender.Name),
)
s.addNotification(receiver.ID, notification)
}
func (s *LinkedInService) addNotification(userID string, notification *Notification) {
s.mu.Lock()
defer s.mu.Unlock()
s.notifications[userID] = append(s.notifications[userID], notification)
}
func (s *LinkedInService) GetNotifications(userID string) []*Notification {
s.mu.RLock()
defer s.mu.RUnlock()
return s.notifications[userID]
}