-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_manager.go
78 lines (69 loc) · 1.71 KB
/
content_manager.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
package main
import (
"sort"
"strings"
"time"
)
type ContentManager struct {
Contents []Content
}
func NewContentManager(contents []Content) *ContentManager {
return &ContentManager{Contents: contents}
}
func (cm *ContentManager) FilterByType(contentType ContentType) []Content {
var filtered []Content
for _, content := range cm.Contents {
if content.Type == contentType {
filtered = append(filtered, content)
}
}
return filtered
}
func (cm *ContentManager) FilterByDateRange(start, end time.Time) []Content {
var filtered []Content
for _, content := range cm.Contents {
if (content.Date.After(start) || content.Date.Equal(start)) &&
(content.Date.Before(end) || content.Date.Equal(end)) {
filtered = append(filtered, content)
}
}
return filtered
}
func (cm *ContentManager) SearchByKeyword(keyword string) []Content {
var results []Content
for _, content := range cm.Contents {
if strings.Contains(strings.ToLower(content.Text), strings.ToLower(keyword)) {
results = append(results, content)
}
}
return results
}
func (cm *ContentManager) FilterByTags(tags []string) []Content {
var filtered []Content
for _, content := range cm.Contents {
if containsAllTags(content.Tags, tags) {
filtered = append(filtered, content)
}
}
return filtered
}
func containsAllTags(contentTags, searchTags []string) bool {
for _, searchTag := range searchTags {
found := false
for _, contentTag := range contentTags {
if strings.EqualFold(searchTag, contentTag) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func (cm *ContentManager) SortByDate() {
sort.Slice(cm.Contents, func(i, j int) bool {
return cm.Contents[i].Date.After(cm.Contents[j].Date)
})
}