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 pathCompany.go
161 lines (126 loc) · 3.33 KB
/
Company.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
package arn
import (
"errors"
"github.com/aerogo/nano"
)
// Company represents an anime studio, producer or licensor.
type Company struct {
Name CompanyName `json:"name" editable:"true"`
Description string `json:"description" editable:"true" type:"textarea"`
Email string `json:"email" editable:"true"`
Links []*Link `json:"links" editable:"true"`
// Mixins
hasID
hasMappings
hasLikes
hasDraft
// Other editable fields
Location *Location `json:"location" editable:"true"`
Tags []string `json:"tags" editable:"true"`
// Editing dates
hasCreator
hasEditor
}
// NewCompany creates a new company.
func NewCompany() *Company {
return &Company{
hasID: hasID{
ID: GenerateID("Company"),
},
Name: CompanyName{},
Links: []*Link{},
Tags: []string{},
hasCreator: hasCreator{
Created: DateTimeUTC(),
},
hasMappings: hasMappings{
Mappings: []*Mapping{},
},
}
}
// Link returns a single company.
func (company *Company) Link() string {
return "/company/" + company.ID
}
// Anime returns the anime connected with this company.
func (company *Company) Anime() (studioAnime []*Anime, producedAnime []*Anime, licensedAnime []*Anime) {
for anime := range StreamAnime() {
if Contains(anime.StudioIDs, company.ID) {
studioAnime = append(studioAnime, anime)
}
if Contains(anime.ProducerIDs, company.ID) {
producedAnime = append(producedAnime, anime)
}
if Contains(anime.LicensorIDs, company.ID) {
licensedAnime = append(licensedAnime, anime)
}
}
SortAnimeByQuality(studioAnime)
SortAnimeByQuality(producedAnime)
SortAnimeByQuality(licensedAnime)
return studioAnime, producedAnime, licensedAnime
}
// Publish publishes the company draft.
func (company *Company) Publish() error {
// No title
if company.Name.English == "" {
return errors.New("No English company name")
}
return publish(company)
}
// Unpublish turns the company into a draft.
func (company *Company) Unpublish() error {
return unpublish(company)
}
// String implements the default string serialization.
func (company *Company) String() string {
return company.Name.English
}
// TypeName returns the type name.
func (company *Company) TypeName() string {
return "Company"
}
// Self returns the object itself.
func (company *Company) Self() Loggable {
return company
}
// GetCompany returns a single company.
func GetCompany(id string) (*Company, error) {
obj, err := DB.Get("Company", id)
if err != nil {
return nil, err
}
return obj.(*Company), nil
}
// StreamCompanies returns a stream of all companies.
func StreamCompanies() <-chan *Company {
channel := make(chan *Company, nano.ChannelBufferSize)
go func() {
for obj := range DB.All("Company") {
channel <- obj.(*Company)
}
close(channel)
}()
return channel
}
// FilterCompanies filters all companies by a custom function.
func FilterCompanies(filter func(*Company) bool) []*Company {
var filtered []*Company
channel := DB.All("Company")
for obj := range channel {
realObject := obj.(*Company)
if filter(realObject) {
filtered = append(filtered, realObject)
}
}
return filtered
}
// AllCompanies returns a slice of all companies.
func AllCompanies() []*Company {
all := make([]*Company, 0, DB.Collection("Company").Count())
stream := StreamCompanies()
for obj := range stream {
all = append(all, obj)
}
return all
}