/YABE/Models/Partial DB Classes/Comment.cs

# · C# · 123 lines · 101 code · 13 blank · 9 comment · 21 complexity · d4438ad044dd344fe72f888803714f62 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.HtmlControls;
  11. using System.Web.UI.WebControls;
  12. using System.Web.UI.WebControls.WebParts;
  13. using System.Xml.Linq;
  14. using YABE.Validators;
  15. namespace YABE.Models
  16. {
  17. public partial class Comment : IRuleEntity
  18. {
  19. //to mockout db connection
  20. public ICommentDL commentDL{get;set;}
  21. #region Implementation of IRuleEntity
  22. public List<RuleViolation> GetRuleViolations()
  23. {
  24. List<RuleViolation> validationIssues = new List<RuleViolation>();
  25. RuleViolation AuthorValidation = CheckAuthorNotEmpty();
  26. if (AuthorValidation != null)
  27. validationIssues.Add(AuthorValidation);
  28. RuleViolation EmailValidation = ValidateEmailNotNull();
  29. if (EmailValidation != null)
  30. validationIssues.Add(EmailValidation);
  31. RuleViolation EmailFormatValidation = ValidateEmailFormat();
  32. if (EmailFormatValidation != null)
  33. validationIssues.Add(EmailFormatValidation);
  34. RuleViolation CommentsEmptyValidation = CheckCommentsNotEmpty();
  35. if (CommentsEmptyValidation != null)
  36. validationIssues.Add(CommentsEmptyValidation);
  37. RuleViolation commentorFrequencyValidation = CheckCommentorFrequency();
  38. if (commentorFrequencyValidation != null)
  39. validationIssues.Add(commentorFrequencyValidation);
  40. return validationIssues;
  41. }
  42. //Email cant be null
  43. public RuleViolation ValidateEmailNotNull()
  44. {
  45. RuleViolation EmailValidation = null;
  46. if(String.IsNullOrEmpty(this.Email)|| (this.Email.Trim().Length==0))
  47. {
  48. EmailValidation = new RuleViolation("EmailNull",String.Empty,Resources.ErrorMessages.EmailIsNull);
  49. }
  50. return EmailValidation;
  51. }
  52. //Email should have valid format
  53. public RuleViolation ValidateEmailFormat()
  54. {
  55. RuleViolation EmailValidation = null;
  56. if(String.IsNullOrEmpty(this.Email))
  57. {
  58. EmailValidation = new RuleViolation("EmailPattern",String.Empty,Resources.ErrorMessages.EmailPattern);
  59. return EmailValidation;
  60. }
  61. string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
  62. @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
  63. @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
  64. Regex regex = new Regex(strRegex);
  65. if(!regex.IsMatch(this.Email))
  66. EmailValidation = new RuleViolation("EmailPattern",this.Email,Resources.ErrorMessages.EmailPattern);
  67. return EmailValidation;
  68. }
  69. //author cant be null
  70. public RuleViolation CheckAuthorNotEmpty()
  71. {
  72. RuleViolation AuthorValidation = null;
  73. if(String.IsNullOrEmpty(this.Author)|| (this.Author.Trim().Length==0))
  74. {
  75. AuthorValidation = new RuleViolation("AuthorEmpty",String.Empty,Resources.ErrorMessages.AuthorEmpty);
  76. }
  77. return AuthorValidation;
  78. }
  79. //comments cant be null
  80. public RuleViolation CheckCommentsNotEmpty()
  81. {
  82. RuleViolation validateCommentsNotEmpty = null;
  83. if((String.IsNullOrEmpty(this.Comments) ) || (this.Comments.Trim().Length==0))
  84. validateCommentsNotEmpty = new RuleViolation("CommentsEmpty",string.Empty,Resources.ErrorMessages.CommentsEmpty);
  85. return validateCommentsNotEmpty;
  86. }
  87. //same commentor cant comment before waiting 30seconds
  88. //commentor id is checked by IP address
  89. public RuleViolation CheckCommentorFrequency()
  90. {
  91. RuleViolation commentorFrequencyValidation = null;
  92. string CommentorIP = this.ClientIP;
  93. //check if any other comments from this ip order by datetime take 1
  94. //below interface checking is done to ease unit testing
  95. if (this.commentDL == null)
  96. commentDL = new YABE.Models.CommentDL();
  97. Comment commentorLatestComment = commentDL.GetLatestCommentByCommentor(this.ClientIP);
  98. if (commentorLatestComment == null)
  99. return commentorFrequencyValidation;
  100. DateTime dateTimeForLatestComment = commentorLatestComment.Created;
  101. TimeSpan timeDifference = DateTime.Now.Subtract(dateTimeForLatestComment);
  102. TimeSpan allowedTimeSpan = new TimeSpan(0,0,0,30);//this magic number could be moved to config
  103. if(timeDifference.TotalSeconds<allowedTimeSpan.TotalSeconds)
  104. commentorFrequencyValidation = new RuleViolation("FrequentCommentor",timeDifference.TotalSeconds,
  105. Resources.ErrorMessages.FrequentCommentor);
  106. return commentorFrequencyValidation;
  107. }
  108. #endregion
  109. }
  110. }