PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/App_Code/Entity/BSComment.cs

#
C# | 469 lines | 436 code | 33 blank | 0 comment | 72 complexity | a60ae2ed96277a5f2c13506f11d98be2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Xml.Serialization;
  7. public enum CommentStates : short
  8. {
  9. Approved = 0,
  10. UnApproved = 1,
  11. All = 9
  12. }
  13. [XmlType("Comment")]
  14. public class BSComment
  15. {
  16. #region Properties
  17. private int _CommentID;
  18. public int CommentID
  19. {
  20. get { return _CommentID; }
  21. set { _CommentID = value; }
  22. }
  23. private string _UserName;
  24. public string UserName
  25. {
  26. get { return _UserName; }
  27. set { _UserName = value; }
  28. }
  29. private string _Content;
  30. public string Content
  31. {
  32. get { return _Content; }
  33. set { _Content = value; }
  34. }
  35. private string _Email;
  36. public string Email
  37. {
  38. get { return _Email; }
  39. set { _Email = value; }
  40. }
  41. private DateTime _Date;
  42. public DateTime Date
  43. {
  44. get { return _Date; }
  45. set { _Date = value; }
  46. }
  47. private string _IP;
  48. public string IP
  49. {
  50. get { return _IP; }
  51. set { _IP = value; }
  52. }
  53. private string _WebPage;
  54. public string WebPage
  55. {
  56. get { return _WebPage; }
  57. set { _WebPage = value; }
  58. }
  59. private int _PostID;
  60. public int PostID
  61. {
  62. get { return _PostID; }
  63. set { _PostID = value; }
  64. }
  65. private int _UserID;
  66. public int UserID
  67. {
  68. get { return _UserID; }
  69. set { _UserID = value; }
  70. }
  71. private string _GravatarLink;
  72. public string GravatarLink
  73. {
  74. get { return _GravatarLink; }
  75. set { _GravatarLink = value; }
  76. }
  77. private bool _Approve;
  78. public bool Approve
  79. {
  80. get { return _Approve; }
  81. set { _Approve = value; }
  82. }
  83. public bool NotifyMe
  84. {
  85. get
  86. {
  87. return _Approve;
  88. }
  89. set
  90. {
  91. _Approve = value;
  92. }
  93. }
  94. private bool _isAdmin;
  95. public bool IsAdmin
  96. {
  97. get { return _isAdmin; }
  98. set { _isAdmin = value; }
  99. }
  100. public string Link
  101. {
  102. get
  103. {
  104. BSPost post = BSPost.GetPost(PostID);
  105. if (post != null)
  106. return String.Format("{0}#{1}", post.Link, CommentID);
  107. else
  108. return String.Empty;
  109. }
  110. }
  111. #endregion
  112. #region Events
  113. public static event EventHandler<CancelEventArgs> Showing;
  114. public static void OnShowing(BSComment BsComment, CancelEventArgs e)
  115. {
  116. if (Showing != null)
  117. {
  118. Showing(BsComment, e);
  119. }
  120. }
  121. public static event EventHandler<EventArgs> Showed;
  122. public static void OnShowed(BSComment BsComment, EventArgs e)
  123. {
  124. if (Showed != null)
  125. {
  126. Showed(BsComment, e);
  127. }
  128. }
  129. public static event EventHandler<CancelEventArgs> Adding;
  130. public static void OnAdding(BSComment BsComment, CancelEventArgs e)
  131. {
  132. if (Adding != null)
  133. {
  134. Adding(BsComment, e);
  135. }
  136. }
  137. public static event EventHandler<EventArgs> Added;
  138. public static void OnAdded(BSComment BsComment, EventArgs e)
  139. {
  140. if (Added != null)
  141. {
  142. Added(BsComment, e);
  143. }
  144. }
  145. public static event EventHandler<CancelEventArgs> Approving;
  146. public static void OnApproving(BSComment BsComment, CancelEventArgs e)
  147. {
  148. if (Approving != null)
  149. {
  150. Approving(BsComment, e);
  151. }
  152. }
  153. public static event EventHandler<EventArgs> Approved;
  154. public static void OnApproved(BSComment BsComment, EventArgs e)
  155. {
  156. if (Approved != null)
  157. {
  158. Approved(BsComment, e);
  159. }
  160. }
  161. public static event EventHandler<CancelEventArgs> Saving;
  162. public static void OnSaving(BSComment BsComment, CancelEventArgs e)
  163. {
  164. if (Saving != null)
  165. {
  166. Saving(BsComment, e);
  167. }
  168. }
  169. public static event EventHandler<EventArgs> Saved;
  170. public static void OnSaved(BSComment BsComment, EventArgs e)
  171. {
  172. if (Saved != null)
  173. {
  174. Saved(BsComment, e);
  175. }
  176. }
  177. public static event EventHandler<CancelEventArgs> Deleting;
  178. public static void OnDeleting(BSComment BsComment, CancelEventArgs e)
  179. {
  180. if (Deleting != null)
  181. {
  182. Deleting(BsComment, e);
  183. }
  184. }
  185. public static event EventHandler<EventArgs> Deleted;
  186. public static void OnDeleted(BSComment BsComment, EventArgs e)
  187. {
  188. if (Deleted != null)
  189. {
  190. Deleted(BsComment, e);
  191. }
  192. }
  193. #endregion
  194. #region Methods
  195. public static BSComment GetComment(int iCommentID)
  196. {
  197. using (DataProcess dp = new DataProcess())
  198. {
  199. dp.AddParameter("CommentID", iCommentID);
  200. dp.ExecuteReader("SELECT * FROM Comments WHERE [CommentID]=@CommentID");
  201. if (dp.Return.Status == DataProcessState.Success)
  202. {
  203. using (IDataReader dr = dp.Return.Value as IDataReader)
  204. {
  205. if (dr != null && dr.Read())
  206. {
  207. BSComment bsComment = new BSComment();
  208. FillComment(dr, bsComment);
  209. return bsComment;
  210. }
  211. }
  212. }
  213. }
  214. return null;
  215. }
  216. public static List<BSComment> GetComments(CommentStates state)
  217. {
  218. return GetComments(state, 0);
  219. }
  220. public static List<BSComment> GetComments(CommentStates state, int iCommentCount)
  221. {
  222. List<BSComment> comments = new List<BSComment>();
  223. using (DataProcess dp = new DataProcess())
  224. {
  225. string top = iCommentCount == 0 ? String.Empty : "TOP " + iCommentCount;
  226. if (state == CommentStates.All)
  227. {
  228. dp.ExecuteReader(String.Format("SELECT {0} * FROM Comments ORDER By CreateDate DESC", top));
  229. }
  230. else
  231. {
  232. dp.AddParameter("Approve", state == CommentStates.Approved);
  233. dp.ExecuteReader(String.Format("SELECT {0} * FROM Comments WHERE [Approve]=@Approve ORDER By CreateDate DESC", top));
  234. }
  235. if (dp.Return.Status == DataProcessState.Success)
  236. {
  237. using (IDataReader dr = dp.Return.Value as IDataReader)
  238. {
  239. while (dr != null && dr.Read())
  240. {
  241. BSComment bsComment = new BSComment();
  242. FillComment(dr, bsComment);
  243. comments.Add(bsComment);
  244. }
  245. }
  246. }
  247. }
  248. return comments;
  249. }
  250. public static List<BSComment> GetCommentsByPostID(int iPostID, CommentStates state)
  251. {
  252. List<BSComment> comments = new List<BSComment>();
  253. using (DataProcess dp = new DataProcess())
  254. {
  255. if (state == CommentStates.All)
  256. {
  257. dp.AddParameter("PostID", iPostID);
  258. dp.ExecuteReader("SELECT * FROM Comments WHERE [PostID]=@PostID ORDER By CreateDate DESC");
  259. }
  260. else
  261. {
  262. dp.AddParameter("PostID", iPostID);
  263. dp.AddParameter("Approve", state == CommentStates.Approved);
  264. dp.ExecuteReader("SELECT * FROM Comments WHERE [PostID]=@PostID AND [Approve]=@Approve ORDER By CreateDate DESC");
  265. }
  266. if (dp.Return.Status == DataProcessState.Success)
  267. {
  268. using (IDataReader dr = dp.Return.Value as IDataReader)
  269. {
  270. while (dr != null && dr.Read())
  271. {
  272. BSComment bsComment = new BSComment();
  273. FillComment(dr, bsComment);
  274. comments.Add(bsComment);
  275. }
  276. }
  277. }
  278. }
  279. return comments;
  280. }
  281. public static List<BSComment> GetCommentsByUserID(int iUserID, CommentStates state)
  282. {
  283. List<BSComment> comments = new List<BSComment>();
  284. using (DataProcess dp = new DataProcess())
  285. {
  286. if (state == CommentStates.All)
  287. {
  288. dp.AddParameter("UserID", iUserID);
  289. dp.ExecuteReader("SELECT * FROM Comments WHERE [UserID]=@UserID ORDER By CreateDate DESC");
  290. }
  291. else
  292. {
  293. dp.AddParameter("UserID", iUserID);
  294. dp.AddParameter("Approve", state == CommentStates.Approved);
  295. dp.ExecuteReader("SELECT * FROM Comments WHERE [UserID]=@UserID AND [Approve]=@Approve ORDER By CreateDate DESC");
  296. }
  297. if (dp.Return.Status == DataProcessState.Success)
  298. {
  299. using (IDataReader dr = dp.Return.Value as IDataReader)
  300. {
  301. while (dr != null && dr.Read())
  302. {
  303. BSComment bsComment = new BSComment();
  304. FillComment(dr, bsComment);
  305. comments.Add(bsComment);
  306. }
  307. }
  308. }
  309. }
  310. return comments;
  311. }
  312. public static void Delete(int iCommentID)
  313. {
  314. CancelEventArgs eventArgs = new CancelEventArgs();
  315. BSComment bsComment = GetComment(iCommentID);
  316. BSComment.OnDeleting(bsComment, eventArgs);
  317. if (!eventArgs.Cancel)
  318. {
  319. using (DataProcess dp = new DataProcess())
  320. {
  321. dp.AddParameter("CommentID", iCommentID);
  322. dp.ExecuteReader("DELETE FROM Comments WHERE [CommentID]=@CommentID");
  323. if (dp.Return.Status == DataProcessState.Success)
  324. {
  325. BSComment.OnDeleted(bsComment, EventArgs.Empty);
  326. }
  327. }
  328. }
  329. }
  330. static void FillComment(IDataReader dr, BSComment bsComment)
  331. {
  332. bsComment.CommentID = Convert.ToInt32(dr["CommentID"]);
  333. bsComment.Content = dr["Comment"].ToString();
  334. bsComment.Date = Convert.ToDateTime(dr["CreateDate"]);
  335. bsComment.Email = dr["EMail"].ToString();
  336. bsComment.GravatarLink = BSHelper.GetGravatar(bsComment.Email);
  337. bsComment.IP = dr["IP"].ToString();
  338. bsComment.PostID = Convert.ToInt32(dr["PostID"]);
  339. bsComment.UserID = Convert.ToInt32(dr["UserID"]);
  340. bsComment.UserName = dr["Name"].ToString();
  341. bsComment.WebPage = dr["WebPage"].ToString();
  342. bsComment.Approve = Convert.ToBoolean(dr["Approve"]);
  343. if (bsComment.UserID != 0)
  344. {
  345. BSUser user = BSUser.GetUser(bsComment.UserID);
  346. if (user != null)
  347. {
  348. bsComment.UserName = user.Name;
  349. bsComment.WebPage = user.WebPage;
  350. bsComment.Email = user.Email;
  351. bsComment.IsAdmin = user.Role.Equals("admin");
  352. }
  353. }
  354. }
  355. public bool DoApprove(bool bApprove)
  356. {
  357. CancelEventArgs eventArgs = new CancelEventArgs();
  358. BSComment bsComment = GetComment(CommentID);
  359. OnApproving(bsComment, eventArgs);
  360. if (!eventArgs.Cancel)
  361. {
  362. using (DataProcess dp = new DataProcess())
  363. {
  364. dp.AddParameter("Approve", bApprove);
  365. dp.AddParameter("CommentID", CommentID);
  366. dp.ExecuteNonQuery("UPDATE Comments SET [Approve]=@Approve WHERE [CommentID]=@CommentID");
  367. if (dp.Return.Status == DataProcessState.Success)
  368. {
  369. bsComment.Approve = bApprove;
  370. OnApproved(bsComment, null);
  371. return true;
  372. }
  373. }
  374. }
  375. return false;
  376. }
  377. public bool Remove()
  378. {
  379. using (DataProcess dp = new DataProcess())
  380. {
  381. CancelEventArgs cancelEvent = new CancelEventArgs();
  382. OnDeleting(this, cancelEvent);
  383. if (!cancelEvent.Cancel)
  384. {
  385. dp.AddParameter("CommentID", CommentID);
  386. dp.ExecuteNonQuery("DELETE FROM Comments WHERE [CommentID] = @CommentID");
  387. if (dp.Return.Status == DataProcessState.Success)
  388. {
  389. OnDeleted(this, null);
  390. return true;
  391. }
  392. }
  393. }
  394. return false;
  395. }
  396. public bool Save()
  397. {
  398. bool bReturnValue = false;
  399. using (DataProcess dp = new DataProcess())
  400. {
  401. dp.AddParameter("UserID", UserID);
  402. dp.AddParameter("PostID", PostID);
  403. dp.AddParameter("Name", UserName);
  404. dp.AddParameter("Comment", Content);
  405. dp.AddParameter("Email", Email);
  406. dp.AddParameter("WebPage", WebPage);
  407. dp.AddParameter("Ip", IP);
  408. dp.AddParameter("CreateDate", Date);
  409. dp.AddParameter("Approve", Approve);
  410. dp.AddParameter("NotifyMe", NotifyMe);
  411. if (CommentID != 0)
  412. {
  413. dp.AddParameter("CommentID", CommentID);
  414. dp.ExecuteNonQuery("UPDATE Comments SET [UserID]=@UserID,[PostID]=@PostID,[Name]=@Name,[Comment]=@Comment,"
  415. + "[Email]=@Email,[WebPage]=@WebPage,[Ip]=@Ip,[CreateDate]=@CreateDate,[Approve]=@Approve,[NotifyMe]=@NotifyMe WHERE [CommentID] = @CommentID");
  416. bReturnValue = dp.Return.Status == DataProcessState.Success;
  417. }
  418. else
  419. {
  420. dp.ExecuteNonQuery("INSERT INTO Comments([UserID],[PostID],[Name],[Comment],[Email],[WebPage],[Ip],[CreateDate],[Approve],[NotifyMe]) "
  421. + "VALUES(@UserID,@PostID,@Name,@Comment,@Email,@WebPage,@Ip,@CreateDate,@Approve,@NotifyMe)");
  422. bReturnValue = dp.Return.Status == DataProcessState.Success;
  423. if (bReturnValue)
  424. {
  425. dp.ExecuteScalar("SELECT @@IDENTITY");
  426. if (dp.Return.Status == DataProcessState.Success)
  427. {
  428. CommentID = Convert.ToInt32(dp.Return.Value);
  429. }
  430. }
  431. }
  432. }
  433. return bReturnValue;
  434. }
  435. #endregion
  436. }