-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharticle.go
56 lines (48 loc) · 1.23 KB
/
article.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
package example
import (
"time"
)
// ArticleStatus is the status of article
type ArticleStatus int
const (
ArticleStatusDraft ArticleStatus = iota + 1
ArticleStatusOpen
)
// Author represents article's author
type Author struct {
ID int
Name string
}
// Article represents article
type Article struct {
ID int
Title string
Body string
AuthorID int
PublishScheduledAt time.Time
PublishedAt time.Time
Status ArticleStatus
LikeCount int
}
// Article represents list of article
type ArticleList []*Article
// SelectPublished returns only published articles
func (list ArticleList) SelectPublished() ArticleList {
var publishedArticles ArticleList
for _, a := range list {
if a.Status == ArticleStatusOpen && a.PublishScheduledAt.Before(time.Now()) {
publishedArticles = append(publishedArticles, a)
}
}
return publishedArticles
}
// SelectAuthoredBy returns only articles authored by given author's id
func (list ArticleList) SelectAuthoredBy(authorID int) ArticleList {
var authoredArticles ArticleList
for _, a := range list {
if a.AuthorID == authorID {
authoredArticles = append(authoredArticles, a)
}
}
return authoredArticles
}