Exemplo n.º 1
0
        public ActionResult Edit(Node node, int ParentNodeID = -1, int TreeLevel = -1)
        {
            int           LinesNoOf, SentencesNoOf, ParagraphsNoOf;
            List <string> lines, sentences, paragrphs, newParagraphs;
            Paragraphs    paragraphs;
            List <int>    SentenceInParagraph;

            SentenceInParagraph = new List <int>();
            paragrphs           = new List <string>();
            sentences           = new List <string>();
            lines         = new List <string>();
            newParagraphs = new List <string>();
            paragraphs    = new Paragraphs();

            ViewBag.SelectedNodeHeading = node.Heading;

            paragraphs.TheText = node.NodeText;

            paragraphs.NoOfChars = paragraphs.TheText.Length;
            paragraphs.Paragrphs(out ParagraphsNoOf, ref paragrphs, out SentencesNoOf, ref sentences, ref SentenceInParagraph, out LinesNoOf, ref lines, 0, false, true, true, false, true, false, false);

            node.NodeText = paragraphs.TheAlteredText;

            if (ParentNodeID != -1)
            {
                node.TreeID       = 2;
                node.TypeID       = 5;
                node.ParentNodeID = ParentNodeID;
                node.TreeLevel    = (short)TreeLevel;
            }
            if (ModelState.IsValid)
            {
                if (node.Heading.Length > 50)
                {
                    node.Heading = node.Heading.Substring(0, 50);
                }
                ;
                if (node.NodeID == 0)
                {
                    repository.Edit(node);
                    TempData["message"] = string.Format("Node: {0}, {1} ... has been created", node.NodeID, node.Heading);
                }
                else
                {
                    repository.Edit(node);
                    TempData["message"] = string.Format("Node: {0}, {1} ... has been edited", node.NodeID, node.Heading);
                }
                return(RedirectToAction("Index", "Book", new { NodeID = node.NodeID }));
            }
            else
            {
                // there is something wrong with the data values
                return(View(node));
            }
        }
Exemplo n.º 2
0
        public async Task<BlResult<NodeModel>> SaveAsync(NodeModel nodeModel)
        {
            BlResult<NodeModel> blResult = new BlResult<NodeModel>();

            try
            {
                if (nodeModel is null)
                {
                    throw new ArgumentNullException(nameof(NodeModel));
                }

                EnsureTransaction();

                var fetchedEntity = await _nodeRepository.GetByIdAsync(nodeModel.Id);
                fetchedEntity = _mapper.Map(nodeModel, fetchedEntity);
                var attributesToDelete = fetchedEntity.NodeAttributes.
                  Where(x => !nodeModel.NodeAttributes.Any(y => y.Id == x.Id)).ToList();
                var attributesToAdd = nodeModel.NodeAttributes.
                    Where(x => !fetchedEntity.NodeAttributes.Any(y => y.Id == x.Id)).Select(z => z.Id);

                if (nodeModel.Id > 0)
                {
                    _nodeRepository.Edit(fetchedEntity);
                }
                else
                {
                    fetchedEntity = _nodeRepository.Add(fetchedEntity);
                }

                await SaveChangesAsync(false);

                if (attributesToDelete != null && attributesToDelete.Count() > 0)
                {
                    foreach (var item in attributesToDelete)
                    {
                        fetchedEntity.NodeAttributes.Remove(item);
                    }
                    await SaveChangesAsync(false);
                }

                if (attributesToAdd != null && attributesToAdd.Count() > 0)
                {
                    await _nodeRepository.AddAttribuets(fetchedEntity, attributesToAdd);
                    await SaveChangesAsync(false);
                }

                await SaveChangesAsync();

                blResult.Success(_mapper.Map<NodeModel>(fetchedEntity));

            }
            catch (ArgumentNullException)
            {
                blResult.Fail(BLErrorCodeTypeEnum.ArgumentIsNull);
            }
            catch (Exception ex)
            {
                blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex.Message, ex);
            }

            return blResult;
        }