public static CommentModel CopyFrom(this CommentModel model, CommentsWrapper wrapper) { model.BlockComments = wrapper.BlockComments; model.ContentId = wrapper.ContentId; model.Data = wrapper.Data; return model; }
CommentsWrapper INewsService.GetNewsComments(int newsId, CommentSortEnum sortType) { var news = _newsRepository.Query(o => o.Id == newsId).SingleOrDefault(); if (news == null) throw new ArgumentException("Данной новости не существует"); var commentsWrap = new CommentsWrapper { ContentId = news.Id, BlockComments = news.BlockComments }; var comments = news.NewsComments.Select(o => new CommentData { Id = o.Comment.Id, Description = o.Comment.Description, CreateDate = o.Comment.CreateDate, ModifierDate = o.Comment.ModifierDate, Rate = o.Comment.Rate, Writer = o.Comment.Writer != null ? new UserData { Id = o.Comment.Writer.Id, UserName = o.Comment.Writer.UserName, PhotoPath = o.Comment.Writer.PhotoPath, RoleId = o.Comment.Writer.RoleId } : new UserData(), ContentId = o.NewsId }).OrderByDescending(o => o.CreateDate).ToList(); var commentIds = comments.Select(o => o.Id).ToArray(); var userComments = _userCommentRepository.GetData(o => new UserCommentData { UserId = o.UserId, CommentId = o.CommentId, IsInc = o.IsIncrement }, o => o.UserId == CurrentUserId && commentIds.Contains(o.CommentId)).ToDictionary(o => o.CommentId, o => o.IsInc); foreach (var comment in comments.Where(comment => userComments.ContainsKey(comment.Id))) { comment.IsInc = userComments[comment.Id]; } switch (sortType) { case CommentSortEnum.Old: commentsWrap.Data = comments.OrderBy(o => o.CreateDate).ToList(); break; case CommentSortEnum.Popular: commentsWrap.Data = comments.OrderByDescending(o => o.Rate); break; default: commentsWrap.Data = comments; break; } return commentsWrap; }