-
Notifications
You must be signed in to change notification settings - Fork 1
/
article.go
221 lines (179 loc) · 4.43 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package db
import (
"errors"
"io"
"log"
"net/http"
"gorm.io/gorm"
)
type Article struct {
gorm.Model
Title string `json:"title"`
Remote string `json:"remote"`
Content []byte
ParentRefer uint
Children []Article `json:"children" gorm:"foreignKey:ParentRefer"`
}
func PopulateArticles(resource string) (*Article, error) {
rootArticle := &Article{}
err := PopulateFromResource(rootArticle, resource)
if err != nil {
return nil, err
}
err = rootArticle.FetchContent(true)
if err != nil {
return nil, err
}
return rootArticle, err
}
func (article *Article) FetchContent(recursive bool) error {
log.Printf("retrieving %s\n", article.Remote)
resp, err := http.Get(article.Remote)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
article.Content = body
if !recursive {
goto skip_recursion
}
for i := range article.Children {
err = article.Children[i].FetchContent(true)
if err != nil {
return err
}
}
skip_recursion:
return nil
}
func ReadArticles(db *gorm.DB) (*Article, error) {
var articles []Article
err := db.Where("title = 'Início'").Find(&articles).Error
if err != nil {
return nil, err
} else if len(articles) <= 0 {
return nil, nil
}
rootArticle := &articles[0]
err = readAndPopulateArticleChildren(db, rootArticle)
if err != nil {
return nil, err
}
return rootArticle, nil
}
func readAndPopulateArticleChildren(db *gorm.DB, root *Article) error {
var children []Article
err := db.Where("parent_refer = ?", root.ID).Find(&children).Error
if err != nil {
return err
} else if len(children) <= 0 {
return nil
}
root.Children = children
for i := range root.Children {
readAndPopulateArticleChildren(db, &root.Children[i])
}
return nil
}
func (rootArticle *Article) Write(db *gorm.DB) error {
res := db.Create(rootArticle)
return res.Error
}
func (rootArticle *Article) FindByID(id uint) *Article {
if rootArticle.ID == id {
return rootArticle
}
for _, article := range rootArticle.Children {
foundArticle := article.FindByID(id)
if foundArticle != nil {
return foundArticle
}
}
return nil
}
func (rootArticle *Article) FindByTitle(title string) *Article {
if rootArticle.Title == title {
return rootArticle
}
for _, article := range rootArticle.Children {
foundArticle := article.FindByTitle(title)
if foundArticle != nil {
return foundArticle
}
}
return nil
}
func (rootArticle *Article) FindParent(child *Article) *Article {
if rootArticle.ID == child.ID {
return child
}
for i, article := range rootArticle.Children {
foundChild := article.FindParent(child)
// if child is returned, return again the parent instead.
if foundChild != nil {
if article.ID == child.ID {
return rootArticle
}
return &rootArticle.Children[i]
}
}
return nil
}
func (rootArticle *Article) FindNext(article *Article) (*Article, error) {
if len(article.Children) > 0 {
return &article.Children[0], nil
}
parentArticle := rootArticle.FindParent(article)
if parentArticle == nil {
return nil, errors.New("failed to find article's parent")
}
for i, child := range parentArticle.Children {
if child.ID == article.ID && (i+1) < len(parentArticle.Children) {
return &parentArticle.Children[i+1], nil
}
}
// parent is last child, go back
actualParent := rootArticle.FindParent(parentArticle)
if actualParent != nil {
for i, child := range actualParent.Children {
if child.ID == parentArticle.ID && (i+1) < len(actualParent.Children) {
return &actualParent.Children[i+1], nil
}
}
}
return nil, errors.New("failed to find next article")
}
func (rootArticle *Article) FindPrevious(article *Article) (*Article, error) {
parentArticle := rootArticle.FindParent(article)
if parentArticle == nil || len(parentArticle.Children) <= 0 {
return nil, errors.New("failed to find article's parent")
}
// first child, so return the parent
if parentArticle.Children[0].ID == article.ID {
return parentArticle, nil
}
for i, child := range parentArticle.Children[1:] {
if child.ID == article.ID {
return &parentArticle.Children[i], nil
}
}
return nil, errors.New("failed to find previous article")
}
func AssociateParentID(id uint) uint {
if id >= 7 && id <= 10 {
return 2
} else if id >= 11 && id <= 15 {
return 3
} else if id >= 16 && id <= 21 {
return 4
} else if id >= 22 && id <= 24 {
return 5
} else if id >= 25 && id <= 28 {
return 6
}
return 1
}