mirror of
https://github.com/documize/community.git
synced 2025-07-24 15:49:44 +02:00
re-worked to use new BLOCK table
This commit is contained in:
parent
b7fa3b9006
commit
bbc2237ef7
25 changed files with 1080 additions and 903 deletions
141
core/api/request/block.go
Normal file
141
core/api/request/block.go
Normal file
|
@ -0,0 +1,141 @@
|
|||
// 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 request
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/documize/community/core/api/entity"
|
||||
"github.com/documize/community/core/log"
|
||||
"github.com/documize/community/core/utility"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// AddBlock saves reusable content block.
|
||||
func (p *Persister) AddBlock(b entity.Block) (err error) {
|
||||
b.OrgID = p.Context.OrgID
|
||||
b.UserID = p.Context.UserID
|
||||
b.Created = time.Now().UTC()
|
||||
b.Revised = time.Now().UTC()
|
||||
|
||||
stmt, err := p.Context.Transaction.Preparex("INSERT INTO block (refid, orgid, labelid, userid, contenttype, pagetype, title, body, excerpt, rawbody, config, externalsource, used, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
log.Error("Unable to prepare insert AddBlock", err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(b.RefID, b.OrgID, b.LabelID, b.UserID, b.ContentType, b.PageType, b.Title, b.Body, b.Excerpt, b.RawBody, b.Config, b.ExternalSource, b.Used, b.Created, b.Revised)
|
||||
|
||||
if err != nil {
|
||||
log.Error("Unable to execute insert AddBlock", err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetBlock returns requested reusable content block.
|
||||
func (p *Persister) GetBlock(id string) (b entity.Block, err error) {
|
||||
stmt, err := Db.Preparex("SELECT id, refid, orgid, labelid, userid, contenttype, pagetype, title, body, excerpt, rawbody, config, externalsource, used, created, revised FROM block WHERE orgid=? AND refid=?")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to prepare select GetBlock %s", id), err)
|
||||
return
|
||||
}
|
||||
|
||||
err = stmt.Get(&b, p.Context.OrgID, id)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute select GetBlock %s", id), err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetBlocksForSpace returns all reusable content scoped to given space.
|
||||
func (p *Persister) GetBlocksForSpace(labelID string) (b []entity.Block, err error) {
|
||||
err = Db.Select(&b, "SELECT a.id, a.refid, a.orgid, a.labelid, a.userid, a.contenttype, a.pagetype, a.title, a.body, a.excerpt, a.rawbody, a.config, a.externalsource, a.used, a.created, a.revised, b.firstname, b.lastname FROM block a LEFT JOIN user b ON a.userid = b.refid WHERE orgid=? AND labelid=? ORDER BY a.title", p.Context.OrgID, labelID)
|
||||
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute select GetBlocksForSpace org %s and label %s", p.Context.OrgID, labelID), err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// IncrementBlockUsage increments usage counter for content block.
|
||||
func (p *Persister) IncrementBlockUsage(id string) (err error) {
|
||||
stmt, err := p.Context.Transaction.Preparex("UPDATE block SET used=used+1, revised=? WHERE orgid=? AND refid=?")
|
||||
defer utility.Close(stmt)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to prepare update IncrementBlockUsage id %s", id), err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(time.Now().UTC(), p.Context.OrgID, id)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute IncrementBlockUsage id %s", id), err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DecrementBlockUsage decrements usage counter for content block.
|
||||
func (p *Persister) DecrementBlockUsage(id string) (err error) {
|
||||
stmt, err := p.Context.Transaction.Preparex("UPDATE block SET used=used-1, revised=? WHERE orgid=? AND refid=?")
|
||||
defer utility.Close(stmt)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to prepare update DecrementBlockUsage id %s", id), err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(time.Now().UTC(), p.Context.OrgID, id)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute DecrementBlockUsage id %s", id), err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateBlock updates existing reusable content block item.
|
||||
func (p *Persister) UpdateBlock(b entity.Block) (err error) {
|
||||
b.Revised = time.Now().UTC()
|
||||
|
||||
var stmt *sqlx.NamedStmt
|
||||
stmt, err = p.Context.Transaction.PrepareNamed("UPDATE block SET title=:title, body=:body, excerpt=:excerpt, rawbody=:rawbody, config=:config, revised=:revised WHERE orgid=:orgid AND refid=:refid")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to prepare update UpdateBlock %s", b.RefID), err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(&b)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute update UpdateBlock %s", b.RefID), err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteBlock removes reusable content block from database.
|
||||
func (p *Persister) DeleteBlock(id string) (rows int64, err error) {
|
||||
return p.Base.DeleteConstrained(p.Context.Transaction, "block", p.Context.OrgID, id)
|
||||
}
|
|
@ -50,7 +50,7 @@ func (p *Persister) AddPage(model models.PageModel) (err error) {
|
|||
model.Page.Sequence = maxSeq * 2
|
||||
}
|
||||
|
||||
stmt, err := p.Context.Transaction.Preparex("INSERT INTO page (refid, orgid, documentid, userid, contenttype, pagetype, level, title, body, revisions, sequence, preset, presetid, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
||||
stmt, err := p.Context.Transaction.Preparex("INSERT INTO page (refid, orgid, documentid, userid, contenttype, pagetype, level, title, body, revisions, sequence, blockid, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -58,7 +58,7 @@ func (p *Persister) AddPage(model models.PageModel) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(model.Page.RefID, model.Page.OrgID, model.Page.DocumentID, model.Page.UserID, model.Page.ContentType, model.Page.PageType, model.Page.Level, model.Page.Title, model.Page.Body, model.Page.Revisions, model.Page.Sequence, model.Page.Preset, model.Page.PresetID, model.Page.Created, model.Page.Revised)
|
||||
_, err = stmt.Exec(model.Page.RefID, model.Page.OrgID, model.Page.DocumentID, model.Page.UserID, model.Page.ContentType, model.Page.PageType, model.Page.Level, model.Page.Title, model.Page.Body, model.Page.Revisions, model.Page.Sequence, model.Page.BlockID, model.Page.Created, model.Page.Revised)
|
||||
|
||||
if err != nil {
|
||||
log.Error("Unable to execute insert for page", err)
|
||||
|
@ -89,9 +89,7 @@ func (p *Persister) AddPage(model models.PageModel) (err error) {
|
|||
|
||||
// GetPage returns the pageID page record from the page table.
|
||||
func (p *Persister) GetPage(pageID string) (page entity.Page, err error) {
|
||||
err = nil
|
||||
|
||||
stmt, err := Db.Preparex("SELECT a.id, a.refid, a.orgid, a.documentid, a.userid, a.contenttype, a.pagetype, a.level, a.sequence, a.title, a.body, a.revisions, a.preset, a.presetid, a.created, a.revised FROM page a WHERE a.orgid=? AND a.refid=?")
|
||||
stmt, err := Db.Preparex("SELECT a.id, a.refid, a.orgid, a.documentid, a.userid, a.contenttype, a.pagetype, a.level, a.sequence, a.title, a.body, a.revisions, a.blockid, a.created, a.revised FROM page a WHERE a.orgid=? AND a.refid=?")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -111,9 +109,7 @@ func (p *Persister) GetPage(pageID string) (page entity.Page, err error) {
|
|||
|
||||
// GetPages returns a slice containing all the page records for a given documentID, in presentation sequence.
|
||||
func (p *Persister) GetPages(documentID string) (pages []entity.Page, err error) {
|
||||
err = nil
|
||||
|
||||
err = Db.Select(&pages, "SELECT a.id, a.refid, a.orgid, a.documentid, a.userid, a.contenttype, a.pagetype, a.level, a.sequence, a.title, a.body, a.revisions, a.preset, a.presetid, a.created, a.revised FROM page a WHERE a.orgid=? AND a.documentid=? ORDER BY a.sequence", p.Context.OrgID, documentID)
|
||||
err = Db.Select(&pages, "SELECT a.id, a.refid, a.orgid, a.documentid, a.userid, a.contenttype, a.pagetype, a.level, a.sequence, a.title, a.body, a.revisions, a.blockid, a.created, a.revised FROM page a WHERE a.orgid=? AND a.documentid=? ORDER BY a.sequence", p.Context.OrgID, documentID)
|
||||
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute select pages for org %s and document %s", p.Context.OrgID, documentID), err)
|
||||
|
@ -126,11 +122,9 @@ func (p *Persister) GetPages(documentID string) (pages []entity.Page, err error)
|
|||
// GetPagesWhereIn returns a slice, in presentation sequence, containing those page records for a given documentID
|
||||
// where their refid is in the comma-separated list passed as inPages.
|
||||
func (p *Persister) GetPagesWhereIn(documentID, inPages string) (pages []entity.Page, err error) {
|
||||
err = nil
|
||||
|
||||
args := []interface{}{p.Context.OrgID, documentID}
|
||||
tempValues := strings.Split(inPages, ",")
|
||||
sql := "SELECT a.id, a.refid, a.orgid, a.documentid, a.userid, a.contenttype, a.pagetype, a.level, a.sequence, a.title, a.body, a.preset, a.presetid, a.revisions, a.created, a.revised FROM page a WHERE a.orgid=? AND a.documentid=? AND a.refid IN (?" + strings.Repeat(",?", len(tempValues)-1) + ") ORDER BY sequence"
|
||||
sql := "SELECT a.id, a.refid, a.orgid, a.documentid, a.userid, a.contenttype, a.pagetype, a.level, a.sequence, a.title, a.body, a.blockid, a.revisions, a.created, a.revised FROM page a WHERE a.orgid=? AND a.documentid=? AND a.refid IN (?" + strings.Repeat(",?", len(tempValues)-1) + ") ORDER BY sequence"
|
||||
|
||||
inValues := make([]interface{}, len(tempValues))
|
||||
|
||||
|
@ -180,7 +174,7 @@ func (p *Persister) GetPagesWhereIn(documentID, inPages string) (pages []entity.
|
|||
// GetPagesWithoutContent returns a slice containing all the page records for a given documentID, in presentation sequence,
|
||||
// but without the body field (which holds the HTML content).
|
||||
func (p *Persister) GetPagesWithoutContent(documentID string) (pages []entity.Page, err error) {
|
||||
err = Db.Select(&pages, "SELECT id, refid, orgid, documentid, userid, contenttype, pagetype, sequence, level, title, revisions, preset, presetid, created, revised FROM page WHERE orgid=? AND documentid=? ORDER BY sequence", p.Context.OrgID, documentID)
|
||||
err = Db.Select(&pages, "SELECT id, refid, orgid, documentid, userid, contenttype, pagetype, sequence, level, title, revisions, blockid, created, revised FROM page WHERE orgid=? AND documentid=? ORDER BY sequence", p.Context.OrgID, documentID)
|
||||
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute select pages for org %s and document %s", p.Context.OrgID, documentID), err)
|
||||
|
@ -193,7 +187,6 @@ func (p *Persister) GetPagesWithoutContent(documentID string) (pages []entity.Pa
|
|||
// UpdatePage saves changes to the database and handles recording of revisions.
|
||||
// Not all updates result in a revision being recorded hence the parameter.
|
||||
func (p *Persister) UpdatePage(page entity.Page, refID, userID string, skipRevision bool) (err error) {
|
||||
err = nil
|
||||
page.Revised = time.Now().UTC()
|
||||
|
||||
// Store revision history
|
||||
|
@ -303,7 +296,6 @@ func (p *Persister) UpdatePage(page entity.Page, refID, userID string, skipRevis
|
|||
|
||||
// UpdatePageMeta persists meta information associated with a document page.
|
||||
func (p *Persister) UpdatePageMeta(meta entity.PageMeta, updateUserID bool) (err error) {
|
||||
err = nil
|
||||
meta.Revised = time.Now().UTC()
|
||||
|
||||
if updateUserID {
|
||||
|
@ -405,8 +397,6 @@ func (p *Persister) DeletePage(documentID, pageID string) (rows int64, err error
|
|||
|
||||
// GetPageMeta returns the meta information associated with the page.
|
||||
func (p *Persister) GetPageMeta(pageID string) (meta entity.PageMeta, err error) {
|
||||
err = nil
|
||||
|
||||
stmt, err := Db.Preparex("SELECT id, pageid, orgid, userid, documentid, rawbody, coalesce(config,JSON_UNQUOTE('{}')) as config, externalsource, created, revised FROM pagemeta WHERE orgid=? AND pageid=?")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
|
@ -427,7 +417,6 @@ func (p *Persister) GetPageMeta(pageID string) (meta entity.PageMeta, err error)
|
|||
|
||||
// GetDocumentPageMeta returns the meta information associated with a document.
|
||||
func (p *Persister) GetDocumentPageMeta(documentID string, externalSourceOnly bool) (meta []entity.PageMeta, err error) {
|
||||
err = nil
|
||||
filter := ""
|
||||
if externalSourceOnly {
|
||||
filter = " AND externalsource=1"
|
||||
|
@ -507,24 +496,3 @@ func (p *Persister) DeletePageRevisions(pageID string) (rows int64, err error) {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
/********************
|
||||
* Section templates
|
||||
********************/
|
||||
|
||||
// GetSpaceSectionTemplates returns a slice all saved section templates.
|
||||
func (p *Persister) GetSpaceSectionTemplates(folderID string) (pages []entity.PageTemplate, err error) {
|
||||
err = Db.Select(&pages, `SELECT a.id, a.refid, a.orgid, a.documentid, a.userid, a.contenttype, a.pagetype, a.level, a.sequence, a.title, a.body, a.revisions, a.preset, a.presetid, a.created, a.revised, u.firstname, u.lastname
|
||||
FROM page a
|
||||
LEFT JOIN document b ON a.documentid = b.refid
|
||||
LEFT JOIN user u ON a.userid = u.refid
|
||||
WHERE a.orgid=? AND b.labelid=? AND a.preset=1
|
||||
ORDER BY a.title`, p.Context.OrgID, folderID)
|
||||
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute GetSpaceSectionTemplates for org %s and space %s", p.Context.OrgID, folderID), err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -56,8 +56,6 @@ func (p *Persister) AddPin(pin entity.Pin) (err error) {
|
|||
|
||||
// GetPin returns requested pinned item.
|
||||
func (p *Persister) GetPin(id string) (pin entity.Pin, err error) {
|
||||
err = nil
|
||||
|
||||
stmt, err := Db.Preparex("SELECT id, refid, orgid, userid, labelid as folderid, documentid, pin, sequence, created, revised FROM pin WHERE orgid=? AND refid=?")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
|
@ -90,7 +88,6 @@ func (p *Persister) GetUserPins(userID string) (pins []entity.Pin, err error) {
|
|||
|
||||
// UpdatePin updates existing pinned item.
|
||||
func (p *Persister) UpdatePin(pin entity.Pin) (err error) {
|
||||
err = nil
|
||||
pin.Revised = time.Now().UTC()
|
||||
|
||||
var stmt *sqlx.NamedStmt
|
||||
|
@ -114,8 +111,6 @@ func (p *Persister) UpdatePin(pin entity.Pin) (err error) {
|
|||
|
||||
// UpdatePinSequence updates existing pinned item sequence number
|
||||
func (p *Persister) UpdatePinSequence(pinID string, sequence int) (err error) {
|
||||
err = nil
|
||||
|
||||
stmt, err := p.Context.Transaction.Preparex("UPDATE pin SET sequence=?, revised=? WHERE orgid=? AND userid=? AND refid=?")
|
||||
defer utility.Close(stmt)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue