Skip to content

Commit 490552a

Browse files
committed
Added comment view model, refactor comments repository
1 parent 2b834fb commit 490552a

15 files changed

Lines changed: 140 additions & 303 deletions

File tree

BlogEngine/BlogEngine.Core/BlogEngine.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<Compile Include="Data\Contracts\IDashboardRepository.cs" />
123123
<Compile Include="Data\DashboardRepository.cs" />
124124
<Compile Include="Data\Services\Json.cs" />
125+
<Compile Include="Data\ViewModels\CommentsVM.cs" />
125126
<Compile Include="Data\ViewModels\DashboardVM.cs" />
126127
<Compile Include="Data\ViewModels\SettingsVM.cs" />
127128
<Compile Include="Services\Syndication\BlogML\BaseReader.cs" />

BlogEngine/BlogEngine.Core/Data/CommentsRepository.cs

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BlogEngine.Core.Data.Contracts;
22
using BlogEngine.Core.Data.Models;
33
using BlogEngine.Core.Data.Services;
4+
using BlogEngine.Core.Data.ViewModels;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -24,84 +25,56 @@ public class CommentsRepository : ICommentsRepository
2425
/// <param name="filter">Filter expression</param>
2526
/// <param name="order">Sort order</param>
2627
/// <returns>List of comments</returns>
27-
public IEnumerable<CommentItem> GetComments(CommentType commentType = CommentType.All, int take = 10, int skip = 0, string filter = "", string order = "")
28+
public CommentsVM Get()
2829
{
2930
if (!Security.IsAuthorizedTo(Rights.ViewPublicComments))
3031
throw new UnauthorizedAccessException();
3132

32-
if (string.IsNullOrEmpty(filter)) filter = "1==1";
33-
if (string.IsNullOrEmpty(order)) order = "DateCreated desc";
34-
35-
var items = new List<Comment>();
36-
var query = items.AsQueryable().Where(filter);
37-
var comments = new List<CommentItem>();
33+
var vm = new CommentsVM();
34+
var comments = new List<Comment>();
35+
var items = new List<CommentItem>();
3836

3937
var all = Security.IsAuthorizedTo(Rights.EditOtherUsersPosts);
40-
4138
foreach (var p in Post.Posts)
4239
{
4340
if (all || p.Author.ToLower() == Security.CurrentUser.Identity.Name.ToLower())
4441
{
45-
switch (commentType)
46-
{
47-
case CommentType.Pending:
48-
items.AddRange(p.NotApprovedComments);
49-
break;
50-
case CommentType.Pingback:
51-
items.AddRange(p.Pingbacks);
52-
break;
53-
case CommentType.Spam:
54-
items.AddRange(p.SpamComments);
55-
break;
56-
case CommentType.Approved:
57-
items.AddRange(p.ApprovedComments);
58-
break;
59-
default:
60-
items.AddRange(p.Comments);
61-
break;
62-
}
42+
comments.AddRange(p.Comments);
6343
}
64-
}
65-
66-
// if take passed in as 0, return all
67-
if (take == 0) take = items.Count;
68-
69-
var itemList = query.OrderBy(order).Skip(skip).Take(take).ToList();
70-
71-
foreach (var item in itemList)
44+
}
45+
foreach (var c in comments)
7246
{
73-
comments.Add(Json.GetComment(item, itemList));
47+
items.Add(Json.GetComment(c, comments));
7448
}
49+
vm.Items = items;
7550

76-
return comments;
77-
}
51+
vm.Detail = new CommentDetail();
52+
vm.SelectedItem = new CommentItem();
7853

54+
return vm;
55+
}
7956
/// <summary>
80-
/// Single commnet by ID
57+
/// Find by ID
8158
/// </summary>
82-
/// <param name="id">
83-
/// Comment id
84-
/// </param>
85-
/// <returns>
86-
/// A JSON Comment
87-
/// </returns>
88-
public CommentItem FindById(Guid id)
59+
/// <param name="id"></param>
60+
/// <returns></returns>
61+
public CommentDetail FindById(Guid id)
8962
{
9063
if (!Security.IsAuthorizedTo(Rights.ViewPublicComments))
9164
throw new UnauthorizedAccessException();
9265

9366
return (from p in Post.Posts
9467
from c in p.AllComments
9568
where c.Id == id
96-
select Json.GetComment(c, p.AllComments)).FirstOrDefault();
69+
select Json.GetCommentDetail(c)).FirstOrDefault();
9770
}
9871

9972
/// <summary>
10073
/// Add item
10174
/// </summary>
10275
/// <param name="item">Comment</param>
10376
/// <returns>Comment object</returns>
104-
public CommentItem Add(CommentItem item)
77+
public CommentItem Add(CommentDetail item)
10578
{
10679
if (!Security.IsAuthorizedTo(Rights.CreateComments))
10780
throw new UnauthorizedAccessException();
@@ -113,22 +86,17 @@ public CommentItem Add(CommentItem item)
11386

11487
c.Id = Guid.NewGuid();
11588
c.ParentId = item.ParentId;
116-
c.IsApproved = item.IsApproved;
89+
c.IsApproved = true;
11790
c.Content = HttpUtility.HtmlAttributeEncode(item.Content);
11891

119-
if (string.IsNullOrEmpty(item.Author))
92+
c.Author = Security.CurrentUser.Identity.Name;
93+
var profile = AuthorProfile.GetProfile(c.Author);
94+
if (profile != null && !string.IsNullOrEmpty(profile.DisplayName))
12095
{
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;
96+
c.Author = profile.DisplayName;
97+
}
13198

99+
c.Email = Membership.Provider.GetUser(Security.CurrentUser.Identity.Name, true).Email;
132100
c.IP = Utils.GetClientIP();
133101
c.DateCreated = DateTime.Now;
134102
c.Parent = post;
@@ -137,7 +105,6 @@ public CommentItem Add(CommentItem item)
137105
post.Save();
138106

139107
var newComm = post.Comments.Where(cm => cm.Content == c.Content).FirstOrDefault();
140-
141108
return Json.GetComment(newComm, post.Comments);
142109
}
143110
catch (Exception ex)
@@ -180,10 +147,10 @@ public bool Update(CommentItem item, string action)
180147
return true;
181148
}
182149

183-
c.Content = item.Content;
150+
//c.Content = item.Content;
184151
c.Author = item.Author;
185152
c.Email = item.Email;
186-
c.Website = string.IsNullOrEmpty(item.Website) ? null : new Uri(item.Website);
153+
//c.Website = string.IsNullOrEmpty(item.Website) ? null : new Uri(item.Website);
187154

188155
if (item.IsPending)
189156
{

BlogEngine/BlogEngine.Core/Data/Contracts/ICommentsRepository.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
32
using BlogEngine.Core.Data.Models;
3+
using BlogEngine.Core.Data.ViewModels;
44

55
namespace BlogEngine.Core.Data.Contracts
66
{
@@ -10,31 +10,22 @@ namespace BlogEngine.Core.Data.Contracts
1010
public interface ICommentsRepository
1111
{
1212
/// <summary>
13-
/// Comments list
13+
/// Comments view model
1414
/// </summary>
15-
/// <param name="commentType">Comment type</param>
16-
/// <param name="take">Items to take</param>
17-
/// <param name="skip">Items to skip</param>
18-
/// <param name="filter">Filter expression</param>
19-
/// <param name="order">Sort order</param>
20-
/// <returns>List of comments</returns>
21-
IEnumerable<CommentItem> GetComments(CommentType commentType = CommentType.All, int take = 10, int skip = 0, string filter = "", string order = "");
15+
/// <returns></returns>
16+
CommentsVM Get();
2217
/// <summary>
23-
/// Single commnet by ID
18+
/// Comment by ID
2419
/// </summary>
25-
/// <param name="id">
26-
/// Comment id
27-
/// </param>
28-
/// <returns>
29-
/// A JSON Comment
30-
/// </returns>
31-
CommentItem FindById(Guid id);
20+
/// <param name="id"></param>
21+
/// <returns></returns>
22+
CommentDetail FindById(Guid id);
3223
/// <summary>
3324
/// Add item
3425
/// </summary>
3526
/// <param name="item">Comment</param>
3627
/// <returns>Comment object</returns>
37-
CommentItem Add(CommentItem item);
28+
CommentItem Add(CommentDetail item);
3829
/// <summary>
3930
/// Update item
4031
/// </summary>
Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Globalization;
3-
using System.Linq;
42

53
namespace BlogEngine.Core.Data.Models
64
{
@@ -9,84 +7,29 @@ namespace BlogEngine.Core.Data.Models
97
/// </summary>
108
public class CommentDetail
119
{
12-
/// <summary>
13-
/// If checked in the UI
14-
/// </summary>
15-
public bool IsChecked { get; set; }
16-
1710
/// <summary>
1811
/// Gets or sets the Comment Id
1912
/// </summary>
2013
public Guid Id { get; set; }
21-
22-
/// <summary>
23-
/// Is approved
24-
/// </summary>
25-
public bool IsApproved { get; set; }
26-
27-
/// <summary>
28-
/// Is spam
29-
/// </summary>
30-
public bool IsSpam { get; set; }
31-
3214
/// <summary>
33-
/// Comment pending approval
15+
/// Parent comment Id
3416
/// </summary>
35-
public bool IsPending { get; set; }
36-
37-
/// <summary>
38-
/// Can be: approved, pending, spam, pingback
39-
/// </summary>
40-
public string CommentType { get; set; }
41-
17+
public Guid ParentId { get; set; }
4218
/// <summary>
43-
/// Gets or sets the Email
19+
/// Comment post ID
4420
/// </summary>
45-
public string Email { get; set; }
46-
47-
/// <summary>
48-
/// Gets or sets the Author
49-
/// </summary>
50-
public string Author { get; set; }
51-
52-
/// <summary>
53-
/// Gets the avatar image
54-
/// </summary>
55-
public string Avatar { get; set; }
56-
57-
/// <summary>
58-
/// Gets a value indicating whether this comment has nested comments
59-
/// </summary>
60-
public bool HasChildren { get; set; }
61-
62-
/// <summary>
63-
/// Gets or sets the comment title
64-
/// </summary>
65-
public string Title { get; set; }
66-
21+
public Guid PostId { get; set; }
6722
/// <summary>
6823
/// Gets or sets the author's website
6924
/// </summary>
7025
public string Website { get; set; }
71-
72-
/// <summary>
73-
/// Static avatar image
74-
/// </summary>
75-
public string AuthorAvatar { get; set; }
76-
7726
/// <summary>
7827
/// Gets or sets the ip
7928
/// </summary>
8029
public string Ip { get; set; }
81-
82-
/// <summary>
83-
/// Gets or sets the date published
84-
/// </summary>
85-
public string DateCreated { get; set; }
86-
8730
/// <summary>
88-
/// Comment link
31+
/// Comment content
8932
/// </summary>
90-
public string RelativeLink { get; set; }
33+
public string Content { get; set; }
9134
}
9235
}

0 commit comments

Comments
 (0)