forked from documize/community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
243 lines (206 loc) · 8.42 KB
/
store.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package space
import (
"database/sql"
"fmt"
"time"
"github.com/documize/community/domain"
"github.com/documize/community/domain/store"
"github.com/documize/community/model/space"
"github.com/pkg/errors"
)
// Store provides data access to space information.
type Store struct {
store.Context
store.SpaceStorer
}
// Add adds new folder into the store.
func (s Store) Add(ctx domain.RequestContext, sp space.Space) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`
INSERT INTO dmz_space
(c_refid, c_name, c_orgid, c_userid, c_type, c_lifecycle,
c_likes, c_icon, c_desc, c_count_category, c_count_content,
c_labelid, c_created, c_revised)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`),
sp.RefID, sp.Name, sp.OrgID, sp.UserID, sp.Type, sp.Lifecycle, sp.Likes,
sp.Icon, sp.Description, sp.CountCategory, sp.CountContent, sp.LabelID,
sp.Created, sp.Revised)
if err != nil {
err = errors.Wrap(err, "unable to execute insert for space")
}
return
}
// Get returns a space from the store.
func (s Store) Get(ctx domain.RequestContext, id string) (sp space.Space, err error) {
err = s.Runtime.Db.Get(&sp, s.Bind(`SELECT id, c_refid AS refid,
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_icon AS icon, c_labelid AS labelid, c_desc AS description,
c_count_category As countcategory, c_count_content AS countcontent,
c_created AS created, c_revised AS revised
FROM dmz_space
WHERE c_orgid=? and c_refid=?`),
ctx.OrgID, id)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("unable to execute select for space %s", id))
}
return
}
// PublicSpaces returns spaces that anyone can see.
func (s Store) PublicSpaces(ctx domain.RequestContext, orgID string) (sp []space.Space, err error) {
qry := s.Bind(`SELECT id, c_refid AS refid,
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_icon AS icon, c_labelid AS labelid, c_desc AS description,
c_count_category AS countcategory, c_count_content AS countcontent,
c_created AS created, c_revised AS revised
FROM dmz_space
WHERE c_orgid=? AND c_type=1`)
err = s.Runtime.Db.Select(&sp, qry, orgID)
if err == sql.ErrNoRows {
err = nil
sp = []space.Space{}
}
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("Unable to execute GetPublicFolders for org %s", orgID))
}
return
}
// GetViewable returns spaces that the user can see.
// Also handles which spaces can be seen by anonymous users.
func (s Store) GetViewable(ctx domain.RequestContext) (sp []space.Space, err error) {
q := s.Bind(`SELECT id, c_refid AS refid,
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_icon AS icon, c_labelid AS labelid, c_desc AS description,
c_count_category AS countcategory, c_count_content AS countcontent,
c_created AS created, c_revised AS revised
FROM dmz_space
WHERE c_orgid=? AND c_refid IN
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_location='space' AND c_refid IN
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_who='user' AND (c_whoid=? OR c_whoid='0') AND c_location='space' AND c_action='view'
UNION ALL
SELECT p.c_refid from dmz_permission p LEFT JOIN dmz_group_member r ON p.c_whoid=r.c_groupid WHERE p.c_orgid=? AND p.c_who='role'
AND p.c_location='space' AND p.c_action='view' AND (r.c_userid=? OR r.c_userid='0')
)
)
ORDER BY c_name`)
err = s.Runtime.Db.Select(&sp, q,
ctx.OrgID,
ctx.OrgID,
ctx.OrgID,
ctx.UserID,
ctx.OrgID,
ctx.UserID)
if err == sql.ErrNoRows {
err = nil
sp = []space.Space{}
}
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed space.GetViewable org %s", ctx.OrgID))
}
return
}
// AdminList returns all shared spaces and orphaned spaces that have no owner.
func (s Store) AdminList(ctx domain.RequestContext) (sp []space.Space, err error) {
qry := s.Bind(`
SELECT id, c_refid AS refid,
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_created AS created, c_revised AS revised,
c_icon AS icon, c_labelid AS labelid, c_desc AS description,
c_count_category AS countcategory, c_count_content AS countcontent
FROM dmz_space
WHERE c_orgid=? AND (c_type=? OR c_type=?)
UNION ALL
SELECT id, c_refid AS refid,
c_name AS name, c_orgid AS orgid, c_userid AS userid,
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
c_created AS created, c_revised AS revised,
c_icon AS icon, c_labelid AS labelid, c_desc AS description,
c_count_category AS countcategory, c_count_content AS countcontent
FROM dmz_space
WHERE c_orgid=? AND (c_type=? OR c_type=?) AND c_refid NOT IN
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_action='own')
ORDER BY name`)
err = s.Runtime.Db.Select(&sp, qry,
ctx.OrgID, space.ScopePublic, space.ScopeRestricted,
ctx.OrgID, space.ScopePublic, space.ScopeRestricted,
ctx.OrgID)
if err == sql.ErrNoRows {
err = nil
sp = []space.Space{}
}
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed space.AdminList org %s", ctx.OrgID))
}
return
}
// Update saves space changes.
func (s Store) Update(ctx domain.RequestContext, sp space.Space) (err error) {
sp.Revised = time.Now().UTC()
_, err = ctx.Transaction.NamedExec(`
UPDATE dmz_space
SET c_name=:name, c_type=:type, c_lifecycle=:lifecycle, c_userid=:userid,
c_likes=:likes, c_desc=:description, c_labelid=:labelid, c_icon=:icon,
c_count_category=:countcategory, c_count_content=:countcontent,
c_revised=:revised
WHERE c_orgid=:orgid AND c_refid=:refid`, &sp)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("unable to execute update for space %s", sp.RefID))
}
return
}
// Delete removes space from the store.
func (s Store) Delete(ctx domain.RequestContext, id string) (rows int64, err error) {
return s.DeleteConstrained(ctx.Transaction, "dmz_space", ctx.OrgID, id)
}
// IncrementCategoryCount increments usage counter for space category.
func (s Store) IncrementCategoryCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_category=c_count_category+1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute increment category count")
}
return
}
// DecrementCategoryCount decrements usage counter for space category.
func (s Store) DecrementCategoryCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_category=c_count_category-1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute decrement category count")
}
return
}
// IncrementContentCount increments usage counter for space category.
func (s Store) IncrementContentCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_content=c_count_content+1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute increment content count")
}
return
}
// DecrementContentCount decrements usage counter for space category.
func (s Store) DecrementContentCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_content=c_count_content-1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute decrement category count")
}
return
}