コード例 #1
0
        public WallPostCommentModel WritePostComment(int userId, int postId, string text)
        {
            WallPostComment comment = new WallPostComment();

            comment.Content      = text;
            comment.WallPostId   = postId;
            comment.WallPost     = ctx.UserPosts.First(p => p.Id == postId);
            comment.Creator      = ctx.Users.First(u => u.Id == userId);
            comment.CreationTime = DateTime.Now;

            if (comment.WallPost.OwnerId != userId)
            {
                Notification notification = Notification.From(DateTime.Now, NotificationType.WallPostWritten,
                                                              comment.Creator, comment.WallPost.Owner, "/Home/Post/" + postId, " коментував запис на вашій стіні");
                ctx.Notifications.Add(notification);
            }

            ctx.UserPostComments.Add(comment);
            ctx.SaveChanges();

            return(ctx.UserPostComments.Where(c => c.WallPostId == postId &&
                                              c.CreationTime == comment.CreationTime).Select(dbComment => new WallPostCommentModel()
            {
                Id = dbComment.Id,
                CreationTime = dbComment.CreationTime,
                Content = dbComment.Content,
                WallPostId = dbComment.WallPostId,
                Creator = new UserShortModel()
                {
                    Id = dbComment.Creator.Id, Username = dbComment.Creator.Username, Avatar = dbComment.Creator.Avatar
                }
            }).OrderByDescending(c => c.Id).Take(1).Single());
        }
コード例 #2
0
        public List <WallPostCommentModel> GetNewWallPostComments(int postId, int?postCommentId)
        {
            WallPost post = ctx.UserPosts.First(p => p.Id == postId);

            if (postCommentId == null)
            {
                try {
                    if (post.Comments.Any())
                    {
                        List <WallPostCommentModel> comments = post.Comments.Select(c => new WallPostCommentModel()
                        {
                            Id           = c.Id,
                            CreationTime = c.CreationTime,
                            Content      = c.Content,
                            WallPostId   = c.WallPostId,
                            Creator      = new UserShortModel()
                            {
                                Id = c.Creator.Id, Username = c.Creator.Username, Avatar = c.Creator.Avatar
                            }
                        }).ToList();

                        return(comments);
                    }
                    else
                    {
                        return(new List <WallPostCommentModel>());
                    }
                }
                catch (Exception e)
                {
                    return(new List <WallPostCommentModel>());
                }
            }
            else
            {
                WallPostComment comment = post.Comments.First(c => c.Id == postCommentId);

                List <WallPostComment> comments =
                    post.Comments.Where(p => comment.CreationTime.Ticks < p.CreationTime.Ticks && comment.Id != p.Id).ToList();

                if (comments.Any())
                {
                    return(comments.Select(c => new WallPostCommentModel()
                    {
                        Id = c.Id,
                        CreationTime = c.CreationTime,
                        Content = c.Content,
                        WallPostId = c.WallPostId,
                        Creator = UserShortModel.From(c.Creator.Id, c.Creator.Username, c.Creator.Avatar)
                    }).ToList());
                }
                else
                {
                    return(new List <WallPostCommentModel>());
                }
            }
        }