public void Update(Guid paragraphId, Paragraph newParagraphData)
 {
     if (ParagraphRepository.Exists(paragraphId))
     {
         Paragraph paragraphToUpdate = ParagraphRepository.GetById(paragraphId);
         if (paragraphToUpdate.Position != newParagraphData.Position)
         {
             IEnumerable <Paragraph> paragraphsInDocument = ParagraphRepository.GetAllByDocument(paragraphToUpdate.DocumentThatBelongs.Id);
             int newPosition = newParagraphData.Position;
             int maxPosition = paragraphsInDocument.Count() - 1;
             if (newPosition > maxPosition || newPosition < 0)
             {
                 throw new InvalidPositionException("This position is not valid for the document in question.");
             }
             else
             {
                 Paragraph swappedParagraph = paragraphsInDocument.First(p => p.Position == newPosition);
                 swappedParagraph.Position = paragraphToUpdate.Position;
                 ParagraphRepository.Update(swappedParagraph);
                 paragraphToUpdate.Position = newPosition;
             }
         }
         if (newParagraphData.StyleClass != null && !StyleClassRepository.Exists(newParagraphData.StyleClass.Name))
         {
             newParagraphData.StyleClass = null;
         }
         paragraphToUpdate.StyleClass = newParagraphData.StyleClass;
         ParagraphRepository.Update(paragraphToUpdate);
     }
     else
     {
         throw new MissingParagraphException("This paragraph is not in the database.");
     }
 }
 public void Update(Guid documentId, Document newDocumentData)
 {
     if (DocumentRepository.Exists(documentId))
     {
         Document documentToUpdate = DocumentRepository.GetById(documentId);
         documentToUpdate.LastModification = DateTime.Now;
         if (newDocumentData.StyleClass != null && !StyleClassRepository.Exists(newDocumentData.StyleClass.Name))
         {
             newDocumentData.StyleClass = null;
         }
         documentToUpdate.StyleClass = newDocumentData.StyleClass;
         documentToUpdate.Title      = newDocumentData.Title;
         DocumentRepository.Update(documentId, documentToUpdate);
     }
     else
     {
         throw new MissingDocumentException("The document is not on the database.");
     }
 }
        public Document Add(string userEmail, Document document)
        {
            if (UserRepository.Exists(userEmail))
            {
                document.Id               = Guid.NewGuid();
                document.CreationDate     = DateTime.Now;
                document.LastModification = document.CreationDate;
                document.Creator          = UserRepository.GetByEmail(userEmail);
                if (document.StyleClass != null && !StyleClassRepository.Exists(document.StyleClass.Name))
                {
                    document.StyleClass = null;
                }

                DocumentRepository.Add(document);

                return(document);
            }
            else
            {
                throw new MissingUserException("The user is not on the database.");
            }
        }
        public Paragraph Add(Guid documentId, Paragraph paragraph)
        {
            if (DocumentRepository.Exists(documentId))
            {
                Document documentThatBelongs = DocumentRepository.GetById(documentId);
                documentThatBelongs.StyleClass = null;

                paragraph.DocumentThatBelongs = documentThatBelongs;

                paragraph.Id = Guid.NewGuid();

                paragraph.Content = new Content()
                {
                    Id = Guid.NewGuid()
                };

                IEnumerable <Paragraph> existingParagraphs = ParagraphRepository.GetAllByDocument(documentId);
                int newMaxPosition = existingParagraphs.Count();

                paragraph.Position = newMaxPosition;

                if (paragraph.StyleClass != null && !StyleClassRepository.Exists(paragraph.StyleClass.Name))
                {
                    paragraph.StyleClass = null;
                }

                ContentRepository.Add(paragraph.Content);
                ParagraphRepository.Add(paragraph);

                return(paragraph);
            }
            else
            {
                throw new MissingDocumentException("This document is not in the database.");
            }
        }