|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Linq; |
7 | 7 | using System.Linq.Dynamic; |
| 8 | +using System.Web; |
| 9 | +using System.Web.Security; |
8 | 10 |
|
9 | 11 | namespace BlogEngine.Core.Data |
10 | 12 | { |
@@ -104,7 +106,45 @@ public CommentItem Add(CommentItem item) |
104 | 106 | if (!Security.IsAuthorizedTo(Rights.CreateComments)) |
105 | 107 | throw new UnauthorizedAccessException(); |
106 | 108 |
|
107 | | - return null; |
| 109 | + var c = new Comment(); |
| 110 | + try |
| 111 | + { |
| 112 | + var post = Post.Posts.Where(p => p.Id == item.PostId).FirstOrDefault(); |
| 113 | + |
| 114 | + c.Id = Guid.NewGuid(); |
| 115 | + c.ParentId = item.ParentId; |
| 116 | + c.IsApproved = item.IsApproved; |
| 117 | + c.Content = HttpUtility.HtmlAttributeEncode(item.Content); |
| 118 | + |
| 119 | + if (string.IsNullOrEmpty(item.Author)) |
| 120 | + { |
| 121 | + c.Author = Security.CurrentUser.Identity.Name; |
| 122 | + var profile = AuthorProfile.GetProfile(c.Author); |
| 123 | + if(profile != null && !string.IsNullOrEmpty(profile.DisplayName)) |
| 124 | + { |
| 125 | + c.Author = profile.DisplayName; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (string.IsNullOrEmpty(item.Email)) |
| 130 | + c.Email = Membership.Provider.GetUser(Security.CurrentUser.Identity.Name, true).Email; |
| 131 | + |
| 132 | + c.IP = Utils.GetClientIP(); |
| 133 | + c.DateCreated = DateTime.Now; |
| 134 | + c.Parent = post; |
| 135 | + |
| 136 | + post.AddComment(c); |
| 137 | + post.Save(); |
| 138 | + |
| 139 | + var newComm = post.Comments.Where(cm => cm.Content == c.Content).FirstOrDefault(); |
| 140 | + |
| 141 | + return Json.GetComment(newComm, post.Comments); |
| 142 | + } |
| 143 | + catch (Exception ex) |
| 144 | + { |
| 145 | + Utils.Log("Core.Data.CommentsRepository.Add", ex); |
| 146 | + return null; |
| 147 | + } |
108 | 148 | } |
109 | 149 |
|
110 | 150 | /// <summary> |
@@ -140,6 +180,7 @@ public bool Update(CommentItem item, string action) |
140 | 180 | return true; |
141 | 181 | } |
142 | 182 |
|
| 183 | + c.Content = item.Content; |
143 | 184 | c.Author = item.Author; |
144 | 185 | c.Email = item.Email; |
145 | 186 | c.Website = string.IsNullOrEmpty(item.Website) ? null : new Uri(item.Website); |
@@ -176,11 +217,11 @@ public bool Update(CommentItem item, string action) |
176 | 217 | public bool Remove(Guid id) |
177 | 218 | { |
178 | 219 | if (!Security.IsAuthorizedTo(Rights.ModerateComments)) |
179 | | - throw new System.UnauthorizedAccessException(); |
| 220 | + throw new UnauthorizedAccessException(); |
180 | 221 |
|
181 | 222 | foreach (var p in Post.Posts.ToArray()) |
182 | 223 | { |
183 | | - BlogEngine.Core.Comment item = (from cmn in p.AllComments |
| 224 | + Comment item = (from cmn in p.AllComments |
184 | 225 | where cmn.Id == id select cmn).FirstOrDefault(); |
185 | 226 |
|
186 | 227 | if (item != null) |
|
0 commit comments