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 pathAnimeCharacters.go
134 lines (103 loc) · 3.1 KB
/
AnimeCharacters.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
package arn
import (
"errors"
"sync"
"github.com/aerogo/nano"
"github.com/akyoto/color"
)
// AnimeCharacters is a list of characters for an anime.
type AnimeCharacters struct {
AnimeID string `json:"animeId" mainID:"true"`
Items []*AnimeCharacter `json:"items" editable:"true"`
sync.Mutex
}
// Anime returns the anime the characters refer to.
func (characters *AnimeCharacters) Anime() *Anime {
anime, _ := GetAnime(characters.AnimeID)
return anime
}
// Add adds an anime character to the list.
func (characters *AnimeCharacters) Add(animeCharacter *AnimeCharacter) error {
if animeCharacter.CharacterID == "" || animeCharacter.Role == "" {
return errors.New("Empty ID or role")
}
characters.Lock()
characters.Items = append(characters.Items, animeCharacter)
characters.Unlock()
return nil
}
// FindByMapping finds an anime character by the given mapping.
func (characters *AnimeCharacters) FindByMapping(service string, serviceID string) *AnimeCharacter {
characters.Lock()
defer characters.Unlock()
for _, animeCharacter := range characters.Items {
character := animeCharacter.Character()
if character == nil {
color.Red("Anime %s has an incorrect Character ID inside AnimeCharacter: %s", characters.AnimeID, animeCharacter.CharacterID)
continue
}
if character.GetMapping(service) == serviceID {
return animeCharacter
}
}
return nil
}
// Link returns the link for that object.
func (characters *AnimeCharacters) Link() string {
return "/anime/" + characters.AnimeID + "/characters"
}
// String implements the default string serialization.
func (characters *AnimeCharacters) String() string {
return characters.Anime().String()
}
// GetID returns the anime ID.
func (characters *AnimeCharacters) GetID() string {
return characters.AnimeID
}
// TypeName returns the type name.
func (characters *AnimeCharacters) TypeName() string {
return "AnimeCharacters"
}
// Self returns the object itself.
func (characters *AnimeCharacters) Self() Loggable {
return characters
}
// Contains tells you whether the given character ID exists.
func (characters *AnimeCharacters) Contains(characterID string) bool {
characters.Lock()
defer characters.Unlock()
for _, item := range characters.Items {
if item.CharacterID == characterID {
return true
}
}
return false
}
// First gives you the first "count" anime characters.
func (characters *AnimeCharacters) First(count int) []*AnimeCharacter {
characters.Lock()
defer characters.Unlock()
if count > len(characters.Items) {
count = len(characters.Items)
}
return characters.Items[:count]
}
// GetAnimeCharacters ...
func GetAnimeCharacters(animeID string) (*AnimeCharacters, error) {
obj, err := DB.Get("AnimeCharacters", animeID)
if err != nil {
return nil, err
}
return obj.(*AnimeCharacters), nil
}
// StreamAnimeCharacters returns a stream of all anime characters.
func StreamAnimeCharacters() <-chan *AnimeCharacters {
channel := make(chan *AnimeCharacters, nano.ChannelBufferSize)
go func() {
for obj := range DB.All("AnimeCharacters") {
channel <- obj.(*AnimeCharacters)
}
close(channel)
}()
return channel
}