-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathuser.go
58 lines (51 loc) · 1.17 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
package socialnetworkingservice
import "sync"
type User struct {
ID string
Name string
Email string
Password string
ProfilePicture string
Bio string
friends map[string]bool
posts []*Post
mu sync.RWMutex
}
func NewUser(id, name, email, password, profilePicture, bio string) *User {
return &User{
ID: id,
Name: name,
Email: email,
Password: password,
ProfilePicture: profilePicture,
Bio: bio,
friends: make(map[string]bool),
posts: make([]*Post, 0),
}
}
func (u *User) AddFriend(friendID string) {
u.mu.Lock()
defer u.mu.Unlock()
u.friends[friendID] = true
}
func (u *User) AddPost(post *Post) {
u.mu.Lock()
defer u.mu.Unlock()
u.posts = append(u.posts, post)
}
func (u *User) GetFriends() []string {
u.mu.RLock()
defer u.mu.RUnlock()
friends := make([]string, 0, len(u.friends))
for friendID := range u.friends {
friends = append(friends, friendID)
}
return friends
}
func (u *User) GetPosts() []*Post {
u.mu.RLock()
defer u.mu.RUnlock()
posts := make([]*Post, len(u.posts))
copy(posts, u.posts)
return posts
}