public virtual EntityResult AssociateTopic(int parentId, int childId)
        {
            // TODO throw errors
            if (!DbContext.Topics.Any(t => t.Id == childId))
            {
                return(EntityResult.Error("Child not Found"));
            }
            if (!DbContext.Topics.Any(t => t.Id == parentId))
            {
                return(EntityResult.Error("Parent not Found"));
            }

            if (DbContext.AssociatedTopics.Any(at => at.ChildTopicId == childId && at.ParentTopicId == parentId))
            {
                return(EntityResult.Error("Allready exists"));
            }

            var relation = new AssociatedTopic()
            {
                ChildTopicId = childId, ParentTopicId = parentId
            };

            DbContext.Add(relation);
            DbContext.SaveChanges();
            return(EntityResult.Successfull(null));
        }
Beispiel #2
0
        public EntityResult CreateAttachment(int topicId, string identity, AttachmentFormModel model)
        {
            if (!DbContext.Topics.Include(t => t.TopicUsers).Any(t => t.Id == topicId))
            {
                return(EntityResult.Error("Unknown Topic"));
            }

            try
            {
                var user        = GetUserByIdentity(identity);
                var attatchment = new TopicAttachment(model)
                {
                    UserId  = user.Id,
                    TopicId = topicId,
                    Type    = "TODO"
                };

                DbContext.TopicAttachments.Add(attatchment);
                DbContext.SaveChanges();

                return(EntityResult.Successfull(attatchment.Id));
            }
            catch (Exception e)
            {
                return(EntityResult.Error(e.Message));
            }
        }
Beispiel #3
0
        public EntityResult CreateAttachment(int topicId, int userId, AttatchmentFormModel model)
        {
            try
            {
                DbContext.Topics.Include(t => t.TopicUsers).Single(t => t.Id == topicId);
            }
            catch (InvalidOperationException)
            {
                return(EntityResult.Error("Unknown Topic"));
            }

            try
            {
                var attatchment = new TopicAttatchment(model)
                {
                    UserId  = userId,
                    TopicId = topicId,
                    Type    = "TODO"
                };

                DbContext.TopicAttatchments.Add(attatchment);
                DbContext.SaveChanges();

                return(EntityResult.Successfull(attatchment.Id));
            }
            catch (Exception e)
            {
                return(EntityResult.Error(e.Message));
            }
        }
Beispiel #4
0
        internal EntityResult CreateLegal(int topicId, int attachmentId, int userId, LegalFormModel legalModel)
        {
            try
            {
                DbContext.TopicAttatchments.Include(at => at.Legal).Single(at => at.Id == attachmentId);
            }
            catch (InvalidOperationException)
            {
                return(EntityResult.Error("Unknown Attachment"));
            }
            // TODO already exitst´s
            try
            {
                Legal legal = new Legal(attachmentId, legalModel);

                DbContext.Add(legal);
                DbContext.SaveChanges();

                return(EntityResult.Successfull());
            }
            catch (Exception e)
            {
                return(EntityResult.Error(e.Message));
            }
        }
Beispiel #5
0
        public EntityResult AddTag(TagFormModel tagModel)
        {
            var tag = new AnnotationTag(tagModel);

            DbContext.AnnotationTags.Add(tag);
            DbContext.SaveChanges();

            return(EntityResult.Successfull(tag.Id));
        }
Beispiel #6
0
        public EntityResult PutAttachment(int attachmentId, string identity, IFormFile file)
        {
            TopicAttachment attachment;

            try
            {
                attachment = DbContext.TopicAttachments.Include(t => t.Topic).ThenInclude(t => t.TopicUsers).Single(t => t.Id == attachmentId);
            }
            catch (InvalidOperationException)
            {
                return(EntityResult.Error("Unknown Attachment"));
            }

            var topicFolder = Path.Combine(Constants.AttachmentPath, attachment.TopicId.ToString());

            if (!Directory.Exists(topicFolder))
            {
                Directory.CreateDirectory(topicFolder);
            }

            var fileName = (Path.Combine(topicFolder, file.FileName));

            using (var outputStream = new FileStream(fileName, FileMode.Create))
            {
                file.CopyTo(outputStream);
            }
            try
            {
                attachment.Path = file.FileName;
                attachment.Type = "TODO";

                DbContext.Update(attachment);
                DbContext.SaveChanges();

                new NotificationProcessor(DbContext, attachment.Topic, identity).OnAttachmetAdded(attachment.Title);

                return(EntityResult.Successfull(attachment.Id));
            }
            catch (Exception e)
            {
                return(EntityResult.Error(e.Message));
            }
        }
Beispiel #7
0
        public EntityResult AddTopic(int userId, TopicFormModel model)
        {
            try
            {
                var topic = new Topic(model)
                {
                    CreatedById = userId
                };
                DbContext.Topics.Add(topic);
                DbContext.SaveChanges();
                new NotificationProcessor(DbContext, topic, userId).OnNewTopic();

                return(EntityResult.Successfull(topic.Id));
            }
            catch (Exception e)
            {
                return(EntityResult.Error(e.Message));
            }
        }
Beispiel #8
0
        internal EntityResult UpdateDocument(int topicId, int userId, string htmlContent)
        {
            try
            {
                DbContext.Topics.Include(t => t.Document).Single(t => t.Id == topicId);
            }
            catch (InvalidOperationException)
            {
                return(EntityResult.Error("Unknown Topic"));
            }
            // already exitsts


            Document document;

            try
            {
                document           = GetDocumentById(topicId);
                document.UpdaterId = userId;
                document.Content   = htmlContent;
            }
            catch (InvalidOperationException)
            {
                document = new Document(topicId, userId, htmlContent);
                DbContext.Add(document);
            }

            // document is saved, so now we can parse it
            var stream       = new System.IO.StringReader("<pseudo-root>" + htmlContent + "</pseudo-root>");
            var xmlReader    = XmlReader.Create(stream);
            var tagInstances = new List <AnnotationTagInstance>();

            try
            {
                while (xmlReader.Read())
                {
                    if (xmlReader.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }
                    if (!xmlReader.HasAttributes)
                    {
                        continue;
                    }

                    var tagModelId    = int.Parse(xmlReader.GetAttribute("data-tag-model-id"));
                    var tagInstanceId = int.Parse(xmlReader.GetAttribute("data-tag-id"));
                    var tagValue      = xmlReader.ReadElementContentAsString();
                    var rx            = new Regex("<span[^>]+?data-tag-id=\"" + tagInstanceId + "\".*?>");
                    var tagPosition   = rx.Match(htmlContent).Index;

                    var tagModel = DbContext.AnnotationTags.First(t => t.Id == tagModelId);
                    var tag      = new AnnotationTagInstance(tagModel)
                    {
                        IdInDocument       = tagInstanceId,
                        Value              = tagValue,
                        PositionInDocument = tagPosition,
                        Document           = document
                    };

                    tagInstances.Add(tag);
                }
                if (DbContext.AnnotationTagInstances.Any(i => i.Document == document))
                {
                    var oldInstances = DbContext.AnnotationTagInstances.Where(i => i.Document == document);
                    DbContext.AnnotationTagInstances.RemoveRange(oldInstances);
                }
                DbContext.AnnotationTagInstances.AddRange(tagInstances);
            }
            catch (Exception)
            {
                return(EntityResult.Error("Parsing Error"));
            }

            try
            {
                DbContext.SaveChanges();
                return(EntityResult.Successfull());
            }
            catch (Exception e)
            {
                return(EntityResult.Error(e.Message));
            }
        }