/// <summary> /// Delete comment /// Called from comment management /// </summary> /// <param name="comment"> /// The Comment /// </param> public void DeleteComment(Comment comment) { string path = this.GetFilePath(comment.FileID); XElement root = null; try { root = XElement.Load(path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } var qry = from elem in root.Element("Comments").Elements("Comment") where elem.Attribute("ID").Value == comment.ID select elem; XElement commentElem = qry.First<XElement>(); if (commentElem == null) { string msg = string.Format(NO_COMMENT_ERROR, comment.ID); Logger.Log(msg); throw new ApplicationException(msg); } commentElem.Remove(); try { int count = int.Parse(root.Attribute("Comments").Value); count--; root.SetAttributeValue("Comments", count); } catch (Exception ex) { string msg = string.Format(COUNT_FORMAT_ERROR, comment.FileID); Logger.Log(msg, ex); } root.Save(path); }
/// <summary> /// Gets the post data /// Called when displaying a post /// </summary> /// <param name="fileID"> /// Post ID /// </param> /// <returns> /// The Post /// </returns> public Post Load(string fileID) { string path = this.GetFilePath(fileID); XElement root = null; try { root = XElement.Load(path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } Post post = new Post(); XElement postElem = root; try { post.FileID = postElem.Attribute("FileID").Value; post.Title = postElem.Attribute("Title").Value; post.Author = postElem.Attribute("Author").Value; post.CatID = postElem.Attribute("CatID").Value; try { post.Time = DateTime.ParseExact( postElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); } catch (Exception ex) { string msg = string.Format(DATE_FORMAT_ERROR, fileID); Logger.Log(msg, ex); post.Time = DateTime.MinValue; } XElement docsElem = postElem.Element("Documents"); if (docsElem != null) { foreach (XElement docElem in docsElem.Elements("Document")) { Document document = new Document(); document.Path = docElem.Attribute("Path").Value; post.Documents.Add(document); } } string contents = HttpContext.Current.Server.HtmlDecode(GetInnerXml(postElem.Element("Content"))); post.Contents = contents; XElement commentsElem = postElem.Element("Comments"); if (commentsElem != null) { foreach (XElement commentElem in commentsElem.Elements("Comment")) { Comment comment = new Comment(); comment.FileID = post.FileID; comment.ID = commentElem.Attribute("ID").Value; comment.Name = commentElem.Attribute("Name").Value; comment.Text = HttpContext.Current.Server.HtmlDecode(GetInnerXml(commentElem)); // comment.Text = HttpContext.Current.Server.HtmlDecode(comment.Text); comment.Url = commentElem.Attribute("Url").Value; comment.Time = DateTime.ParseExact( commentElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); comment.Ip = commentElem.Attribute("Ip").Value; if (commentElem.Attribute("IsAuthor") != null) { comment.IsAuthor = bool.Parse(commentElem.Attribute("IsAuthor").Value); } post.Comments.Add(comment); } } } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } return post; }
/// <summary> /// The update comment. /// </summary> /// <param name="comment"> /// The comment. /// </param> /// <exception cref="ApplicationException"> /// Application exception /// </exception> public void UpdateComment(Comment comment) { string path = this.GetFilePath(comment.FileID); XElement root = null; try { root = XElement.Load(path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } var qry = from elem in root.Element("Comments").Elements("Comment") where elem.Attribute("ID").Value == comment.ID select elem; if (qry.Count<XElement>() != 1) { string msg = string.Format(NO_COMMENT_ERROR, comment.ID); Logger.Log(msg); throw new ApplicationException(msg); } XElement commentElem = qry.First<XElement>(); commentElem.SetAttributeValue("Name", comment.Name); commentElem.SetAttributeValue("Url", comment.Url ?? string.Empty); commentElem.SetAttributeValue("IsAuthor", comment.IsAuthor); commentElem.Value = comment.Text; root.Save(path); }
/// <summary> /// Loads draft /// Called when creating a post /// </summary> /// <param name="draftID"> /// Draft ID /// </param> /// <returns> /// Draft Post /// </returns> public DraftPost Load(string draftID) { string path = this.GetFilePath(draftID); XElement root = null; try { root = XElement.Load(path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } XElement postElem = root; DraftPost post = new DraftPost(); try { post.FileID = postElem.Attribute("FileID").Value; post.DraftID = draftID; post.Title = postElem.Attribute("Title").Value; post.Author = postElem.Attribute("Author").Value; post.CatID = postElem.Attribute("CatID").Value; XAttribute attr = postElem.Attribute("Type"); if (attr != null) { post.Type = (PostType)Enum.Parse(typeof(PostType), attr.Value, true); } // Load draft data, if we are loading a draft if (post.Type == PostType.Draft) { if (postElem.Attribute("OldCatID") != null) { post.OldCatID = postElem.Attribute("OldCatID").Value; } } try { post.Time = DateTime.ParseExact( postElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); } catch (Exception ex) { string msg = string.Format(DATE_FORMAT_ERROR, draftID); Logger.Log(msg, ex); post.Time = DateTime.MinValue; } XElement docsElem = postElem.Element("Documents"); if (docsElem != null) { foreach (XElement docElem in docsElem.Elements("Document")) { Document document = new Document(); document.Path = docElem.Attribute("Path").Value; post.Documents.Add(document); } } // string contents = HttpContext.Current.Server.HtmlDecode(postElem.Element("Content").Value); string contents = HttpContext.Current.Server.HtmlDecode(GetInnerXml(postElem.Element("Content"))); post.Contents = contents; XElement commentsElem = postElem.Element("Comments"); if (commentsElem != null) { foreach (XElement commentElem in commentsElem.Elements("Comment")) { Comment comment = new Comment(); comment.FileID = post.FileID; comment.ID = commentElem.Attribute("ID").Value; comment.Name = commentElem.Attribute("Name").Value; comment.Text = HttpContext.Current.Server.HtmlDecode(GetInnerXml(commentElem)); comment.Url = commentElem.Attribute("Url").Value; comment.Time = DateTime.ParseExact( commentElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); comment.Ip = commentElem.Attribute("Ip").Value; post.Comments.Add(comment); } } } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } return post; }
/// <summary> /// Inserts comment into a post /// Called when user enters a comment /// </summary> /// <param name="comment"> /// The Comment /// </param> public void InsertComment(Comment comment) { string path = this.GetFilePath(comment.FileID); XElement root = null; try { root = XElement.Load(path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } XElement commentsElem = root.Element("Comments"); if (commentsElem != null) { XElement commentElem = new XElement( "Comment", new XAttribute("ID", comment.ID), new XAttribute("Name", comment.Name), new XAttribute("Url", comment.Url), new XAttribute( "Time", comment.Time.ToString(DataContext.DateTimeFormat, CultureInfo.InvariantCulture)), new XAttribute("Ip", comment.Ip), new XAttribute("IsAuthor", comment.IsAuthor)); commentElem.Value = comment.Text; commentsElem.AddFirst(commentElem); } try { int count = int.Parse(root.Attribute("Comments").Value); count++; root.SetAttributeValue("Comments", count); } catch (Exception ex) { string msg = string.Format(COUNT_FORMAT_ERROR, comment.FileID); Logger.Log(msg, ex); } root.Save(path); }
public ActionResult Edit(Comment comment) { if (string.IsNullOrEmpty(comment.Name)) { this.ModelState.AddModelError("Name", "评论人不能为空"); } if (string.IsNullOrEmpty(comment.Text)) { this.ModelState.AddModelError("Text", "评论内容不能为空"); } comment.Url = comment.Url ?? string.Empty; try { comment.Text = comment.Text.Replace(" ", " "); CommentComp.Update(comment); } catch (Exception ex) { this.ModelState.AddModelError("Update", "更新错误"); Logger.Log("Update failed", ex); } if (this.ModelState.IsValid) { this.TempData["Message"] = "评论已更新"; // Find out the page index int pageSize = 10; int rowIndex = CommentComp.GetComments().FindIndex(c => c.ID == comment.ID); int pageIndex = (rowIndex / pageSize) + 1; if (pageIndex == 1) { return this.RedirectToAction("Manage"); } else { return this.RedirectToAction("Manage", new { page = pageIndex }); } } this.ViewData.Model = comment; return this.View(); }
/// <summary> /// The update. /// </summary> /// <param name="comment"> /// The comment. /// </param> public static void Update(Comment comment) { ConfigHelper.DataContext.CommentData.Update(comment); if (comment.IsApproved) { ConfigHelper.DataContext.PostData.UpdateComment(comment); } }
/// <summary> /// The insert. /// </summary> /// <param name="fileID"> /// The file id. /// </param> /// <param name="name"> /// The name. /// </param> /// <param name="url"> /// The url. /// </param> /// <param name="text"> /// The text. /// </param> /// <param name="isAuthor"> /// The is author. /// </param> public static void Insert(string fileID, string name, string url, string text, bool isAuthor = false) { bool commentModeration = SettingsComp.GetSettings().CommentModeration; Comment comment = new Comment(); comment.FileID = fileID; comment.Name = name; comment.Url = url; comment.Text = text; TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(SettingsComp.GetSettings().Timezone); comment.Time = LocalTime.GetCurrentTime(tzi); comment.Ip = HttpContext.Current.Request.UserHostAddress; comment.IsAuthor = isAuthor; if (commentModeration) { comment.IsApproved = false; } else { comment.IsApproved = true; } comment.ID = ConfigHelper.DataContext.CommentData.Insert(comment); if (!commentModeration) { ConfigHelper.DataContext.PostData.InsertComment(comment); } }
/// <summary> /// Inserts comment into XML /// </summary> /// <param name="comment"> /// The Comment /// </param> /// <returns> /// Comment ID /// </returns> public string Insert(Comment comment) { XElement root = null; try { root = XElement.Load(this._path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } int id = 0; try { id = int.Parse(root.Attribute("TotalComments").Value); } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); } id++; root.Attribute("TotalComments").Value = id.ToString(); XElement commentElem = new XElement( "Comment", new XAttribute("ID", id), new XAttribute("FileID", comment.FileID), new XAttribute("Name", comment.Name), new XAttribute("Url", comment.Url), new XAttribute("Ip", comment.Ip), new XAttribute("Time", comment.Time.ToString(DataContext.DateTimeFormat, CultureInfo.InvariantCulture)), new XAttribute("IsAuthor", comment.IsAuthor), new XAttribute("Approved", comment.IsApproved)); // commentElem.Value = HttpContext.Current.Server.HtmlEncode(comment.Text); commentElem.Value = comment.Text; try { root.AddFirst(commentElem); int commentCount = root.Elements("Comment").Count<XElement>(); if (commentCount > 100) { List<XElement> removes = new List<XElement>(); for (int index = 100; index < commentCount; index++) { removes.Add(root.Elements("Comment").ElementAt(index)); } foreach (XElement remove in removes) { remove.Remove(); } } // root.Save(_path); XmlHelper.Save(root, this._path); } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } return id.ToString(); }
/// <summary> /// Get list of comments /// Called by comment managment /// Called by recent comments /// </summary> /// <returns>List of comments</returns> public List<Comment> GetComments() { List<Comment> comments = new List<Comment>(); XElement root = null; try { root = XElement.Load(this._path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } try { foreach (XElement commentElem in root.Elements("Comment")) { Comment comment = new Comment(); comment.ID = commentElem.Attribute("ID").Value; comment.FileID = commentElem.Attribute("FileID").Value; comment.Name = commentElem.Attribute("Name").Value; comment.Ip = commentElem.Attribute("Ip").Value; comment.Url = commentElem.Attribute("Url").Value; // comment.Text = HttpContext.Current.Server.HtmlDecode(commentElem.Value); comment.Text = HttpContext.Current.Server.HtmlDecode(GetInnerXml(commentElem)); // comment.Text = HttpContext.Current.Server.HtmlDecode(comment.Text); try { comment.IsApproved = bool.Parse(commentElem.Attribute("Approved").Value); if (commentElem.Attribute("IsAuthor") != null) { comment.IsAuthor = bool.Parse(commentElem.Attribute("IsAuthor").Value); } comment.Time = DateTime.ParseExact( commentElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); } catch (Exception ex) { string msg = string.Format(DATE_FORMAT_ERROR, comment.ID); Logger.Log(msg, ex); comment.Time = DateTime.MinValue; } comments.Add(comment); } } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } return comments; }