PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/YABE/Models/PostDL.cs

#
C# | 421 lines | 374 code | 38 blank | 9 comment | 46 complexity | ac96d88c89b0f93766519c676aef6694 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Collections.Generic;
  13. using System.Data.Linq;
  14. namespace YABE.Models
  15. {
  16. public interface IPostDL
  17. {
  18. List<DateTime> GetPostMonths();
  19. List<Post> GetRecentPosts(int numberPosts);
  20. List<Post> GetPosts();
  21. List<Post> GetPosts(int year);
  22. List<Post> GetPosts(int year, int month);
  23. List<Post> GetPosts(int year, int month, int day);
  24. Post GetPost(int year, int month, int day, string title);
  25. Post GetPostById(int id);
  26. Post GetPostByIdWithUnApprovedComments(int id);
  27. List<Post> GetPostsByTag(string tag);
  28. void SavePosts(IList<Post> posts);
  29. void AddPost(Post post, IList<Tag> tags);
  30. void UpdatePost(Post post, IList<Tag> tags);
  31. void DeletePost(int id);
  32. void AddComment(int PostID,string Author, string Email, string Url, string Comments, DateTime Created,bool IsApproved,
  33. string ClientIP, DateTime Modified);
  34. void ScrubPosts();
  35. }
  36. public class PostDL:IPostDL
  37. {
  38. public List<DateTime> GetPostMonths()
  39. {
  40. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  41. {
  42. var dates = from p in context.Posts
  43. group p by new
  44. {
  45. PostMonth = new DateTime(p.Created.Year, p.Created.Month, 1)
  46. } into g
  47. select g.Key.PostMonth;
  48. return dates.ToList();
  49. }
  50. }
  51. public void ScrubPosts()
  52. {
  53. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  54. {
  55. var posts = from p in context.Posts
  56. where p.Body.Contains("<br>")
  57. select p;
  58. foreach (Post p in posts)
  59. {
  60. p.Body = p.Body.Replace("<br>", "<br/>");
  61. }
  62. context.SubmitChanges();
  63. }
  64. }
  65. public List<Post> GetRecentPosts(int numberPosts)
  66. {
  67. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  68. {
  69. context.DeferredLoadingEnabled = false;
  70. DataLoadOptions dlo = new DataLoadOptions();
  71. dlo.LoadWith<Post>(p => p.Comments);
  72. dlo.AssociateWith<Post>(p => p.Comments.Where(c => c.IsApproved == true));
  73. dlo.LoadWith<Post>(p => p.PostTags);
  74. dlo.LoadWith<PostTag>(pt => pt.Tag);
  75. context.LoadOptions = dlo;
  76. return context.Posts.OrderByDescending( p => p.Created).Take(numberPosts).ToList();
  77. }
  78. }
  79. public List<Post> GetPosts()
  80. {
  81. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  82. {
  83. context.DeferredLoadingEnabled = false;
  84. DataLoadOptions dlo = new DataLoadOptions();
  85. dlo.LoadWith<Post>(p => p.Comments);
  86. dlo.AssociateWith<Post>(p => p.Comments.Where(c => c.IsApproved == true));
  87. dlo.LoadWith<Post>(p => p.PostTags);
  88. dlo.LoadWith<PostTag>(pt => pt.Tag);
  89. context.LoadOptions = dlo;
  90. return context.Posts.ToList();
  91. }
  92. }
  93. public List<Post> GetPosts(int year)
  94. {
  95. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  96. {
  97. context.DeferredLoadingEnabled = false;
  98. DataLoadOptions dlo = new DataLoadOptions();
  99. dlo.LoadWith<Post>(p => p.Comments);
  100. dlo.AssociateWith<Post>(p => p.Comments.Where(c => c.IsApproved == true));
  101. dlo.LoadWith<Post>(p => p.PostTags);
  102. dlo.LoadWith<PostTag>(pt => pt.Tag);
  103. context.LoadOptions = dlo;
  104. return context.Posts.Where(s => s.Created.Year == year).ToList();
  105. }
  106. }
  107. public List<Post> GetPosts(int year, int month)
  108. {
  109. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  110. {
  111. context.DeferredLoadingEnabled = false;
  112. DataLoadOptions dlo = new DataLoadOptions();
  113. dlo.LoadWith<Post>(p => p.Comments);
  114. dlo.AssociateWith<Post>(p => p.Comments.Where(c => c.IsApproved == true));
  115. dlo.LoadWith<Post>(p => p.PostTags);
  116. dlo.LoadWith<PostTag>(pt => pt.Tag);
  117. context.LoadOptions = dlo;
  118. return context.Posts.Where(s => s.Created.Year == year && s.Created.Month == month).ToList();
  119. }
  120. }
  121. public List<Post> GetPosts(int year, int month, int day)
  122. {
  123. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  124. {
  125. context.DeferredLoadingEnabled = false;
  126. DataLoadOptions dlo = new DataLoadOptions();
  127. dlo.LoadWith<Post>(p => p.Comments);
  128. dlo.AssociateWith<Post>(p => p.Comments.Where(c => c.IsApproved == true));
  129. dlo.LoadWith<Post>(p => p.PostTags);
  130. dlo.LoadWith<PostTag>(pt => pt.Tag);
  131. context.LoadOptions = dlo;
  132. return context.Posts.Where(s => s.Created.Year == year && s.Created.Month == month
  133. && s.Created.Day == day).ToList();
  134. }
  135. }
  136. public Post GetPost(int year, int month, int day, string title)
  137. {
  138. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  139. {
  140. context.DeferredLoadingEnabled = false;
  141. DataLoadOptions dlo = new DataLoadOptions();
  142. dlo.LoadWith<Post>(p => p.Comments);
  143. dlo.AssociateWith<Post>(p => p.Comments.Where(c => c.IsApproved == true));
  144. dlo.LoadWith<Post>(p => p.PostTags);
  145. dlo.LoadWith<PostTag>(pt => pt.Tag);
  146. context.LoadOptions = dlo;
  147. return context.Posts.Single(s => s.Created.Year == year && s.Created.Month == month && s.Created.Day == day && s.Title.Replace(' ', '-').ToLower() == title.ToLower());
  148. }
  149. }
  150. public Post GetPostById(int id)
  151. {
  152. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  153. {
  154. context.DeferredLoadingEnabled = false;
  155. DataLoadOptions dlo = new DataLoadOptions();
  156. dlo.LoadWith<Post>(p => p.Comments);
  157. dlo.AssociateWith<Post>(p => p.Comments.Where(c => c.IsApproved == true));
  158. dlo.LoadWith<Post>(p => p.PostTags);
  159. dlo.LoadWith<PostTag>(pt => pt.Tag);
  160. context.LoadOptions = dlo;
  161. return context.Posts.Single(s => s.PostID == id);
  162. }
  163. }
  164. public Post GetPostByIdWithUnApprovedComments(int id)
  165. {
  166. using (YabeDataContext context = YabeDataManager.Instance.DataContext) {
  167. context.DeferredLoadingEnabled = false;
  168. DataLoadOptions dlo = new DataLoadOptions();
  169. dlo.LoadWith<Post>(p => p.Comments);
  170. dlo.LoadWith<Post>(p => p.PostTags);
  171. dlo.LoadWith<PostTag>(pt => pt.Tag);
  172. context.LoadOptions = dlo;
  173. return context.Posts.Single(s => s.PostID == id);
  174. }
  175. }
  176. public List<Post> GetPostsByTag(string tag)
  177. {
  178. if (tag == null)
  179. {
  180. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  181. {
  182. context.DeferredLoadingEnabled = false;
  183. DataLoadOptions dlo = new DataLoadOptions();
  184. dlo.LoadWith<Post>(p => p.Comments);
  185. dlo.AssociateWith<Post>(p => p.Comments.Where(c => c.IsApproved == true));
  186. dlo.LoadWith<Post>(p => p.PostTags);
  187. dlo.LoadWith<PostTag>(pt => pt.Tag);
  188. context.LoadOptions = dlo;
  189. return context.Posts.Where(s => s.PostTags == null || s.PostTags.Count == 0 ).ToList();
  190. }
  191. }
  192. else
  193. {
  194. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  195. {
  196. context.DeferredLoadingEnabled = false;
  197. DataLoadOptions dlo = new DataLoadOptions();
  198. dlo.LoadWith<Post>(p => p.Comments);
  199. dlo.AssociateWith<Post>(p => p.Comments.Where(c => c.IsApproved == true));
  200. dlo.LoadWith<Post>(p => p.PostTags);
  201. dlo.LoadWith<PostTag>(pt => pt.Tag);
  202. context.LoadOptions = dlo;
  203. return context.Posts.Where(s => s.PostTags.Single(
  204. pt => pt.Tag.TagName.ToLower() == tag.Trim().ToLower()) != null).ToList();
  205. }
  206. }
  207. }
  208. public void SavePosts(IList<Post> posts)
  209. {
  210. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  211. {
  212. foreach (Post postItem in posts)
  213. {
  214. Post postItem1 = postItem;
  215. Post p = context.Posts.SingleOrDefault(c =>
  216. c.PostID == postItem1.PostID);
  217. if (p == null)
  218. {
  219. p = new Post()//post does not exist in db
  220. {
  221. Author = postItem.Author,
  222. Title = postItem.Title,
  223. Description = postItem.Description,
  224. Body = postItem.Body,
  225. Created = postItem.Created,
  226. Modified = postItem.Modified,
  227. IsPublic = true,
  228. ShowOnFrontPage = true,
  229. AllowComments = true,
  230. //Categories = postItem.Categories
  231. };
  232. context.Posts.InsertOnSubmit(p);
  233. }
  234. else //update
  235. {
  236. p.Author = postItem.Author;
  237. p.Title = postItem.Title;
  238. p.Description = postItem.Description;
  239. p.Body = postItem.Body;
  240. p.Modified = DateTime.Now;
  241. p.IsPublic = true;
  242. p.ShowOnFrontPage = true;
  243. p.AllowComments = true;
  244. //p.Categories = postItem.Categories;
  245. }
  246. }
  247. try
  248. {
  249. context.SubmitChanges(ConflictMode.ContinueOnConflict);
  250. }
  251. catch (ChangeConflictException)
  252. {
  253. context.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues); //User values will be keeped on the resolution
  254. }
  255. }
  256. }
  257. public void UpdatePost(Post post, IList<Tag> tags)
  258. {
  259. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  260. {
  261. Post p = context.Posts.SingleOrDefault(c =>
  262. c.PostID == post.PostID);
  263. p.AllowComments = post.AllowComments;
  264. p.Author = post.Author;
  265. p.Body = post.Body;
  266. p.Categories = post.Categories;
  267. //p.Comments = post.Comments;
  268. p.Created = post.Created;
  269. p.Description = post.Description;
  270. p.IsPublic = post.IsPublic;
  271. p.Modified = post.Modified;
  272. p.PostTags.Clear();
  273. p.ShowOnFrontPage = post.ShowOnFrontPage;
  274. p.Syndicated = post.Syndicated;
  275. p.Title = post.Title;
  276. foreach (Tag tag in tags)
  277. {
  278. PostTag pt = new PostTag();
  279. pt.TagID = tag.TagID;
  280. //tag.PostTags.Add(pt);
  281. p.PostTags.Add(pt);
  282. }
  283. try
  284. {
  285. context.SubmitChanges(ConflictMode.ContinueOnConflict);
  286. }
  287. catch (ChangeConflictException)
  288. {
  289. context.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues); //User values will be keeped on the resolution
  290. }
  291. }
  292. }
  293. public void AddPost(Post post, IList<Tag> tags)
  294. {
  295. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  296. {
  297. Post p = context.Posts.SingleOrDefault(c =>
  298. c.PostID == post.PostID);
  299. // new post
  300. if (p == null)
  301. {
  302. p = post;
  303. foreach (Tag tag in tags)
  304. {
  305. PostTag pt = new PostTag();
  306. pt.TagID = tag.TagID;
  307. //tag.PostTags.Add(pt);
  308. p.PostTags.Add(pt);
  309. }
  310. context.Posts.InsertOnSubmit(p);
  311. }
  312. else //update
  313. {
  314. p.Author = post.Author;
  315. p.Title = post.Title;
  316. p.Description = post.Description;
  317. p.Body = post.Body;
  318. p.Modified = DateTime.Now;
  319. p.IsPublic = true;
  320. p.ShowOnFrontPage = true;
  321. p.AllowComments = true;
  322. //p.Categories = postItem.Categories;
  323. }
  324. try
  325. {
  326. context.SubmitChanges(ConflictMode.ContinueOnConflict);
  327. }
  328. catch (ChangeConflictException)
  329. {
  330. context.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues); //User values will be keeped on the resolution
  331. }
  332. }
  333. }
  334. //TODO: Check if this deletes all the foreign objects such as comments, tags (dont think so)
  335. public void DeletePost(int id)
  336. {
  337. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  338. {
  339. Post p = context.Posts.SingleOrDefault(c =>
  340. c.PostID == id);
  341. // delete post
  342. if (p != null)
  343. {
  344. context.Posts.DeleteOnSubmit(p);
  345. }
  346. try
  347. {
  348. context.SubmitChanges(ConflictMode.ContinueOnConflict);
  349. }
  350. catch (ChangeConflictException)
  351. {
  352. context.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues); //User values will be keeped on the resolution
  353. }
  354. }
  355. }
  356. public void AddComment(int PostID,string Author, string Email, string Url, string Comments,
  357. DateTime Created,bool IsApproved, string ClientIP, DateTime Modified)
  358. {
  359. using (YabeDataContext context = YabeDataManager.Instance.DataContext)
  360. {
  361. Comment newComment = new Comment();
  362. newComment.PostID = PostID;
  363. newComment.Author = Author;
  364. newComment.Email = Email;
  365. newComment.Url = Url;
  366. newComment.Comments = Comments;
  367. newComment.IsApproved = IsApproved;
  368. newComment.ClientIP = ClientIP;
  369. newComment.Modified = Modified;
  370. newComment.Created = Created;
  371. context.Comments.InsertOnSubmit(newComment);
  372. try
  373. {
  374. context.SubmitChanges(ConflictMode.ContinueOnConflict);
  375. }
  376. catch (ChangeConflictException)
  377. {
  378. context.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues); //User values will be keeped on the resolution
  379. }
  380. }
  381. }
  382. }
  383. }