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 pathCompanyAPI.go
139 lines (111 loc) · 3.29 KB
/
CompanyAPI.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
package arn
import (
"errors"
"fmt"
"reflect"
"github.com/aerogo/aero"
"github.com/aerogo/api"
)
// Force interface implementations
var (
_ Publishable = (*Company)(nil)
_ fmt.Stringer = (*Company)(nil)
_ api.Newable = (*Company)(nil)
_ api.Editable = (*Company)(nil)
_ api.Deletable = (*Company)(nil)
_ api.ArrayEventListener = (*Company)(nil)
)
// Actions
func init() {
API.RegisterActions("Company", []*api.Action{
// Publish
PublishAction(),
// Unpublish
UnpublishAction(),
// Like
LikeAction(),
// Unlike
UnlikeAction(),
})
}
// Create sets the data for a new company with data we received from the API request.
func (company *Company) Create(ctx aero.Context) error {
user := GetUserFromContext(ctx)
if user == nil {
return errors.New("Not logged in")
}
company.ID = GenerateID("Company")
company.Created = DateTimeUTC()
company.CreatedBy = user.ID
company.Location = &Location{}
// Write log entry
logEntry := NewEditLogEntry(user.ID, "create", "Company", company.ID, "", "", "")
logEntry.Save()
return company.Unpublish()
}
// Edit creates an edit log entry.
func (company *Company) Edit(ctx aero.Context, key string, value reflect.Value, newValue reflect.Value) (consumed bool, err error) {
return edit(company, ctx, key, value, newValue)
}
// OnAppend saves a log entry.
func (company *Company) OnAppend(ctx aero.Context, key string, index int, obj interface{}) {
onAppend(company, ctx, key, index, obj)
}
// OnRemove saves a log entry.
func (company *Company) OnRemove(ctx aero.Context, key string, index int, obj interface{}) {
onRemove(company, ctx, key, index, obj)
}
// Save saves the company in the database.
func (company *Company) Save() {
DB.Set("Company", company.ID, company)
}
// DeleteInContext deletes the company in the given context.
func (company *Company) DeleteInContext(ctx aero.Context) error {
user := GetUserFromContext(ctx)
// Write log entry
logEntry := NewEditLogEntry(user.ID, "delete", "Company", company.ID, "", fmt.Sprint(company), "")
logEntry.Save()
return company.Delete()
}
// Delete deletes the object from the database.
func (company *Company) Delete() error {
if company.IsDraft {
draftIndex := company.Creator().DraftIndex()
draftIndex.CompanyID = ""
draftIndex.Save()
}
// Remove company ID from all anime
for anime := range StreamAnime() {
for index, id := range anime.StudioIDs {
if id == company.ID {
anime.StudioIDs = append(anime.StudioIDs[:index], anime.StudioIDs[index+1:]...)
break
}
}
for index, id := range anime.ProducerIDs {
if id == company.ID {
anime.ProducerIDs = append(anime.ProducerIDs[:index], anime.ProducerIDs[index+1:]...)
break
}
}
for index, id := range anime.LicensorIDs {
if id == company.ID {
anime.LicensorIDs = append(anime.LicensorIDs[:index], anime.LicensorIDs[index+1:]...)
break
}
}
}
DB.Delete("Company", company.ID)
return nil
}
// Authorize returns an error if the given API request is not authorized.
func (company *Company) Authorize(ctx aero.Context, action string) error {
user := GetUserFromContext(ctx)
if user == nil {
return errors.New("Not logged in")
}
if user.Role != "editor" && user.Role != "admin" {
return errors.New("Insufficient permissions")
}
return nil
}