コード例 #1
0
        public bool createNewTopic(Topic topic, int sectionId, string userId)
        {
            using (var dbContext = new ApplicationDbContext())
            {
                try
                {
                    topic.Owner     = dbContext.Users.Find(userId);
                    topic.IsOpen    = true;
                    topic.SectionId = this.GetSection(sectionId);
                    topic.Date      = DateTime.Now;
                    dbContext.Topics.Add(topic);
                    XmlPost newTopic = new XmlPost {
                        id = topic.Id, Date = DateTime.Now, Owner = userId, content = topic.PrimaryPost
                    };
                    dbContext.SaveChanges();
                    XmlPost.addPost(newTopic, topic.Id);

                    return(true);
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }
            }
            return(false);
        }
コード例 #2
0
        public static void addPost(XmlPost post, int topicId)
        {
            List <XmlPost> posts = XmlPost.getPosts(topicId);

            posts.Add(post);
            XmlRootAttribute oRootAttr = new XmlRootAttribute()
            {
                ElementName = "Posts", IsNullable = true
            };
            XmlSerializer oSerializer   = new XmlSerializer(typeof(List <XmlPost>), oRootAttr);
            StreamWriter  oStreamWriter = null;

            try
            {
                oStreamWriter = new StreamWriter("TopicsStorage/" + topicId + ".xml");
                oSerializer.Serialize(oStreamWriter, posts);
            }
            catch (Exception oException)
            {
                Console.WriteLine("Aplikacja wygenerowała następujący wyjątek: " + oException.Message);
            }
            finally
            {
                if (null != oStreamWriter)
                {
                    oStreamWriter.Dispose();
                }
            }
        }
コード例 #3
0
 public List <XmlPost> GetPosts(int id)
 {
     using (var dbContext = new ApplicationDbContext())
     {
         var topicInfo = dbContext.Topics.FirstOrDefault(t => t.Id == id);
         return(XmlPost.getPosts(id));
     }
 }