@@ -427,6 +427,54 @@ func appendBlock(c *gin.Context) {
427427 broadcastTransactions (transactions )
428428}
429429
430+ func batchAppendBlock (c * gin.Context ) {
431+ ret := gulu .Ret .NewResult ()
432+ defer c .JSON (http .StatusOK , ret )
433+
434+ arg , ok := util .JsonArg (c , ret )
435+ if ! ok {
436+ return
437+ }
438+
439+ blocksArg := arg ["blocks" ].([]interface {})
440+ var transactions []* model.Transaction
441+ luteEngine := util .NewLute ()
442+ for _ , blockArg := range blocksArg {
443+ blockMap := blockArg .(map [string ]interface {})
444+ data := blockMap ["data" ].(string )
445+ dataType := blockMap ["dataType" ].(string )
446+ parentID := blockMap ["parentID" ].(string )
447+ if util .InvalidIDPattern (parentID , ret ) {
448+ return
449+ }
450+ if "markdown" == dataType {
451+ var err error
452+ data , err = dataBlockDOM (data , luteEngine )
453+ if err != nil {
454+ ret .Code = - 1
455+ ret .Msg = "data block DOM failed: " + err .Error ()
456+ return
457+ }
458+ }
459+
460+ transactions = append (transactions , & model.Transaction {
461+ DoOperations : []* model.Operation {
462+ {
463+ Action : "appendInsert" ,
464+ Data : data ,
465+ ParentID : parentID ,
466+ },
467+ },
468+ })
469+ }
470+
471+ model .PerformTransactions (& transactions )
472+ model .FlushTxQueue ()
473+
474+ ret .Data = transactions
475+ broadcastTransactions (transactions )
476+ }
477+
430478func prependBlock (c * gin.Context ) {
431479 ret := gulu .Ret .NewResult ()
432480 defer c .JSON (http .StatusOK , ret )
@@ -472,6 +520,54 @@ func prependBlock(c *gin.Context) {
472520 broadcastTransactions (transactions )
473521}
474522
523+ func batchPrependBlock (c * gin.Context ) {
524+ ret := gulu .Ret .NewResult ()
525+ defer c .JSON (http .StatusOK , ret )
526+
527+ arg , ok := util .JsonArg (c , ret )
528+ if ! ok {
529+ return
530+ }
531+
532+ blocksArg := arg ["blocks" ].([]interface {})
533+ var transactions []* model.Transaction
534+ luteEngine := util .NewLute ()
535+ for _ , blockArg := range blocksArg {
536+ blockMap := blockArg .(map [string ]interface {})
537+ data := blockMap ["data" ].(string )
538+ dataType := blockMap ["dataType" ].(string )
539+ parentID := blockMap ["parentID" ].(string )
540+ if util .InvalidIDPattern (parentID , ret ) {
541+ return
542+ }
543+ if "markdown" == dataType {
544+ var err error
545+ data , err = dataBlockDOM (data , luteEngine )
546+ if err != nil {
547+ ret .Code = - 1
548+ ret .Msg = "data block DOM failed: " + err .Error ()
549+ return
550+ }
551+ }
552+
553+ transactions = append (transactions , & model.Transaction {
554+ DoOperations : []* model.Operation {
555+ {
556+ Action : "prependInsert" ,
557+ Data : data ,
558+ ParentID : parentID ,
559+ },
560+ },
561+ })
562+ }
563+
564+ model .PerformTransactions (& transactions )
565+ model .FlushTxQueue ()
566+
567+ ret .Data = transactions
568+ broadcastTransactions (transactions )
569+ }
570+
475571func insertBlock (c * gin.Context ) {
476572 ret := gulu .Ret .NewResult ()
477573 defer c .JSON (http .StatusOK , ret )
@@ -628,6 +724,72 @@ func updateBlock(c *gin.Context) {
628724 broadcastTransactions (transactions )
629725}
630726
727+ func batchInsertBlock (c * gin.Context ) {
728+ ret := gulu .Ret .NewResult ()
729+ defer c .JSON (http .StatusOK , ret )
730+
731+ arg , ok := util .JsonArg (c , ret )
732+ if ! ok {
733+ return
734+ }
735+
736+ blocksArg := arg ["blocks" ].([]interface {})
737+ var transactions []* model.Transaction
738+ luteEngine := util .NewLute ()
739+ for _ , blockArg := range blocksArg {
740+ blockMap := blockArg .(map [string ]interface {})
741+ data := blockMap ["data" ].(string )
742+ dataType := blockMap ["dataType" ].(string )
743+ var parentID , previousID , nextID string
744+ if nil != blockMap ["parentID" ] {
745+ parentID = blockMap ["parentID" ].(string )
746+ if "" != parentID && util .InvalidIDPattern (parentID , ret ) {
747+ return
748+ }
749+ }
750+ if nil != blockMap ["previousID" ] {
751+ previousID = blockMap ["previousID" ].(string )
752+ if "" != previousID && util .InvalidIDPattern (previousID , ret ) {
753+ return
754+ }
755+ }
756+ if nil != blockMap ["nextID" ] {
757+ nextID = blockMap ["nextID" ].(string )
758+ if "" != nextID && util .InvalidIDPattern (nextID , ret ) {
759+ return
760+ }
761+ }
762+
763+ if "markdown" == dataType {
764+ var err error
765+ data , err = dataBlockDOM (data , luteEngine )
766+ if err != nil {
767+ ret .Code = - 1
768+ ret .Msg = "data block DOM failed: " + err .Error ()
769+ return
770+ }
771+ }
772+
773+ transactions = append (transactions , & model.Transaction {
774+ DoOperations : []* model.Operation {
775+ {
776+ Action : "insert" ,
777+ Data : data ,
778+ ParentID : parentID ,
779+ PreviousID : previousID ,
780+ NextID : nextID ,
781+ },
782+ },
783+ })
784+ }
785+
786+ model .PerformTransactions (& transactions )
787+ model .FlushTxQueue ()
788+
789+ ret .Data = transactions
790+ broadcastTransactions (transactions )
791+ }
792+
631793func batchUpdateBlock (c * gin.Context ) {
632794 ret := gulu .Ret .NewResult ()
633795 defer c .JSON (http .StatusOK , ret )
0 commit comments