PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mojoPortal.Business/ContentRating.cs

#
C# | 441 lines | 290 code | 79 blank | 72 comment | 17 complexity | 61ddade128bd636e28fc1d04330de384 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. // Author: Joe Audette
  2. // Created: 2008-10-06
  3. // Last Modified: 2011-10-05
  4. //
  5. // The use and distribution terms for this software are covered by the
  6. // Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. // which can be found in the file CPL.TXT at the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by
  9. // the terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using mojoPortal.Data;
  16. namespace mojoPortal.Business
  17. {
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. public class ContentRating
  22. {
  23. #region Constructors
  24. public ContentRating()
  25. { }
  26. public ContentRating(Guid rowGuid)
  27. {
  28. GetContentRating(rowGuid);
  29. }
  30. public ContentRating(Guid contentGuid, Guid userGuid)
  31. {
  32. GetContentRating(contentGuid, userGuid);
  33. }
  34. #endregion
  35. #region Private Properties
  36. private Guid rowGuid = Guid.Empty;
  37. private Guid siteGuid = Guid.Empty;
  38. private Guid contentGuid = Guid.Empty;
  39. private Guid userGuid = Guid.Empty;
  40. private string emailAddress = string.Empty;
  41. private int rating = -1;
  42. private string comments = string.Empty;
  43. private string ipAddress = string.Empty;
  44. private DateTime createdUtc;
  45. private DateTime lastModUtc;
  46. #endregion
  47. #region Public Properties
  48. public Guid RowGuid
  49. {
  50. get { return rowGuid; }
  51. set { rowGuid = value; }
  52. }
  53. public Guid SiteGuid
  54. {
  55. get { return siteGuid; }
  56. set { siteGuid = value; }
  57. }
  58. public Guid ContentGuid
  59. {
  60. get { return contentGuid; }
  61. set { contentGuid = value; }
  62. }
  63. public Guid UserGuid
  64. {
  65. get { return userGuid; }
  66. set { userGuid = value; }
  67. }
  68. public string EmailAddress
  69. {
  70. get { return emailAddress; }
  71. set { emailAddress = value; }
  72. }
  73. public int Rating
  74. {
  75. get { return rating; }
  76. set { rating = value; }
  77. }
  78. public string Comments
  79. {
  80. get { return comments; }
  81. set { comments = value; }
  82. }
  83. public string IpAddress
  84. {
  85. get { return ipAddress; }
  86. set { ipAddress = value; }
  87. }
  88. public DateTime CreatedUtc
  89. {
  90. get { return createdUtc; }
  91. set { createdUtc = value; }
  92. }
  93. public DateTime LastModUtc
  94. {
  95. get { return lastModUtc; }
  96. set { lastModUtc = value; }
  97. }
  98. #endregion
  99. #region Private Methods
  100. /// <summary>
  101. /// Gets an instance of ContentRating.
  102. /// </summary>
  103. /// <param name="rowGuid"> rowGuid </param>
  104. private void GetContentRating(Guid rowGuid)
  105. {
  106. IDataReader reader = DBContentRating.GetOne(rowGuid);
  107. PopulateFromReader(reader);
  108. }
  109. /// <summary>
  110. /// Gets an instance of ContentRating.
  111. /// </summary>
  112. /// <param name="rowGuid"> rowGuid </param>
  113. private void GetContentRating(Guid contentGuid, Guid userGuid)
  114. {
  115. IDataReader reader = DBContentRating.GetOneByContentAndUser(contentGuid, userGuid);
  116. PopulateFromReader(reader);
  117. }
  118. private void PopulateFromReader(IDataReader reader)
  119. {
  120. try
  121. {
  122. if (reader.Read())
  123. {
  124. this.rowGuid = new Guid(reader["RowGuid"].ToString());
  125. this.siteGuid = new Guid(reader["SiteGuid"].ToString());
  126. this.contentGuid = new Guid(reader["ContentGuid"].ToString());
  127. this.userGuid = new Guid(reader["UserGuid"].ToString());
  128. this.emailAddress = reader["EmailAddress"].ToString();
  129. this.rating = Convert.ToInt32(reader["Rating"]);
  130. this.comments = reader["Comments"].ToString();
  131. this.ipAddress = reader["IpAddress"].ToString();
  132. this.createdUtc = Convert.ToDateTime(reader["CreatedUtc"]);
  133. this.lastModUtc = Convert.ToDateTime(reader["LastModUtc"]);
  134. }
  135. }
  136. finally
  137. {
  138. reader.Close();
  139. }
  140. }
  141. /// <summary>
  142. /// Persists a new instance of ContentRating. Returns true on success.
  143. /// </summary>
  144. /// <returns></returns>
  145. private bool Create()
  146. {
  147. this.rowGuid = Guid.NewGuid();
  148. int rowsAffected = DBContentRating.Create(
  149. this.rowGuid,
  150. this.siteGuid,
  151. this.contentGuid,
  152. this.userGuid,
  153. this.emailAddress,
  154. this.rating,
  155. this.comments,
  156. this.ipAddress,
  157. this.createdUtc);
  158. return (rowsAffected > 0);
  159. }
  160. /// <summary>
  161. /// Updates this instance of ContentRating. Returns true on success.
  162. /// </summary>
  163. /// <returns>bool</returns>
  164. private bool Update()
  165. {
  166. return DBContentRating.Update(
  167. this.rowGuid,
  168. this.emailAddress,
  169. this.rating,
  170. this.comments,
  171. this.ipAddress,
  172. this.lastModUtc);
  173. }
  174. #endregion
  175. #region Public Methods
  176. /// <summary>
  177. /// Saves this instance of ContentRating. Returns true on success.
  178. /// </summary>
  179. /// <returns>bool</returns>
  180. public bool Save()
  181. {
  182. if (this.rowGuid != Guid.Empty)
  183. {
  184. return Update();
  185. }
  186. else
  187. {
  188. return Create();
  189. }
  190. }
  191. #endregion
  192. #region Static Methods
  193. /// <summary>
  194. /// Deletes an instance of ContentRating. Returns true on success.
  195. /// </summary>
  196. /// <param name="rowGuid"> rowGuid </param>
  197. /// <returns>bool</returns>
  198. public static bool Delete(Guid rowGuid)
  199. {
  200. return DBContentRating.Delete(rowGuid);
  201. }
  202. ///// <summary>
  203. ///// Gets a count of ContentRating.
  204. ///// </summary>
  205. //public static int GetCountByContent(Guid contentGuid)
  206. //{
  207. // return DBContentRating.GetCountByContent(contentGuid);
  208. //}
  209. private static List<ContentRating> LoadListFromReader(IDataReader reader)
  210. {
  211. List<ContentRating> contentRatingList = new List<ContentRating>();
  212. using(reader)
  213. {
  214. while (reader.Read())
  215. {
  216. ContentRating contentRating = new ContentRating();
  217. contentRating.rowGuid = new Guid(reader["RowGuid"].ToString());
  218. contentRating.siteGuid = new Guid(reader["SiteGuid"].ToString());
  219. contentRating.contentGuid = new Guid(reader["ContentGuid"].ToString());
  220. contentRating.userGuid = new Guid(reader["UserGuid"].ToString());
  221. contentRating.emailAddress = reader["EmailAddress"].ToString();
  222. contentRating.rating = Convert.ToInt32(reader["Rating"]);
  223. contentRating.comments = reader["Comments"].ToString();
  224. contentRating.ipAddress = reader["IpAddress"].ToString();
  225. contentRating.createdUtc = Convert.ToDateTime(reader["CreatedUtc"]);
  226. contentRating.lastModUtc = Convert.ToDateTime(reader["LastModUtc"]);
  227. contentRatingList.Add(contentRating);
  228. }
  229. }
  230. return contentRatingList;
  231. }
  232. /// <summary>
  233. /// Gets an IList with page of instances of ContentRating.
  234. /// </summary>
  235. /// <param name="pageNumber">The page number.</param>
  236. /// <param name="pageSize">Size of the page.</param>
  237. /// <param name="totalPages">total pages</param>
  238. public static List<ContentRating> GetPage(Guid contentGuid, int pageNumber, int pageSize, out int totalPages)
  239. {
  240. totalPages = 1;
  241. IDataReader reader = DBContentRating.GetPage(contentGuid, pageNumber, pageSize, out totalPages);
  242. return LoadListFromReader(reader);
  243. }
  244. public static ContentRatingStats GetStats(Guid contentGuid)
  245. {
  246. ContentRatingStats stats = new ContentRatingStats();
  247. using(IDataReader reader = DBContentRating.GetStatsByContent(contentGuid))
  248. {
  249. if (reader.Read())
  250. {
  251. stats.TotalVotes = Convert.ToInt32(reader["TotalRatings"]);
  252. stats.AverageRating = Convert.ToInt32(reader["CurrentRating"]);
  253. }
  254. }
  255. return stats;
  256. }
  257. #endregion
  258. #region Comparison Methods
  259. /// <summary>
  260. /// Compares 2 instances of ContentRating.
  261. /// </summary>
  262. public static int CompareByEmailAddress(ContentRating contentRating1, ContentRating contentRating2)
  263. {
  264. return contentRating1.EmailAddress.CompareTo(contentRating2.EmailAddress);
  265. }
  266. /// <summary>
  267. /// Compares 2 instances of ContentRating.
  268. /// </summary>
  269. public static int CompareByRating(ContentRating contentRating1, ContentRating contentRating2)
  270. {
  271. return contentRating1.Rating.CompareTo(contentRating2.Rating);
  272. }
  273. /// <summary>
  274. /// Compares 2 instances of ContentRating.
  275. /// </summary>
  276. public static int CompareByComments(ContentRating contentRating1, ContentRating contentRating2)
  277. {
  278. return contentRating1.Comments.CompareTo(contentRating2.Comments);
  279. }
  280. /// <summary>
  281. /// Compares 2 instances of ContentRating.
  282. /// </summary>
  283. public static int CompareByIpAddress(ContentRating contentRating1, ContentRating contentRating2)
  284. {
  285. return contentRating1.IpAddress.CompareTo(contentRating2.IpAddress);
  286. }
  287. /// <summary>
  288. /// Compares 2 instances of ContentRating.
  289. /// </summary>
  290. public static int CompareByCreatedUtc(ContentRating contentRating1, ContentRating contentRating2)
  291. {
  292. return contentRating1.CreatedUtc.CompareTo(contentRating2.CreatedUtc);
  293. }
  294. /// <summary>
  295. /// Compares 2 instances of ContentRating.
  296. /// </summary>
  297. public static int CompareByLastModUtc(ContentRating contentRating1, ContentRating contentRating2)
  298. {
  299. return contentRating1.LastModUtc.CompareTo(contentRating2.LastModUtc);
  300. }
  301. public static void RateContent(
  302. Guid siteGuid,
  303. Guid contentGuid,
  304. Guid userGuid,
  305. int rating,
  306. string emailAddress,
  307. string comments,
  308. string ipAddress,
  309. int minutesBetweenAnonymousVotes
  310. )
  311. {
  312. bool userHasRated = false;
  313. if (userGuid != Guid.Empty)
  314. {
  315. userHasRated = (DBContentRating.GetCountByContentAndUser(contentGuid, userGuid) > 0);
  316. }
  317. if (userHasRated)
  318. {
  319. ContentRating contentRating = new ContentRating(contentGuid, userGuid);
  320. contentRating.Rating = rating;
  321. if ((!string.IsNullOrEmpty(emailAddress))&&(contentRating.EmailAddress.Length == 0))
  322. {
  323. contentRating.EmailAddress = emailAddress;
  324. }
  325. if ((!string.IsNullOrEmpty(comments)) && (contentRating.Comments.Length == 0))
  326. {
  327. contentRating.Comments = comments;
  328. }
  329. contentRating.IpAddress = ipAddress;
  330. contentRating.Save();
  331. }
  332. else
  333. {
  334. bool doRating = true;
  335. if (userGuid == Guid.Empty)
  336. {
  337. // if the anonymous user has rated this item from this ip address
  338. // within the last x minutes, don't let them vote again
  339. int countOfRatings = DBContentRating.GetCountOfRatingsSince(
  340. contentGuid,
  341. ipAddress,
  342. DateTime.UtcNow.AddMinutes(-minutesBetweenAnonymousVotes));
  343. doRating = (countOfRatings == 0);
  344. }
  345. if (doRating)
  346. {
  347. DBContentRating.Create(
  348. Guid.NewGuid(),
  349. siteGuid,
  350. contentGuid,
  351. userGuid,
  352. emailAddress,
  353. rating,
  354. comments,
  355. ipAddress,
  356. DateTime.UtcNow);
  357. }
  358. }
  359. }
  360. #endregion
  361. }
  362. }