1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 13:19:43 +02:00

reusable content block editing completed

This commit is contained in:
Harvey Kandola 2017-01-21 17:28:31 -08:00
parent bbc2237ef7
commit 2493a09ba9
15 changed files with 307 additions and 23 deletions

View file

@ -229,6 +229,34 @@ func AddBlock(w http.ResponseWriter, r *http.Request) {
writeSuccessEmptyJSON(w)
}
// GetBlock returns requested reusable content block.
func GetBlock(w http.ResponseWriter, r *http.Request) {
method := "GetBlock"
p := request.GetPersister(r)
params := mux.Vars(r)
blockID := params["blockID"]
if len(blockID) == 0 {
writeMissingDataError(w, method, "blockID")
return
}
b, err := p.GetBlock(blockID)
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)
}
// GetBlocksForSpace returns available reusable content blocks for the space.
func GetBlocksForSpace(w http.ResponseWriter, r *http.Request) {
method := "GetBlocksForSpace"
@ -264,3 +292,48 @@ func GetBlocksForSpace(w http.ResponseWriter, r *http.Request) {
writeSuccessBytes(w, json)
}
// UpdateBlock inserts new reusable content block into database.
func UpdateBlock(w http.ResponseWriter, r *http.Request) {
method := "UpdateBlock"
p := request.GetPersister(r)
defer utility.Close(r.Body)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writeBadRequestError(w, method, "Bad payload")
return
}
b := entity.Block{}
err = json.Unmarshal(body, &b)
if err != nil {
writePayloadError(w, method, err)
return
}
if !p.CanUploadDocument(b.LabelID) {
writeForbiddenError(w)
return
}
tx, err := request.Db.Beginx()
if err != nil {
writeTransactionError(w, method, err)
return
}
p.Context.Transaction = tx
err = p.UpdateBlock(b)
if err != nil {
log.IfErr(tx.Rollback())
writeGeneralSQLError(w, method, err)
return
}
log.IfErr(tx.Commit())
writeSuccessEmptyJSON(w)
}