-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
152 lines (125 loc) · 5.12 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
// 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 block
import (
"database/sql"
"time"
"github.com/documize/community/domain"
"github.com/documize/community/domain/store"
"github.com/documize/community/model/block"
"github.com/pkg/errors"
)
// Store provides data access to section template information.
type Store struct {
store.Context
store.BlockStorer
}
// Add saves reusable content block.
func (s Store) Add(ctx domain.RequestContext, b block.Block) (err error) {
b.OrgID = ctx.OrgID
b.UserID = ctx.UserID
b.Created = time.Now().UTC()
b.Revised = time.Now().UTC()
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_section_template (c_refid, c_orgid, c_spaceid, c_userid, c_contenttype, c_type, c_name, c_body, c_desc, c_rawbody, c_config, c_external, c_used, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"),
b.RefID, b.OrgID, b.SpaceID, b.UserID, b.ContentType, b.Type, b.Name, b.Body, b.Excerpt, b.RawBody, b.Config, b.ExternalSource, b.Used, b.Created, b.Revised)
if err != nil {
err = errors.Wrap(err, "execute insert block")
}
return
}
// Get returns requested reusable content block.
func (s Store) Get(ctx domain.RequestContext, id string) (b block.Block, err error) {
err = s.Runtime.Db.Get(&b, s.Bind(`
SELECT a.id, a.c_refid as refid,
a.c_orgid as orgid,
a.c_spaceid AS spaceid, a.c_userid AS userid, a.c_contenttype AS contenttype, a.c_type AS type,
a.c_name AS name, a.c_body AS body, a.c_desc AS excerpt, a.c_rawbody AS rawbody,
a.c_config AS config, a.c_external AS externalsource, a.c_used AS used,
a.c_created AS created, a.c_revised AS revised,
b.c_firstname AS firstname, b.c_lastname AS lastname
FROM dmz_section_template a LEFT JOIN dmz_user b ON a.c_userid = b.c_refid
WHERE a.c_orgid=? AND a.c_refid=?`),
ctx.OrgID, id)
if err != nil {
err = errors.Wrap(err, "execute select block")
}
return
}
// GetBySpace returns all reusable content scoped to given space.
func (s Store) GetBySpace(ctx domain.RequestContext, spaceID string) (b []block.Block, err error) {
err = s.Runtime.Db.Select(&b, s.Bind(`
SELECT a.id, a.c_refid as refid,
a.c_orgid as orgid,
a.c_spaceid AS spaceid, a.c_userid AS userid, a.c_contenttype AS contenttype, a.c_type AS type,
a.c_name AS name, a.c_body AS body, a.c_desc AS excerpt, a.c_rawbody AS rawbody,
a.c_config AS config, a.c_external AS externalsource, a.c_used AS used,
a.c_created AS created, a.c_revised AS revised,
b.c_firstname AS firstname, b.c_lastname AS lastname
FROM dmz_section_template a LEFT JOIN dmz_user b ON a.c_userid = b.c_refid
WHERE a.c_orgid=? AND a.c_spaceid=?
ORDER BY a.c_name`),
ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "select space blocks")
}
return
}
// IncrementUsage increments usage counter for content block.
func (s Store) IncrementUsage(ctx domain.RequestContext, id string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_section_template SET
c_used=c_used+1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, id)
if err != nil {
err = errors.Wrap(err, "execute increment block usage")
}
return
}
// DecrementUsage decrements usage counter for content block.
func (s Store) DecrementUsage(ctx domain.RequestContext, id string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_section_template SET
c_used=c_used-1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, id)
if err != nil {
err = errors.Wrap(err, "execute decrement block usage")
}
return
}
// RemoveReference clears page.blockid for given blockID.
func (s Store) RemoveReference(ctx domain.RequestContext, id string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_section SET
c_templateid='', c_revised=?
WHERE c_orgid=? AND c_templateid=?`),
time.Now().UTC(), ctx.OrgID, id)
if err == sql.ErrNoRows {
err = nil
}
if err != nil {
err = errors.Wrap(err, "execute remove block ref")
}
return
}
// Update updates existing reusable content block item.
func (s Store) Update(ctx domain.RequestContext, b block.Block) (err error) {
b.Revised = time.Now().UTC()
_, err = ctx.Transaction.NamedExec(s.Bind(`UPDATE dmz_section_template SET
c_name=:name, c_body=:body, c_desc=:excerpt, c_rawbody=:rawbody,
c_config=:config, c_revised=:revised
WHERE c_orgid=:orgid AND c_refid=:refid`),
b)
if err != nil {
err = errors.Wrap(err, "execute update block")
}
return
}
// Delete removes reusable content block from database.
func (s Store) Delete(ctx domain.RequestContext, id string) (rows int64, err error) {
return s.DeleteConstrained(ctx.Transaction, "dmz_section_template", ctx.OrgID, id)
}