-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocialnet.go
More file actions
157 lines (139 loc) · 4.35 KB
/
socialnet.go
File metadata and controls
157 lines (139 loc) · 4.35 KB
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
package socialnet
import (
"sort"
"time"
)
// PostStorage is an interface for
// storing and retrieving posts.
type PostStorage interface {
Create(Post) (Post, error)
Read(id string) (Post, error)
Update(id string, newPost Post) (Post, error)
Delete(id string) error
List(username string) ([]Post, error)
}
// Post is a post submission.
type Post struct {
ID string `json:"id" db:"id"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
Author string `json:"author" db:"username"`
UserID string `json:"-" db:"users_id"`
ImageURL string `json:"imageURL" db:"image_url"`
Title string `json:"title"`
Body string `json:"body"`
Likes []Like `json:"likes" db:"-"`
Comments []Comment `json:"comments" db:"-"`
}
// PostService stores and retrieves posts.
type PostService struct {
Store PostStorage
Like LikeStorage
Comment CommentStorage
}
// LikeStorage stores and retrieves post likes.
type LikeStorage interface {
Create(username, postID string) error
Delete(username, postID string) error
List(postID string) ([]Like, error)
}
// Like is a post's like.
type Like struct {
ID string `json:"-" db:"id"`
PostID string `json:"postID" db:"post_id"`
UserItem `json:"user"`
}
// CommentStorage stores and retrieves post comments.
type CommentStorage interface {
Create(username, postID, text string) error
Delete(username, postID string) error
List(postID string) ([]Comment, error)
}
// Comment is a post comment made by a user.
type Comment struct {
ID string `json:"id"`
CreatedAt string `json:"createdAt" db:"created_at"`
PostID string `json:"postID" db:"post_id"`
UserItem `json:"user"`
Text string `json:"text"`
}
// User is a social network user.
type User struct {
ID string `json:"-"`
Username string `json:"username"`
ImageURL string `json:"imageURL" db:"image_url"`
FirstName string `json:"firstName" db:"first_name"`
LastName string `json:"lastName" db:"last_name"`
Email string `json:"email"`
Posts []Post `json:"posts"`
Password string `json:"password"`
CreatedAt time.Time `json:"-" db:"created_at"`
UpdatedAt time.Time `json:"-" db:"updated_at"`
}
// UserService stores, and authenticates
// a user.
type UserService struct {
Store UserStorage
Auth UserAuth
Follow UserFollow
}
// UserStorage is an interface for
// storing and retrieving posts.
type UserStorage interface {
Create(User) (User, error)
Read(username string) (User, error)
Update(username string, newUsr User) (User, error)
Delete(username string) error
List() ([]User, error)
}
// UserAuth validates a user's credentials
type UserAuth interface {
CreateToken(username string) (token string, err error)
ValidateToken(token string) (username string, err error)
ValidateCreds(username, password string) error
}
// UserFollow updates follow relationships between users.
type UserFollow interface {
Follow(follower, followee string) error
Unfollow(follower, followee string) error
Followers(username string) ([]UserItem, error)
Following(username string) ([]UserItem, error)
}
// Profile is a public profile.
type Profile struct {
Username string `json:"username"`
ImageURL string `json:"imageURL" db:"image_url"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Posts []Post `json:"posts"`
Followers []UserItem `json:"followers" db:"-"`
Following []UserItem `json:"following" db:"-"`
IsFollower bool `json:"isFollower" db:"-"`
}
// Settings contain personal details.
type Settings struct {
Username string `json:"username"`
ImageURL string `json:"imageURL" db:"image_url"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
}
// UserItem is used in user list. i.e. a user's followers
type UserItem struct {
Username string `json:"username"`
ImageURL string `json:"imageURL" db:"image_url"`
FirstName string `json:"firstName" db:"first_name"`
LastName string `json:"lastName" db:"last_name"`
}
// Feed is a sortable collection
// of a user's following list posts.
type Feed interface {
sort.Interface
List() []FeedItem
}
// FeedItem is a user post
type FeedItem struct {
ProfileImageURL string `json:"imageURL"`
Post `json:"post"`
Liked bool `json:"liked" db:"-"`
}