This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAnimeRelations.go
127 lines (98 loc) · 2.64 KB
/
AnimeRelations.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
package arn
import (
"sort"
"sync"
"github.com/aerogo/nano"
)
// AnimeRelations is a list of relations for an anime.
type AnimeRelations struct {
AnimeID string `json:"animeId" mainID:"true"`
Items []*AnimeRelation `json:"items" editable:"true"`
sync.Mutex
}
// Link returns the link for that object.
func (relations *AnimeRelations) Link() string {
return "/anime/" + relations.AnimeID + "/relations"
}
// SortByStartDate ...
func (relations *AnimeRelations) SortByStartDate() {
relations.Lock()
defer relations.Unlock()
sort.Slice(relations.Items, func(i, j int) bool {
a := relations.Items[i].Anime()
b := relations.Items[j].Anime()
if a == nil {
return false
}
if b == nil {
return true
}
if a.StartDate == b.StartDate {
return a.Title.Canonical < b.Title.Canonical
}
return a.StartDate < b.StartDate
})
}
// Anime returns the anime the relations list refers to.
func (relations *AnimeRelations) Anime() *Anime {
anime, _ := GetAnime(relations.AnimeID)
return anime
}
// String implements the default string serialization.
func (relations *AnimeRelations) String() string {
return relations.Anime().String()
}
// GetID returns the anime ID.
func (relations *AnimeRelations) GetID() string {
return relations.AnimeID
}
// TypeName returns the type name.
func (relations *AnimeRelations) TypeName() string {
return "AnimeRelations"
}
// Self returns the object itself.
func (relations *AnimeRelations) Self() Loggable {
return relations
}
// Find returns the relation with the specified anime ID, if available.
func (relations *AnimeRelations) Find(animeID string) *AnimeRelation {
relations.Lock()
defer relations.Unlock()
for _, item := range relations.Items {
if item.AnimeID == animeID {
return item
}
}
return nil
}
// Remove removes the anime ID from the relations.
func (relations *AnimeRelations) Remove(animeID string) bool {
relations.Lock()
defer relations.Unlock()
for index, item := range relations.Items {
if item.AnimeID == animeID {
relations.Items = append(relations.Items[:index], relations.Items[index+1:]...)
return true
}
}
return false
}
// GetAnimeRelations ...
func GetAnimeRelations(animeID string) (*AnimeRelations, error) {
obj, err := DB.Get("AnimeRelations", animeID)
if err != nil {
return nil, err
}
return obj.(*AnimeRelations), nil
}
// StreamAnimeRelations returns a stream of all anime relations.
func StreamAnimeRelations() <-chan *AnimeRelations {
channel := make(chan *AnimeRelations, nano.ChannelBufferSize)
go func() {
for obj := range DB.All("AnimeRelations") {
channel <- obj.(*AnimeRelations)
}
close(channel)
}()
return channel
}