-
Notifications
You must be signed in to change notification settings - Fork 2
/
article.go
130 lines (111 loc) · 2.74 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
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
package gokit_realworld
import (
"errors"
"github.com/gosimple/slug"
"time"
)
var (
ErrArticleNotFound = Error{ENotFound, errors.New("article not found")}
ErrArticleAlreadyExists = Error{EConflict, errors.New("article already exists")}
)
type Favorites map[int64]struct{}
func (ff Favorites) FavoritedBy(id int64) bool {
_, ok := ff[id]
return ok
}
type Tags map[string]Tag
func (tt Tags) HasTag(t string) bool {
_, ok := tt[t]
return ok
}
func (tt Tags) TagsList() (tagList []string) {
tagList = make([]string, 0)
for k, _ := range tt {
tagList = append(tagList, k)
}
return
}
type Comments map[int64]Comment
type Article struct {
ID int64
Slug string
Title string
Description string
Body string
Author User
Comments Comments
Favorites Favorites
Tags Tags
CreatedAt time.Time
UpdatedAt time.Time
}
func (a Article) MakeSlug() string {
return slug.Make(a.Title)
}
func (a Article) Favorited(id int64) bool {
if a.Favorites == nil {
return false
}
if _, ok := a.Favorites[id]; ok {
return true
}
return false
}
type ListRequest struct {
Tag string
AuthorID int64
FavoriterID int64
Offset int
Limit int
}
type FeedRequest struct {
UserID int64
FollowingIDs []int64
Limit int
Offset int
}
type ArticleService interface {
Create(a Article) (*Article, error)
Update(slug string, a Article) (*Article, error)
Get(a Article) (*Article, error)
List(r ListRequest) ([]*Article, int, error)
Feed(r FeedRequest) ([]*Article, int, error)
Delete(a Article) error
Favorite(a Article, u User) (*Article, error)
Unfavorite(a Article, u User) (*Article, error)
AddComment(c Comment) (*Comment, error)
DeleteComment(c Comment) error
Comments(a Article) ([]*Comment, error)
Tags() ([]*Tag, error)
}
type ArticleRepo interface {
Get(slug string) (*Article, error)
List(offset, limit int) ([]*Article, int, error)
ListByTag(tag string, offset, limit int) ([]*Article, int, error)
ListByAuthorID(id int64, offset, limit int) ([]*Article, int, error)
ListByFavoriterID(id int64, offset, limit int) ([]*Article, int, error)
Feed(req FeedRequest) ([]*Article, int, error)
Create(u Article) (*Article, error)
Update(slug string, u Article) (*Article, error)
Delete(u Article) error
AddFavorite(a Article, u User) (*Article, error)
RemoveFavorite(a Article, u User) (*Article, error)
AddComment(c Comment) (*Comment, error)
DeleteComment(c Comment) error
Comments(a Article) ([]*Comment, error)
Tags() ([]*Tag, error)
}
type Comment struct {
ID int64
Article Article
ArticleID int64
UserID int64
Body string
CreatedAt time.Time
UpdatedAt time.Time
}
type Tag struct {
ID int64
Tag string
Articles []Article
}