11using BlogEngine . Core . Data . Contracts ;
22using BlogEngine . Core . Data . Models ;
33using BlogEngine . Core . Data . Services ;
4+ using BlogEngine . Core . Data . ViewModels ;
45using System ;
56using System . Collections . Generic ;
67using 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 {
0 commit comments