public ActionResult Edit( DocumentBranchEditViewModel Model )
 {
     if ( Model == null ) {
         return this.View( "NotFound" );
     }
     return this.SaveBranchNode( Model );
 }
 public ActionResult Add( int id = 0 )
 {
     int parentDocumentId = id;
     if ( parentDocumentId < Document.ROOT_NODE_ID ) {
         parentDocumentId = Document.ROOT_NODE_ID;
     }
     if ( !this.documentRepository.NodeExists( parentDocumentId, ActiveOnly: false ) ) {
         return this.View( "NotFound" );
     }
     DocumentBranchEditViewModel model = new DocumentBranchEditViewModel {
         ParentDocumentId = parentDocumentId
     };
     this.SetupBreadcrumb( model );
     return this.View( "Edit", model );
 }
 private void BranchModelToNode( DocumentBranchEditViewModel Model, Document Node )
 {
     Node.Name = Model.Name;
     Node.Slug = this.documentService.SanitizeSlug( Model.Slug, Model.Name, Document.SLUG_MAX_LENGTH );
     Node.IsActive = Model.IsActive;
 }
 private void SetupBreadcrumb( DocumentBranchEditViewModel Model )
 {
     int nodeId = Model.DocumentId;
     if ( nodeId < 1 ) {
         nodeId = Model.ParentDocumentId;
     }
     Model.LastNodeIsLink = true;
     Model.PathNodes = this.documentService.GetParents( nodeId );
 }
        private ActionResult SaveBranchNode( DocumentBranchEditViewModel Model )
        {
            if ( Model == null ) {
                // Fix your errors and try again
                return this.View( "NotFound" );
            }
            if ( Model.DocumentId == Document.ROOT_NODE_ID ) {
                this.ModelState.AddModelError( "DocumentId", "Can't set root node" );
            }
            if ( !this.documentRepository.NodeExists( Model.ParentDocumentId, ActiveOnly: false ) ) {
                this.ModelState.AddModelError( "ParentDocumentId", "Node has an invalid parent" );
            }
            if ( !this.ModelState.IsValid ) {
                // Fix your errors and try again
                this.SetupBreadcrumb( Model );
                return this.View( "Edit", Model );
            }

            Document node = null;
            if ( Model.DocumentId > 0 ) {
                node = this.documentRepository.GetNode( Model.DocumentId, false );
                if ( node == null ) {
                    return this.View( "NotFound" );
                }
            } else {
                node = new Document {
                    NodeType = NodeType.Branch,
                    ParentDocumentId = Model.ParentDocumentId
                };
            }

            this.BranchModelToNode( Model, node );
            bool uniqueSlug = this.documentRepository.SlugUniqueAmongSiblings( node.ParentDocumentId, node.Slug, node.DocumentId );
            if ( !uniqueSlug ) {
                this.ModelState.AddModelError( "Slug", "Slug is not unique for nodes in this category" );
                return this.View( "Edit", Model );
            }

            this.documentService.SaveModifiedNode( node );

            // Saved successfully
            return this.RedirectToAction( "Index", "Document" );
        }