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); }
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(); } } }
public List <XmlPost> GetPosts(int id) { using (var dbContext = new ApplicationDbContext()) { var topicInfo = dbContext.Topics.FirstOrDefault(t => t.Id == id); return(XmlPost.getPosts(id)); } }