1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00
documize/domain/pin/mysql/store.go

132 lines
4.4 KiB
Go
Raw Normal View History

// 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 pin
import (
"fmt"
"time"
2017-07-26 10:50:26 +01:00
"github.com/documize/community/core/env"
"github.com/documize/community/domain"
"github.com/documize/community/domain/store/mysql"
2017-07-26 10:50:26 +01:00
"github.com/documize/community/model/pin"
"github.com/pkg/errors"
)
2017-07-26 10:50:26 +01:00
// Scope provides data access to MySQL.
type Scope struct {
Runtime *env.Runtime
}
// Add saves pinned item.
2017-07-26 10:50:26 +01:00
func (s Scope) Add(ctx domain.RequestContext, pin pin.Pin) (err error) {
2018-09-18 20:55:40 +01:00
row := s.Runtime.Db.QueryRow("SELECT max(c_sequence) FROM dmz_pin WHERE c_orgid=? AND c_userid=?", ctx.OrgID, ctx.UserID)
var maxSeq int
err = row.Scan(&maxSeq)
if err != nil {
maxSeq = 99
}
pin.Created = time.Now().UTC()
pin.Revised = time.Now().UTC()
pin.Sequence = maxSeq + 1
2018-09-18 20:55:40 +01:00
_, err = ctx.Transaction.Exec("INSERT INTO dmz_pin (c_refid, c_orgid, c_userid, c_spaceid, c_docid, c_name, c_sequence, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
pin.RefID, pin.OrgID, pin.UserID, pin.SpaceID, pin.DocumentID, pin.Name, pin.Sequence, pin.Created, pin.Revised)
if err != nil {
err = errors.Wrap(err, "execute pin insert")
}
return
}
// GetPin returns requested pinned item.
2017-07-26 10:50:26 +01:00
func (s Scope) GetPin(ctx domain.RequestContext, id string) (pin pin.Pin, err error) {
2018-09-18 20:55:40 +01:00
err = s.Runtime.Db.Get(&pin, `SELECT id, c_refid AS refid,
c_orgid AS orgid, c_userid AS userid, c_spaceid AS spaceid, c_docid AS documentid,
2018-09-19 16:03:29 +01:00
c_name AS name, c_sequence AS sequence, c_created AS created, c_revised AS revised
2018-09-18 20:55:40 +01:00
FROM dmz_pin
WHERE c_orgid=? AND c_refid=?`,
2017-09-25 14:37:11 +01:00
ctx.OrgID, id)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("execute select for pin %s", id))
}
return
}
// GetUserPins returns pinned items for specified user.
2017-07-26 10:50:26 +01:00
func (s Scope) GetUserPins(ctx domain.RequestContext, userID string) (pins []pin.Pin, err error) {
2018-09-18 20:55:40 +01:00
err = s.Runtime.Db.Select(&pins, `SELECT id, c_refid AS refid,
c_orgid AS orgid, c_userid AS userid, c_spaceid AS spaceid, c_docid AS documentid,
2018-09-19 16:03:29 +01:00
c_name AS name, c_sequence AS sequence, c_created AS created, c_revised AS revised
2018-09-18 20:55:40 +01:00
FROM dmz_pin
WHERE c_orgid=? AND c_userid=?
ORDER BY c_sequence`,
ctx.OrgID, userID)
if err != nil {
2017-07-26 10:50:26 +01:00
err = errors.Wrap(err, fmt.Sprintf("execute select pins for org %s and user %s", ctx.OrgID, userID))
}
return
}
// UpdatePin updates existing pinned item.
2017-07-26 10:50:26 +01:00
func (s Scope) UpdatePin(ctx domain.RequestContext, pin pin.Pin) (err error) {
pin.Revised = time.Now().UTC()
2018-09-18 20:55:40 +01:00
_, err = ctx.Transaction.NamedExec(`UPDATE dmz_pin SET
c_spaceid=:spaceid, c_docid=:documentid, c_name=:name, c_sequence=:sequence,
c_revised=:revised
WHERE c_orgid=:orgid AND c_refid=:refid`,
2017-09-25 14:37:11 +01:00
&pin)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("execute pin update %s", pin.RefID))
}
return
}
// UpdatePinSequence updates existing pinned item sequence number
2017-07-26 10:50:26 +01:00
func (s Scope) UpdatePinSequence(ctx domain.RequestContext, pinID string, sequence int) (err error) {
2018-09-18 20:55:40 +01:00
_, err = ctx.Transaction.Exec("UPDATE dmz_pin SET c_sequence=?, c_revised=? WHERE c_orgid=? AND c_userid=? AND c_refid=?",
2017-09-25 14:37:11 +01:00
sequence, time.Now().UTC(), ctx.OrgID, ctx.UserID, pinID)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("execute pin sequence update %s", pinID))
}
return
}
// DeletePin removes folder from the store.
2017-07-26 10:50:26 +01:00
func (s Scope) DeletePin(ctx domain.RequestContext, id string) (rows int64, err error) {
b := mysql.BaseQuery{}
2018-09-18 20:55:40 +01:00
return b.DeleteConstrained(ctx.Transaction, "dmz_pin", ctx.OrgID, id)
}
// DeletePinnedSpace removes any pins for specified space.
2017-07-26 10:50:26 +01:00
func (s Scope) DeletePinnedSpace(ctx domain.RequestContext, spaceID string) (rows int64, err error) {
b := mysql.BaseQuery{}
2018-09-18 20:55:40 +01:00
return b.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM dmz_pin WHERE c_orgid=\"%s\" AND c_spaceid=\"%s\"", ctx.OrgID, spaceID))
}
// DeletePinnedDocument removes any pins for specified document.
2017-07-26 10:50:26 +01:00
func (s Scope) DeletePinnedDocument(ctx domain.RequestContext, documentID string) (rows int64, err error) {
b := mysql.BaseQuery{}
2018-09-18 20:55:40 +01:00
return b.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM dmz_pin WHERE c_orgid=\"%s\" AND c_docid=\"%s\"", ctx.OrgID, documentID))
}