mirror of
https://github.com/documize/community.git
synced 2025-07-24 15:49:44 +02:00
implemented delete reusable content block
This commit is contained in:
parent
2493a09ba9
commit
fc9127e165
12 changed files with 130 additions and 16 deletions
|
@ -219,6 +219,7 @@ func init() {
|
|||
log.IfErr(Add(RoutePrefixPrivate, "sections/blocks/space/{folderID}", []string{"GET", "OPTIONS"}, nil, GetBlocksForSpace))
|
||||
log.IfErr(Add(RoutePrefixPrivate, "sections/blocks/{blockID}", []string{"GET", "OPTIONS"}, nil, GetBlock))
|
||||
log.IfErr(Add(RoutePrefixPrivate, "sections/blocks/{blockID}", []string{"PUT", "OPTIONS"}, nil, UpdateBlock))
|
||||
log.IfErr(Add(RoutePrefixPrivate, "sections/blocks/{blockID}", []string{"DELETE", "OPTIONS"}, nil, DeleteBlock))
|
||||
log.IfErr(Add(RoutePrefixPrivate, "sections/blocks", []string{"POST", "OPTIONS"}, nil, AddBlock))
|
||||
|
||||
// Links
|
||||
|
|
|
@ -226,7 +226,19 @@ func AddBlock(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
log.IfErr(tx.Commit())
|
||||
|
||||
writeSuccessEmptyJSON(w)
|
||||
b, err = p.GetBlock(b.RefID)
|
||||
if err != nil {
|
||||
writeGeneralSQLError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
json, err := json.Marshal(b)
|
||||
if err != nil {
|
||||
writeJSONMarshalError(w, method, "block", err)
|
||||
return
|
||||
}
|
||||
|
||||
writeSuccessBytes(w, json)
|
||||
}
|
||||
|
||||
// GetBlock returns requested reusable content block.
|
||||
|
@ -337,3 +349,43 @@ func UpdateBlock(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
writeSuccessEmptyJSON(w)
|
||||
}
|
||||
|
||||
// DeleteBlock removes requested reusable content block.
|
||||
func DeleteBlock(w http.ResponseWriter, r *http.Request) {
|
||||
method := "DeleteBlock"
|
||||
p := request.GetPersister(r)
|
||||
|
||||
params := mux.Vars(r)
|
||||
blockID := params["blockID"]
|
||||
|
||||
if len(blockID) == 0 {
|
||||
writeMissingDataError(w, method, "blockID")
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := request.Db.Beginx()
|
||||
if err != nil {
|
||||
writeTransactionError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
p.Context.Transaction = tx
|
||||
|
||||
_, err = p.DeleteBlock(blockID)
|
||||
if err != nil {
|
||||
log.IfErr(tx.Rollback())
|
||||
writeGeneralSQLError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = p.RemoveBlockReference(blockID)
|
||||
if err != nil {
|
||||
log.IfErr(tx.Rollback())
|
||||
writeGeneralSQLError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
log.IfErr(tx.Commit())
|
||||
|
||||
writeSuccessEmptyJSON(w)
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
package request
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
|
@ -113,6 +114,30 @@ func (p *Persister) DecrementBlockUsage(id string) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// RemoveBlockReference clears page.blockid for given blockID.
|
||||
func (p *Persister) RemoveBlockReference(id string) (err error) {
|
||||
stmt, err := p.Context.Transaction.Preparex("UPDATE page SET blockid='', revised=? WHERE orgid=? AND blockid=?")
|
||||
defer utility.Close(stmt)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to prepare update RemoveBlockReference id %s", id), err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(time.Now().UTC(), p.Context.OrgID, id)
|
||||
|
||||
// skip no rows affected
|
||||
if err == sql.ErrNoRows {
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to execute RemoveBlockReference 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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue